Search code examples
djangopython-imaging-librarysorl-thumbnail

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.


Solution

  • 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.