Skip to content

Commit 4db4bb0

Browse files
committed
Make image loading compatible with Pillow<2.9.0
1 parent c690c99 commit 4db4bb0

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

pyclstm.pyx

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,14 @@ cdef load_img(img, _clstm.Tensor2 *data):
4949
:param img: Image
5050
:type img: :py:class:`PIL.Image.Image`
5151
"""
52-
data.resize(img.width, img.height)
52+
if hasattr(img, 'width'):
53+
width, height = img.width, img.height
54+
elif hasattr(img. 'size'):
55+
width, height = img.size
56+
data.resize(width, height)
5357
imgdata = img.load()
54-
for i in range(img.width):
55-
for j in range(img.height):
58+
for i in range(width):
59+
for j in range(height):
5660
px = imgdata[i, j]
5761
# Pillow returns pixels as [0, 255], but we need [0, 1]
5862
if isinstance(px, tuple):
@@ -182,10 +186,10 @@ cdef class ClstmOcr:
182186
:rtype: unicode
183187
"""
184188
cdef _clstm.Tensor2 data
185-
if hasattr(img, 'width'):
186-
load_img(img, &data)
187-
elif hasattr(img, 'shape'):
189+
if hasattr(img, 'shape'):
188190
load_nparray(img, &data)
191+
else:
192+
load_img(img, &data)
189193
return self._ocr.train_utf8(
190194
data.map(), text.encode('utf8')).decode('utf8')
191195

@@ -198,10 +202,10 @@ cdef class ClstmOcr:
198202
:rtype: unicode
199203
"""
200204
cdef _clstm.Tensor2 data
201-
if hasattr(img, 'width'):
202-
load_img(img, &data)
203-
elif hasattr(img, 'shape'):
205+
if hasattr(img, 'shape'):
204206
load_nparray(img, &data)
207+
else:
208+
load_img(img, &data)
205209
return self._ocr.predict_utf8(data.map()).decode('utf8')
206210

207211
def recognize_chars(self, img):
@@ -218,10 +222,10 @@ cdef class ClstmOcr:
218222
cdef vector[_clstm.CharPrediction] preds
219223
cdef vector[_clstm.CharPrediction].iterator pred_it
220224
cdef wchar_t[2] cur_char
221-
if hasattr(img, 'width'):
222-
load_img(img, &data)
223-
elif hasattr(img, 'shape'):
225+
if hasattr(img, 'shape'):
224226
load_nparray(img, &data)
227+
else:
228+
load_img(img, &data)
225229
self._ocr.predict(preds, data.map())
226230
for i in range(preds.size()):
227231
cur_char[0] = preds[i].c

0 commit comments

Comments
 (0)