Fix for JPEG upload problem in Django in virtualenv
Upload a valid image. The file you uploaded was either not an image or a corrupted image.
Here’s a screenshot (click on it for a larger view):
This has worked before so I was stumped to find out it’s giving me a red flag now.
Django is using Python Imaging Library or PIL for images so understandably the culprit may be on this level. I tested PIL in Python’s shell using these commands:
$ python >>> from PIL import Image >>> img = Image.open("/home/cyril/Pictures/Anatomy.jpg") >>> img.verify() >>> img.size (1600,1200)
Clearly, nothing is wrong with my PIL installation.
I began searching the net and found the following fix: Fixing – PIL IOError: decoder jpeg not available. Obviously, the author’s problems are similar to mine but he wasn’t using Django, only Python.
Since I have been using virtualenv to sandbox my Django projects, I have a similar but different approach.
Here are the steps I took to fix this problem:
- I removed PIL by deleting the PIL folder and the PIL.pth file from my project’s virtualenv path.
- I then installed the JPEG library using:
sudo aptitude install libjpeg libjpeg-dev
- I then re-installed PIL on my project using:
$ pip install -E ../projectname/ -r requirements.txt
After this, I have successfully uploaded a .jpg file in Django’s admin interface.
This fix is useful if you are using virtualenv to sandbox your Django projects like me. Here is my post on how to do this. If you are not using virtualenv, just do the above steps but re-install PIL as you normally do using easy_install or pip.
A note of advice when installing PIL. Read the PIL Setup Summary displayed after the installation.
This PIL Setup Summary says there is No JPEG support (click on it for a larger view):
I must have missed that during my virtualenv installation which caused me hours of gasping agony (as if being constricted by a python).
Thankfully I found the fix. Kudos to Greg Pinero for posting it.
List of related articles on myListBoard.com: