I'm using sorl thumbnail with Django. On my local setup it works fine, but in production the thumbnails are not made.
My code looks like this:
{% load thumbnail %}
{% thumbnail up.image "32x32" crop="center" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% empty %}
<img src="{{ MEDIA_URL }}/images/missing_small.png" alt="" title="" />
{% endthumbnail %}
I enabled logging and the trace looks like this:
Traceback (most recent call last):
[...]
File "/usr/local/lib/python2.6/dist-packages/PIL/ImageFile.py", line
215, in load
raise_ioerror(e)
File "/usr/local/lib/python2.6/dist-packages/PIL/ImageFile.py", line
52, in raise_ioerror
raise IOError(message + " when reading image file")
IOError: broken data stream when reading image file
The error isn't very helpful since the file is there and is readable by all. I'm not sure how to get a more explicit error, or what to try and fix.
And then more baffling is the fact that it works using manage.py shell
In [1]: from sorl.thumbnail import get_thumbnail
In [2]: im = get_thumbnail('/myproject/static/images/user_profiles/1/11-20-2010-2_5.jpg',
'32x32', crop='center' )
In [3]: im
Out[3]: <sorl.thumbnail.images.ImageFile object at 0x29fe090>
In [4]: im.url
Out[4]: 'http://example.com/cache/ff/31/ff318b4a995ff345d1d48e79b67ec62b.jpg'
It made the thumbnail, just won't make one via the template code.
Anyone?
Short answer: uninstall all versions of
ligjpeg
dev files except 6.2. Uninstall all versions ofPIL
. Install an recentsorl.thumbnail
andPIL-1.1.7
into a virtualenv usingpip
so thatPIL
compiles againstlibjpeg6.2
.Detailed answer:
I had a lot of trouble with this on a site I mantain - turns it was due to version conflicts between
PIL
,libjpeg
andsorl
- someone had installed lots of versions of all of them. I had to strip it down to a single install ofPIL1.1.7
,libjpeg6.2
andsorl.thumbnail 11.9
Here's what I had to do (this assumes you're using virtualenv. If you're not, well now's a good time to start):
oh wait, it was also installed via easy_install so I also have to remove PIL from site-packages:
Now I uninstall PIL from my virtualenv
there seems to be a bug in the version of pip i'm using - I have to remove stuff in the virtualenv build dir or pip won't install a later version
now I have to remove the versions of libjpeg that I don't want... You should probably do
... or whatever incantation is necessary. Of course if you have other stuff that depends on that version of
libjpeg
then you're in an interesting situation. I'm sure you can getPIL
to install and build against a specific version oflibjpeg
. Then maybe you can getlibjpeg8
to work ok with this - please reply if you do.On my trainwreck of a server both
libjpeg7
andlibjpeg8
dev files had been installed from source (in addition to the package manager installed version 6.2 - hilarious eh?) so I had to download the source for bothlibjpeg7
andlibjpeg8
, run./configure
and then./make uninstall
. There were still some libs left in/usr/local/lib/
so I had to dorm /usr/local/lib/libjpeg*
.I should have probably just done that last bit but wanted to be thorough.
Check that there are no other installs or remnants of installs of
PIL
orlibjpeg
on your system.Now you can start afresh...
On this battered old CentOS box that gets version 6.2 which seems to work best with
PIL1.1.7
. On recent Debian/Ubuntu you'd have to runnow in my virtualenv run
restart webserver. happiness.
Have you done your syncdb?
Run this to check : python manage.py thumbnail cleanup
Tried everything. Ended up solving itself with a newer Linux distro. Wouldn't work on Ubuntu 10.04 but after that it works fine for me, 11.04, 11.10, etc.
A solutions for lazy people like me is to try another THUMBNAIL_ENGINE in the settings file.
I was facing the same problem. As it obviously results from problems with the image library, for my case I've decided to simply use a different image library - and it worked. Currently there are 3 possible image libraries: PIL (default), Pgmagick, and ImageMagick/GraphicsMagick. Details are explained here.
Of course, one of the alternative libraries must be installed on the server. Otherwiese, this solution won't work.