openpaperwork / pyocr

A Python wrapper for Tesseract and Cuneiform -- Moved to Gnome's Gitlab
https://gitlab.gnome.org/World/OpenPaperwork/pyocr
930 stars 152 forks source link

AttibuteError: tobytes #45

Closed toxic-0518 closed 7 years ago

toxic-0518 commented 7 years ago

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.

jflesch commented 7 years ago

(I missed the fact that you specified your PIL version in the ticket, sorry).

The problem is that you're using a really old version of PIL. You should be using Pillow (at least >= 2.7).

tostring() is obsolete. tobytes() is the correct way.

jflesch commented 7 years ago

This is neither a PIL bug nor a Pyocr bug. I'm going to close this issue.

toxic-0518 commented 7 years ago

OK! Thank you very much for your explanation!