Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sorl-thumbnail: encoder error -2 when writing image file

I've been using sorl-thumbnail for some time without problems. However, the following error started to appear: encoder error -2 when writing image file.

The following code causes the error:

from sorl.thumbnail import get_thumbnail
photobooth_thumbnail = get_thumbnail(img_file,
    PHOTOBOOTH_THUMB_SIZE, crop='center', quality=99)

being img_file a Django models' ImageField and when PHOTOBOOTH_THUMB_SIZE is "sufficiently large". When I was using PHOTOBOOTH_THUMB_SIZE = '670', everything worked just fine, but when I increased it to PHOTOBOOTH_THUMB_SIZE = '1280', the aforementioned error appeared.

I'm suspecting this is an error in PIL rather than in sorl-thumbnail, given the low level message. I'd like to have bigger thumbnails, so I'd appreciate any help on this. Thanks in advance.

like image 972
Pablo Antonio Avatar asked Jan 31 '26 12:01

Pablo Antonio


1 Answers

I ended up patching the file pil_engine.py in /lib/python2.7/site-packages/sorl/thumbnail/engines:

--- pil_engine.py   2013-09-09 03:58:27.000000000 +0000
+++ pil_engine_new.py   2013-11-05 21:19:15.053034383 +0000
@@ -79,6 +79,7 @@
             image.save(buf, **params)
         except IOError:
             params.pop('optimize')
+            ImageFile.MAXBLOCK = image.size[0] * image.size[1]
             image.save(buf, **params)
         raw_data = buf.getvalue()
         buf.close()

This fixed the issue for me.

like image 141
Pablo Antonio Avatar answered Feb 02 '26 08:02

Pablo Antonio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!