PyOCR
PyOCR 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 tools
- Libtesseract (Python bindings for the C API)
- Tesseract (wrapper: fork + exec)
- Cuneiform (wrapper: fork + exec)
Features
- Supports all the image formats supported by Pillow,
including jpeg, png, gif, bmp, tiff and others
- Various 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 format
- PDF generation (libtesseract only)
Limitations
- hOCR: Only a subset of the specification is supported. For instance, pages and
paragraph positions are not stored.
Installation
sudo pip3 install pyocr
or the manual way:
mkdir -p ~/git ; cd git
git clone https://gitlab.gnome.org/World/OpenPaperwork/pyocr.git
cd pyocr
make install
Usage
Initialization
from PIL import Image
import sys
import pyocr
import pyocr.builders
tools = pyocr.get_available_tools()
if len(tools) == 0:
print("No OCR tool found")
sys.exit(1)
tool = tools[0]
print("Will use tool '%s'" % (tool.get_name()))
langs = tool.get_available_languages()
print("Available languages: %s" % ", ".join(langs))
lang = langs[0]
print("Will use lang '%s'" % (lang))
Image to text
txt = tool.image_to_string(
Image.open('test.png'),
lang=lang,
builder=pyocr.builders.TextBuilder()
)
word_boxes = tool.image_to_string(
Image.open('test.png'),
lang="eng",
builder=pyocr.builders.WordBoxBuilder()
)
line_and_word_boxes = tool.image_to_string(
Image.open('test.png'), lang="fra",
builder=pyocr.builders.LineBoxBuilder()
)
digits = tool.image_to_string(
Image.open('test-digits.png'),
lang=lang,
builder=pyocr.tesseract.DigitBuilder()
)
Argument '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 exception pyocr.PyocrException
will be raised.
An exception MAY be raised if the input image contains no
text at all (depends on the OCR tool behavior).
Orientation detection
Currently only available with Tesseract or Libtesseract.
if tool.can_detect_orientation():
try:
orientation = tool.detect_orientation(
Image.open('test.png'),
lang='fra'
)
except pyocr.PyocrException as exc:
print("Orientation detection failed: {}".format(exc))
return
print("Orientation: {}".format(orientation))
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 files
Writing:
import codecs
import pyocr
import pyocr.builders
tool = pyocr.get_available_tools()[0]
builder = pyocr.builders.TextBuilder()
txt = tool.image_to_string(
Image.open('test.png'),
lang=lang,
builder=builder
)
with codecs.open("toto.txt", 'w', encoding='utf-8') as file_descriptor:
builder.write_file(file_descriptor, txt)
Reading:
import codecs
import pyocr.builders
builder = pyocr.builders.TextBuilder()
with codecs.open("toto.txt", 'r', encoding='utf-8') as file_descriptor:
txt = builder.read_file(file_descriptor)
Writing and reading hOCR files
Writing:
import codecs
import pyocr
import pyocr.builders
tool = pyocr.get_available_tools()[0]
builder = pyocr.builders.LineBoxBuilder()
line_boxes = tool.image_to_string(
Image.open('test.png'),
lang=lang,
builder=builder
)
with codecs.open("toto.html", 'w', encoding='utf-8') as file_descriptor:
builder.write_file(file_descriptor, line_boxes)
Reading:
import codecs
import pyocr.builders
builder = pyocr.builders.LineBoxBuilder()
with codecs.open("toto.html", 'r', encoding='utf-8') as file_descriptor:
line_boxes = builder.read_file(file_descriptor)
Generating PDF file from an image
With libtesseract >= 4, it's possible to generate a PDF from an image:
import PIL.Image
import pyocr
image = PIL.Image.open("image.jpg")
builder = pyocr.libtesseract.LibtesseractPdfBuilder()
builder.add_image(image)
builder.set_lang("deu")
builder.set_output_file("output_filename")
builder.build()
Add text layer to PDF
import pyocr
import pdf2image
images = pdf2image.convert_from_path("file.pdf", dpi=200, fmt='jpg')
builder = pyocr.libtesseract.LibtesseractPdfBuilder()
for image in images:
builder.add_image(image)
builder.set_output_file("output")
builder.build()
Beware this code hasn't been adapted to libtesseract 3 yet.
Dependencies
- PyOCR requires Python 3.4 or later.
- You will need Pillow
or Python Imaging Library (PIL). Under Debian/Ubuntu, Pillow is in
the package
python-pil
(python3-pil
for the Python 3
version). - Install an OCR:
- libtesseract
('libtesseract3' + 'tesseract-ocr-<lang>' in Debian).
- or tesseract-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 Cuneiform
Tests
make check
make test
Tests are made to be run without external dependencies (no Tesseract or Cuneiform needed).
OCR on natural scenes
If 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 is Stroke Width
Transform.
Contact
Applications that use PyOCR
If you know of any other applications that use Pyocr, please
tell us :-)
Copyright
PyOCR is released under the GPL v3+.
Copyright belongs to the authors of each piece of code
(see the file AUTHORS for the contributors list, and
git blame
to know which lines belong to which author).
https://gitlab.gnome.org/World/OpenPaperwork/pyocr