tags:

views:

427

answers:

3

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

models.py

class ImageMain(models.Model):
"""This is the Main Image of the product"""
    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.

The jpg I'm trying to upload can be viewed with os X Preview so it would seem to be valid.

It seems the problem is Python Imaging Library doesn't recognize it as an image. Why would that be happening with a valid image?

PIL is installed, verified via the Django shell.

+4  A: 

According to Django's source code. Those three lines are responsible for verifying images:

from PIL import Image
trial_image = Image.open(file)
trial_image.verify()

The image type could be unsupported by PIL. Check the list of supported formats here

Nadia Alramli
The image is a jpeg and according to the list is installed.
BryanWheelock
Try to open the image in PIL and see what you get. The image might be indeed corrupted even if you can view it in os X Preview
Nadia Alramli
IOError: decoder jpeg not availableI've got PIL release 1.1.6I'm going to try to install libjpeg.
BryanWheelock
A: 

I looked into the Django source, in django/forms/fields.py, in the ImageField class. Django actually does use PIL to determine when an image is valid.

Christian Oudard
+2  A: 

Did you try uploading image format like gif or png? It might be that your PIL was not built with the jpeg lib properly. I had a similar issue with Django on Ubuntu. If you have ever seen the error message decoder jpeg not available, check this link. Relevant line from the link:

$ cd libImaging
$ ./configure --with-jpeg=/somelib/lib --with-zlib=/somelib/lib
Thierry Lam