Package: pyocr
PyOCRPyOCR is an optical character recognition (OCR) tool wrapper for python.
That is, it helps using various OCR tools from a Python program.It has been tested only on GNU/Linux systems. It should also work on similar
systems (*BSD, etc). It may or may not work on Windows, MacOSX, etc.Supported OCR toolsLibtesseract (Python bindings for the C API)Tesseract (wrapper: fork + exec)Cuneiform (wrapper: fork + exec)FeaturesSupports all the image formats supported byPillow,
including jpeg, png, gif, bmp, tiff and othersVarious output types: text only, bounding boxes, etc.Orientation detection (Tesseract and libtesseract only)Can focus on digits only (Tesseract and libtesseract only)Can save and reload boxes in hOCR formatPDF generation (libtesseract only)LimitationshOCR: Only a subset of the specification is supported. For instance, pages and
paragraph positions are not stored.Installationsudopip3installpyocr# Python 3.Xor the manual way:mkdir-p~/git;cdgit
gitclonehttps://gitlab.gnome.org/World/OpenPaperwork/pyocr.gitcdpyocr
makeinstall# will run 'python ./setup.py install'UsageInitializationfromPILimportImageimportsysimportpyocrimportpyocr.builderstools=pyocr.get_available_tools()iflen(tools)==0:print("No OCR tool found")sys.exit(1)# The tools are returned in the recommended order of usagetool=tools[0]print("Will use tool '%s'"%(tool.get_name()))# Ex: Will use tool 'libtesseract'langs=tool.get_available_languages()print("Available languages:%s"%", ".join(langs))lang=langs[0]print("Will use lang '%s'"%(lang))# Ex: Will use lang 'fra'# Note that languages are NOT sorted in any way. Please refer# to the system locale settings for the default language# to use.Image to texttxt=tool.image_to_string(Image.open('test.png'),lang=lang,builder=pyocr.builders.TextBuilder())# txt is a Python stringword_boxes=tool.image_to_string(Image.open('test.png'),lang="eng",builder=pyocr.builders.WordBoxBuilder())# list of box objects. For each box object:#   box.content is the word in the box#   box.position is its position on the page (in pixels)## Beware that some OCR tools (Tesseract for instance)# may return empty boxesline_and_word_boxes=tool.image_to_string(Image.open('test.png'),lang="fra",builder=pyocr.builders.LineBoxBuilder())# list of line objects. For each line object:#   line.word_boxes is a list of word boxes (the individual words in the line)#   line.content is the whole text of the line#   line.position is the position of the whole line on the page (in pixels)## Each word box object has an attribute 'confidence' giving the confidence# score provided by the OCR tool. Confidence score depends entirely on# the OCR tool. Only supported with Tesseract and Libtesseract (always 0# with Cuneiform).## Beware that some OCR tools (Tesseract for instance) may return boxes# with an empty content.# Digits - Only Tesseract (not 'libtesseract' yet !)digits=tool.image_to_string(Image.open('test-digits.png'),lang=lang,builder=pyocr.tesseract.DigitBuilder())# digits is a python stringArgument 'lang' is optional. The default value depends of
the tool used.Argument 'builder' is optional. Default value is
builders.TextBuilder().If the OCR fails, an exceptionpyocr.PyocrExceptionwill be raised.An exception MAY be raised if the input image contains no
text at all (depends on the OCR tool behavior).Orientation detectionCurrently only available with Tesseract or Libtesseract.iftool.can_detect_orientation():try:orientation=tool.detect_orientation(Image.open('test.png'),lang='fra')exceptpyocr.PyocrExceptionasexc:print("Orientation detection failed:{}".format(exc))returnprint("Orientation:{}".format(orientation))# Ex: Orientation: {#   'angle': 90,#   'confidence': 123.4,# }Angles are given in degrees (range: [0-360[). Exact possible
values depend of the tool used. Tesseract only returns angles =
0, 90, 180, 270.Confidence is a score arbitrarily defined by the tool. It MAY not
be returned.detect_orientation() MAY raise an exception if there is no text
detected in the image.Writing and reading text filesWriting:importcodecsimportpyocrimportpyocr.builderstool=pyocr.get_available_tools()[0]builder=pyocr.builders.TextBuilder()txt=tool.image_to_string(Image.open('test.png'),lang=lang,builder=builder)# txt is a Python stringwithcodecs.open("toto.txt",'w',encoding='utf-8')asfile_descriptor:builder.write_file(file_descriptor,txt)# toto.txt is a simple text file, encoded in utf-8Reading:importcodecsimportpyocr.buildersbuilder=pyocr.builders.TextBuilder()withcodecs.open("toto.txt",'r',encoding='utf-8')asfile_descriptor:txt=builder.read_file(file_descriptor)# txt is a Python stringWriting and reading hOCR filesWriting:importcodecsimportpyocrimportpyocr.builderstool=pyocr.get_available_tools()[0]builder=pyocr.builders.LineBoxBuilder()line_boxes=tool.image_to_string(Image.open('test.png'),lang=lang,builder=builder)# list of LineBox (each box points to a list of word boxes)withcodecs.open("toto.html",'w',encoding='utf-8')asfile_descriptor:builder.write_file(file_descriptor,line_boxes)# toto.html is a valid XHTML fileReading:importcodecsimportpyocr.buildersbuilder=pyocr.builders.LineBoxBuilder()withcodecs.open("toto.html",'r',encoding='utf-8')asfile_descriptor:line_boxes=builder.read_file(file_descriptor)# list of LineBox (each box points to a list of word boxes)Generating PDF file from an imageWith libtesseract >= 4, it's possible to generate a PDF from an image:importPIL.Imageimportpyocrimage=PIL.Image.open("image.jpg")builder=pyocr.libtesseract.LibtesseractPdfBuilder()builder.add_image(image)# multiple images are added as separate pagesbuilder.set_lang("deu")# optionalbuilder.set_output_file("output_filename")# .pdf will be appendedbuilder.build()Add text layer to PDFimportpyocrimportpdf2imageimages=pdf2image.convert_from_path("file.pdf",dpi=200,fmt='jpg')builder=pyocr.libtesseract.LibtesseractPdfBuilder()forimageinimages:builder.add_image(image)builder.set_output_file("output")# .pdf will be appendedbuilder.build()Beware this code hasn't been adapted to libtesseract 3 yet.DependenciesPyOCR requires Python 3.4 or later.You will needPillowor Python Imaging Library (PIL). Under Debian/Ubuntu, Pillow is in
the packagepython-pil(python3-pilfor the Python 3
version).Install an OCR:libtesseract('libtesseract3' + 'tesseract-ocr-<lang>' in Debian).ortesseract-ocr('tesseract-ocr' + 'tesseract-ocr-<lang>' in Debian).
You must be able to invoke the tesseract command as "tesseract".
PyOCR is tested with Tesseract >= 3.01 only.or CuneiformTestsmakecheck# requires pyflake8maketest# requires tox, pytest and python3Tests are made to be run without external dependencies (no Tesseract or Cuneiform needed).OCR on natural scenesIf you want to run OCR on natural scenes (photos, etc), you will have to filter
the image first. There are many algorithms possible to do that. One of those
who gives the best results isStroke Width
Transform.ContactForumBug trackerApplications that use PyOCRMayan EDMSPaperlessPaperworkIf you know of any other applications that use Pyocr, pleasetell us:-)CopyrightPyOCR is released under the GPL v3+.
Copyright belongs to the authors of each piece of code
(see the file AUTHORS for the contributors list, andgit blameto know which lines belong to which author).https://gitlab.gnome.org/World/OpenPaperwork/pyocr