Home > programming > Fix for JPEG upload problem in Django in virtualenv

Fix for JPEG upload problem in Django in virtualenv

December 2nd, 2009

I have been busy modifying Django‘s models for a project to include a field for uploading an image and was testing the model using Django’s Admin interface when I suddenly got this error:

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):

django-upload-jpeg-problem

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:

  1. I removed PIL by deleting the PIL folder and the PIL.pth file from my project’s virtualenv path.
  2. I then installed the JPEG library using:
    $ sudo aptitude install libjpeg libjpeg-dev
  3. 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):

pil-install-without-jpeg-library-1While this PIL Setup Summary says there is JPEG support (click on it for a larger view):

pil-install-with-jpeg-library-1

I must have missed that during my virtualenv installation which caused me hours of gasping agony (as if being constricted by a python). :D

Thankfully I found the fix.  Kudos to Greg Pinero for posting it.

Happy Coding!

List of related articles on myListBoard.com:

Author: Cyril Pauya Categories: programming Tags: , , , , ,
  1. Chris
    March 8th, 2010 at 07:12 | #1

    This was driving me bonkers. Thanks for posting.

  2. March 14th, 2010 at 03:34 | #2

    @Chris
    Glad to be of help. :)

  3. June 27th, 2010 at 01:26 | #3

    thanks! this what i am looking for. great job.

  4. January 6th, 2011 at 00:23 | #4

    Thanks for posting it!!!!!!
    I spent hours to figure it out, I almost recreated my virtualenv without the no site-packages directive.
    This worked great:)

    lzantal

  5. VidJa
    March 5th, 2012 at 20:25 | #6

    please not this post, otherwise PIL still won’t compile on 64 bit systems
    http://jj.isgeek.net/2011/09/install-pil-with-jpeg-support-on-ubuntu-oneiric-64bits/

  6. April 20th, 2012 at 07:47 | #7

    Many Thanks for sharing, so yes, my problem is when using Django in virtualenv, and it did not appear when deactivating the virtualenv!
    However I tried your steps except since I do noy have aptitude, I used:
    $ sudo apt-get install libjpeg8
    $ sudo apt-get install libjpeg8-dev
    then re-installed PIL, but still does not work inside thevirtualenv:S

    Any help?

  7. February 9th, 2013 at 01:22 | #8

    @Montaro Sorry for the very late reply. :)

    And for those looking for an update, here’s a post at StackOverflow that helped me solve this same problem on an Ubuntu 12.04 linode box: http://stackoverflow.com/questions/10658516/django-says-all-images-invalid-but-pil-works

  8. March 7th, 2013 at 18:09 | #9

    I got someting strage.. I’m using virtualenv and installation is ok, but:

    1) JPEG support is OK

    2) in python manage.py shell this code runs okay

    >>> from PIL import Image
    >>> img = Image.open(“/file.jpg”)
    >>> img.size
    (200, 243)
    >>> img.thumbnail([100, 100], Image.ANTIALIAS)
    >>> img.size
    (82, 100)
    >>>
    >>> img.save(‘/file_100x100.jpg’, quality=90, optimize=1)

    BUT my app says that “decoder jpeg not available”

  9. March 7th, 2013 at 19:07 | #10

    @Alex Matveev
    My bad, in app .py it was “import Image”, not “from PIL import Image” =D
    Thank you for your post anyway )

  1. September 5th, 2011 at 16:35 | #1