tags:

views:

670

answers:

3

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.

+4  A: 

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
I did install libjpeg first.
BryanWheelock
A: 

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.

Béres Botond
+1  A: 

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