Skip to content

Commit

Permalink
Make image loading compatible with Pillow<2.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaiter committed Oct 26, 2016
1 parent c690c99 commit 1c89c0a
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions pyclstm.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ cdef load_img(img, _clstm.Tensor2 *data):
:param img: Image
:type img: :py:class:`PIL.Image.Image`
"""
data.resize(img.width, img.height)
if hasattr(img, 'width'):
width, height = img.width, img.height
elif hasattr(img, 'size'):
width, height = img.size
data.resize(width, height)
imgdata = img.load()
for i in range(img.width):
for j in range(img.height):
for i in range(width):
for j in range(height):
px = imgdata[i, j]
# Pillow returns pixels as [0, 255], but we need [0, 1]
if isinstance(px, tuple):
Expand Down Expand Up @@ -182,10 +186,10 @@ cdef class ClstmOcr:
:rtype: unicode
"""
cdef _clstm.Tensor2 data
if hasattr(img, 'width'):
load_img(img, &data)
elif hasattr(img, 'shape'):
if hasattr(img, 'shape'):
load_nparray(img, &data)
else:
load_img(img, &data)
return self._ocr.train_utf8(
data.map(), text.encode('utf8')).decode('utf8')

Expand All @@ -198,10 +202,10 @@ cdef class ClstmOcr:
:rtype: unicode
"""
cdef _clstm.Tensor2 data
if hasattr(img, 'width'):
load_img(img, &data)
elif hasattr(img, 'shape'):
if hasattr(img, 'shape'):
load_nparray(img, &data)
else:
load_img(img, &data)
return self._ocr.predict_utf8(data.map()).decode('utf8')

def recognize_chars(self, img):
Expand All @@ -218,10 +222,10 @@ cdef class ClstmOcr:
cdef vector[_clstm.CharPrediction] preds
cdef vector[_clstm.CharPrediction].iterator pred_it
cdef wchar_t[2] cur_char
if hasattr(img, 'width'):
load_img(img, &data)
elif hasattr(img, 'shape'):
if hasattr(img, 'shape'):
load_nparray(img, &data)
else:
load_img(img, &data)
self._ocr.predict(preds, data.map())
for i in range(preds.size()):
cur_char[0] = preds[i].c
Expand Down

0 comments on commit 1c89c0a

Please sign in to comment.