-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_pyocr.py
41 lines (34 loc) · 1.16 KB
/
test_pyocr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 6 17:38:18 2014
@author: rafik
"""
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()))
# Ex: Will use tool 'tesseract'
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'
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=lang,
builder=pyocr.builders.WordBoxBuilder())
line_and_word_boxes = tool.image_to_string(
Image.open('test.png'), lang=lang,
builder=pyocr.builders.LineBoxBuilder())
# Digits - Only Tesseract
digits = tool.image_to_string(Image.open('test-digits.png'),
lang=lang,
builder=pyocr.tesseract.DigitBuilder())