OCR with Python


# Purpose: To make OCR on a directory containing image files, 

#          recognized texts are stored in the text files of a target directory.


Step 1 : Install tesseract for Python :


==> https://www.pyimagesearch.com/2017/07/10/using-tesseract-ocr-python/


Step 2 : Eventually install additional language packs


==> https://ocrmypdf.readthedocs.io/en/latest/languages.html



Source code for example : 


from PIL import Image

import pytesseract
import os

DirIn = 'C:/TestIn'     # Directory containing scanned pictures

DirOut = 'C:/TestOut'   # Output directory

FileList = [ f for f in os.listdir(DirIn) if os.path.isfile(os.path.join(DirIn,f)) ]


for i in range(0,len(FileList)):


inFile = FileList[i]


inFileFullPath = DirIn + '/' + FileList[i]


target = inFile.replace('.gif','.txt')


target = DirOut + '/' + target

    
outFile = open(target,'wb')

    # German text 

text = pytesseract.image_to_string(Image.open(inFileFullPath), lang='deu')

outFile.write(text.encode('utf-8'))


outFile.close()



File download : OCR.py

Commentaires

Posts les plus consultés de ce blog

SQL script to HTML