AttibuteError: tobytes
Created by: toxic-0518
I met a problem when using this wrapper. My test code goes here:
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 lang '%s'" % (lang))
txt = tool.image_to_string(
Image.open('test.jpg'),
lang="eng",
builder=pyocr.builders.TextBuilder()
)
print txt
$ python test.py
Will use tool 'Tesseract (C-API)'
Available languages: char100,digit,chi_sim,eng
Will use lang 'eng'
Traceback (most recent call last):
File "test.py", line 23, in <module>
builder=pyocr.builders.TextBuilder()
File "build/bdist.linux-x86_64/egg/pyocr/libtesseract/__init__.py", line 96, in image_to_string
File "build/bdist.linux-x86_64/egg/pyocr/libtesseract/tesseract_raw.py", line 359, in set_image
File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 512, in __getattr__
raise AttributeError(name)
AttributeError: tobytes
My PIL version is 1.1.7 with libjpeg(JPEG support) and zlib(PNG/ZIP support)
I changed the source code in tesseract_raw.py relpaced line 359 with:
try:
imgdata = image.tobytes("raw", "RGB")
except AttributeError:
imgdata = image.tostring("raw", "RGB")
and the tool finally worked well. I think this is a PIL bug but I don't know how it comes.