Django ImageField issue with JPEG's

Posted by Kieran Lynn on Stack Overflow See other posts from Stack Overflow or by Kieran Lynn
Published on 2010-12-27T23:32:24Z Indexed on 2010/12/27 23:54 UTC
Read the original article Hit count: 429

Filed under:
|
|
|

I am having a major issue with PIL (Python Image Library) in Django and have jumpped through a lot of hoops and have thus far not been able to figure out what the root of the issue is.

The problem essentially breaks down to not being able to upload JPEG images through the ImageField in the Django admin. But the issue is not as simple as installing libjpeg.

First, I installed PIL (through Buildout) and realized once it was installed that I had not installed libjpeg because JPEG support was not available.

Having not setup the server myself, I just assumed that it was not installed and I compiled libjpeg 8 from the source. This ended up in my /usr/local/lib/ directory. I cleared out my Buildout files and rebuilt everything. This time when PIL compiled I had JPEG support. But I went to the Django Admin and tried to upload a JPEG though an ImageField with no luck. I got the "Upload a valid image. The file you uploaded was either not an image or a corrupted image" error. Just as a test I opened up a the Djano shell and ran the following:

> import Image
> i = Image.open( "/absolute_path/file.jpg" )
> print i
<JpegImagePlugin.JpegImageFile image mode=RGB size=940x375 at 0x7F908C529BD8>

This runs with no errors and shows that PIL is able to open JPEG's.

After doing some reading, I come across this thread: Is it possible to control which libraries apache uses?

Looks like PHP also uses libjpeg and is loading before Django, and therefor loading libjpeg 6.2 before. This is show when using lsof:

COMMAND  PID     USER  FD   TYPE DEVICE SIZE/OFF   NODE NAME
apache2 2561 www-data mem    REG  202,1   146032 639276 /usr/lib/libjpeg.so.62.0.0

So my thought is that I should be using libjpeg 6.2. So I removed libjpeg located in my /usr/local/lib directory. After rereading the PIL installation instructions, I realized that I might not have the dev/header files for libjpeg that PIL needs. So I also uninstalled libjpeg using the aptitude uninstaller (sudo aptitude remove libjpeg62). Then to ensure that I got the header files that PIL needed I installed libjpeg using aptitude: (sudo aptget install libjpeg62-dev).

From here I cleaned out my Buildout directory, and reran Buildout, which in turn reinstalled PIL. Once again, I have JPEG support, now using the libjpeg62.

So I go to test in the Django Admin. Still no JPEG support. So I wanted to test JPEG support in general and see if the exception was not handled, what kind of error it would throw. So in my homepage view I added the following code to open a JPEG image:

import Image
i = Image.open( "/absolute_path/file.jpg" )
v = i.verify()

Then I pass i to the HTML view just to easily see the output. I deploy these changes to the server and restart. I am surprised not to see an error and get the following output:

{{ i }} - <JpegImagePlugin.JpegImageFile image mode=RGB size=940x375 at 0x7F908C529BD8>
{{ v }} - None

So at this point I am really confused:

  1. Why can I successfully open a JPEG while the admin cannot?
  2. Am I missing something, is this not an issue with libjpeg?
  3. If not an issue with libjpeg, why can I upload a PNG with no issues?

Any help would be much appreciated, I have been on this for 2 days debugging with no luck.

Setup: 1. Rackspace Cloud Server 2. Ubuntu 10.04 3. Django 1.2.3 (Installed though Buildout) 4. PIL 1.1.7 (Installed though Buildout) 5. libjpeg 6.2 (installed through aptitude (sudo aptget install libjpeg62-dev)

© Stack Overflow or respective owner

Related posts about django

Related posts about pil