6

I am running Django 1.4 and PIL 1.1.7 in a virtualenv managed with pip. Whenever I upload a JPEG file via my admin interface, I get the following error: Upload a valid image. The file you uploaded was either not an image or a corrupted image.

As many Ubuntu users have reported, on installation, PIL erronously looked in /usr/lib/ for libjpeg, while its true location was in /usr/lib/i386-linux-gnu/. That's taken care of; I followed the answers in these posts:

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

Why can't I upload jpg files to my Django app via admin/?

Now the final output of installation looks as follows:


PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version       1.1.7
platform      linux2 2.7.3 (default, Apr 20 2012, 22:44:07)
              [GCC 4.6.3]
--------------------------------------------------------------------
--- TKINTER support available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
--- FREETYPE2 support available
*** LITTLECMS support not available
--------------------------------------------------------------------
To add a missing option, make sure you have the required
library, and set the corresponding ROOT variable in the
setup.py script.

To check the build, run the selftest.py script.
changing mode of build/scripts-2.7/pilfile.py from 644 to 755
changing mode of build/scripts-2.7/pilfont.py from 644 to 755
changing mode of build/scripts-2.7/pilconvert.py from 644 to 755
changing mode of build/scripts-2.7/pilprint.py from 644 to 755
changing mode of build/scripts-2.7/pildriver.py from 644 to 755

changing mode of /usr/local/bin/pilfile.py to 755
changing mode of /usr/local/bin/pilfont.py to 755
changing mode of /usr/local/bin/pilconvert.py to 755
changing mode of /usr/local/bin/pilprint.py to 755
changing mode of /usr/local/bin/pildriver.py to 755
Successfully installed PIL

However, Django still doesn't allow me to upload anything but BMP files, which is not acceptable for this project. Unlike the user in the second link, I'm not using Apache, so my problem is probably not related. My suspicion is that Django is still using an old installation of PIL. The problem is that I don't know where it could be getting it from. The files in ~/.virtualenvs/project/local/lib/python2.7/site-packages seem up-to-date to me. Any ideas?

EDIT 1: Also note that I have tried logging out and logging back in the admin, as well as installing pillow, as in this post: Uploading a JPEG image via Django displays error

Community
  • 1
  • 1
novembrine
  • 81
  • 7
  • Did you try installing [Pillow](http://pypi.python.org/pypi/Pillow) instead of PIL? Check your sys.path – jterrace Jul 27 '12 at 16:47
  • @jterrace: Yes. And that installation also told me JPEG support was available. – novembrine Jul 27 '12 at 16:55
  • @nicowernli: Sure. I will add it in an edit. – novembrine Jul 27 '12 at 16:55
  • 2
    Run ``manage.py shell`` and try something like ``import Image; im = Image.open("file.jpg"); im.show()`` to see if it works – jterrace Jul 27 '12 at 17:06
  • Also, I should let you know that I'm not the only one working on this project. The other user, operating on a Mac OS X system, has had no problem with PIL and Django getting along. – novembrine Jul 27 '12 at 17:08
  • @jterrace is right, maybe when you re install PIL you didn´t do it on your virtualenv, or something like that. – nicowernli Jul 27 '12 at 17:09
  • @jterrace: Aha! It didn't work. Here's the output: Traceback (most recent call last): File "", line 1, in File "/home/sionydus/.virtualenvs/django_are_news/local/lib/python2.7/site-packages/PIL/Image.py", line 1952, in open fp = __builtin__.open(fp, "rb") IOError: [Errno 2] No such file or directory: 'file.jpg' – novembrine Jul 27 '12 at 17:10
  • @nicowernli: But, I did... I've re-installed many, many times and made sure it was on the virtualenv. – novembrine Jul 27 '12 at 17:11
  • @nicowernli file.jpg was an example filename - you need to give it a path to a real file – jterrace Jul 27 '12 at 17:13
  • @jterrace: Haha boy do I feel stupid. I got too excited, I guess. :) Verifying that I am in the correct virtualenv, output shows no error when I give it a real JPEG. – novembrine Jul 27 '12 at 17:19
  • @novembrine that verifies that your PIL installation is working with JPEG then – jterrace Jul 27 '12 at 17:31
  • @jterrace: That's exactly why this problem is so strange to me. Just to be sure, `im.show()` isn't supposed to display the JPEG file, is it? Nothing happens when I execute that. – novembrine Jul 27 '12 at 17:35
  • I am going to take down the model now. I don't believe the way my model is constructed could be part of the problem. – novembrine Jul 27 '12 at 17:48
  • Apache is not a database :) and I am sure it is not a database problem, because you are trying to save images in your filesystem. – Aidas Bendoraitis Jul 27 '12 at 18:00
  • @AidasBendoraitis You're right. I fixed that comment. And I never really had that suspicion, but I wanted to eliminate the possibility. – novembrine Jul 27 '12 at 18:07
  • @novembrine yes, im.show should show the image, but JpegImagePlugin verifies that it's working at least – jterrace Jul 27 '12 at 18:38
  • 2
    It might be that you have more than one PIL's in your system. try this `>>> import Image` `>>> Image.__file__` `'/usr/lib/python2.6/site-packages/PIL/Image.pyc' ` and see which one you're importing. – Meitham Jul 28 '12 at 18:55
  • Thank you all for your helpful comments. I apologize, but you wrongly assumed my competence in using pip correctly. See my answer below for the true fix. – novembrine Jul 30 '12 at 16:00

1 Answers1

2

It turns out that this whole thing is my fault due to a misunderstanding of how pip actually works. By habit, I associate any and all installations with superuser privileges, which was not only not necessary in this case, but a recipe for confusion.

The first time I installed PIL, I did not prefix it with sudo, but I did each time after. Thus, building from Meitham's advice, I checked to see where the import was coming from. It came from the correct place, but it did not have the extensions I desired, despite the post-installation output saying I did. Long story short, I removed the directory from my site-packages, then pip freeze continued to tell me I did not have PIL but sudo pip install told me I did.

Lesson learned: virtualenv is based in the user's directory, on the user's privileges. Think twice before combining sudo and pip in the same command.

novembrine
  • 81
  • 7