17

I'm trying to add images to my models in my Django app.

models.py

class ImageMain(models.Model):
  product = models.ForeignKey(Product)
  photo = models.ImageField(upload_to='products')

In development mode, every time I try to upload the image via Django admin, I keep getting:

Upload a valid image. The file you uploaded was either not an image or a corrupted image.

I installed libjpeg via fink and then installed PIL 1.1.6 on ox X 10.5.7

from PIL import Image
file = open('/Users/Bryan/work/review_app/media/lcdtvs/samsung_UN46B6000_front.jpg', 'r')
trial_image = Image.open(file)
trial_image.verify()

It seems that the jpg is valid based on that session. However, it does not load. I have tried other jpgs, they don't work either.

What could be going wrong?

I was able to successfully upload a png file.

Timmy O'Mahony
  • 53,000
  • 18
  • 155
  • 177
BryanWheelock
  • 12,146
  • 18
  • 64
  • 109

9 Answers9

16

Did you install libjpeg after PIL was already compiled/installed on the system? Maybe it can't find the decoder properly?

Here's one set of instructions I found on getting libjpeg and PIL playing nicely on MacOS (see towards the end; looks like you may need to explicitly set the dir of the decoder):

http://djangodays.com/2008/09/03/django-imagefield-validation-error-caused-by-incorrect-pil-installation-on-mac/

pithyless
  • 1,659
  • 2
  • 16
  • 31
16

I met the same problem in Ubuntu server, then you can fix it by installing libjpeg-dev before PIL.

sudo apt-get install libjpeg-dev
sudo pip install PIL --upgrade

and if you already installed libjpeg-dev after PIL. then you can remove PIL first, and try to reinstall PIL as following.

sudo pip uninstall PIL
sudo apt-get install libjpeg-dev
sudo pip install PIL

It works for me, and hope it works for you.

yprez
  • 14,854
  • 11
  • 55
  • 70
hyliker
  • 178
  • 1
  • 5
  • I had pillow installed as well - and had to uninstall / reinstall after installing libjpeg-dev. – stephendwolff Sep 10 '12 at 08:44
  • This did not work for me. Even with libjpeg-dev installed, building PIL in a virtualenv still reports "JPEG support not available". – Cerin Oct 24 '12 at 15:47
  • Thanks so much! This error was driving me mad. Latest version of PIL, reads the same jpeg from file perfectly well, but in no way from StringIO! Your recipe cured it at once. – Antony Hatchkins Nov 13 '12 at 10:09
  • After make that changes on pip installed libraries, it is necessary to restart the server? or something else? – Tony Sep 10 '13 at 13:26
2

Note, for anyone getting this error with a virtualenv on Ubuntu, this post is helpful.

Basically, Ubuntu installs shared objects to locations that pip doesn't know about. In this case, pip expects system libraries to be in /usr/lib, but Ubuntu puts them in /usr/lib/x86_64-linux-gnu, or some other architexture-dependent location.

The short-term fix is to simply symlink the libraries into /usr/lib:

sudo ln -s /usr/lib/x86_64-linux-gnu/libfreetype.so /usr/lib/
sudo ln -s /usr/lib/x86_64-linux-gnu/libz.so /usr/lib/
sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib/

This fixed the error for me on Ubuntu 12.04.

Cerin
  • 60,957
  • 96
  • 316
  • 522
1

I had a similar problem on Ubuntu 11.04. Apparently in Ubuntu 11, it seems that you need libjpeg8-dev, rather than libjpeg or libjpeg62. Thankyou to Guillaumes post http://gpiot.com/ubuntu-9-10-install-pil-in-virtualenv/

Neil_Alex
  • 11
  • 1
1

just do
sudo easy_install PIL

this will install PIL specific to your os. please make sure that the egg file generated in /usr/local/lib/python2.6/dist-packages/ is having egg information (because when i did the same, egg file was not correct). If not then just rename the PIL-build-specific-name to PIL and add a PIL.pth file in the dist-packages folder. write PIL in the PIL.pth file and you are done

Rahul
  • 568
  • 2
  • 6
  • 16
0

'python -v' then 'import _imaging' is useful for figuring out where _imaging.so is loaded from. If you reinstall PIL without cleaning out the PIL dir in site-packages you may still be running with an old _imaging.so under the Python package dir. PIL's selftest.py will pass, because you've got a new _imaging.so in your build dir. And make sure you edit JPEG_ROOT in setup.py to pick up the correct header and .so directories at build time.

osullivj
  • 341
  • 1
  • 5
0

I had this problem although on Linux not Mac, so might not be able to give too specific info. However you might need libjpeg-devel too (if there is a correspondent for Mac).

Also make sure to purge your current PIL installation completely from the system. And after you are sure libjpeg is installed properly then reinstall PIL with build_ext -i. Then run PIL's selftest.py to check if it gives the JPEG error.

Botond Béres
  • 16,057
  • 2
  • 37
  • 50
0

I had a similar problem and i had cleared it with referring below link:

http://osqa.sjsoft.com/questions/96/why-is-there-no-jpegpng-decoder-when-i-pip-install-pil-on-ubuntu

Vijesh Venugopal
  • 1,621
  • 15
  • 26
0

Django is trying to import PIL from a folder called PIL, but PIL installs itself in a folder called e.g. PIL-1.1.7-py2.6-macosx-10.6-universal.egg, so the import fails - which Django (or PIL ?) seem to interpret as corrupt image.

A simple symlink

host:~ user$ cd /Library/Python/2.6/site-packages
host:site-packages user$ ln -vis PIL-1.1.7-py2.6-macosx-10.6-universal.egg PIL
create symbolic link `PIL' to `PIL-1.1.7-py2.6-macosx-10.6-universal.egg'

has fixed this for me on a Mac OSX 10.6.x MBP. On Linux machines, the folder might instead be called dist-packages and be underneath /usr/lib/python/ or so, but the idea is the same.

You can often find the folder where python modules are usually installed to as described here.

Community
  • 1
  • 1
ssc
  • 9,528
  • 10
  • 64
  • 94