3

I installed PIL using PIP. However, using PIL on Django, when trying to upload a .jpg file, I get the error:

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

I read on the Internet about this error and a solution was to delete the compiled files for the current OS, then use setup.py again.

However, as I installed PIL with PIP, I have no setup.py and no folder with files compiled for my particular OS. This is Mac OSX Lion.

Update: I did not have libjpeg installed on my computer. I have it now and I am trying to change the PIL configuration to point to the libjpeg library.

4 Answers4

10

PIL need to find some libraries like libjpeg and libz during installation.

We encountered the same problems on our server and we installed PIL system-wide using

aptitude install python-imaging

This is a quick fix and it works for us.

Also googling about this show two ways how to fix this problem using PIL.

The first one is to symlink libjpeg.so, libfreetype.so and libz.so from /usr/lib/x86_64-linux-gnu/ to /usr/lib

The second one is to use pip --no-install key to download the package and then alter the setup.py to put correct paths there

1. Call 'pip install -I pil --no-install' to download and unpack the PIL source into your build directory;
2. Get into your build directory and edit setup.py;
3. Find the line that says 'add_directory(library_dirs, "/usr/lib")' (line 214 here);
4. Add the line 'add_directory(library_dirs, "/usr/lib/x86_64-linux-gnu/")' afterwards;
5. Call 'pip install -I pil --no-download' to finish the installation.

If you have i386 arch use i386-linux-gnu instead of x86_64-linux-gnu

Igor
  • 3,129
  • 1
  • 21
  • 32
  • Thank you for the valuable comment. I would much rather install it through an automated installer such as aptitude than compile the libraries. However, what is an equivalent of aptitude for Mac OS? –  Dec 06 '11 at 16:00
  • Works on Ubuntu as well. – Greg Kramida Mar 10 '14 at 15:33
4

if you're using pip and virtualenv then there's no need to mess with system paths.

PIL installed via pip has trouble finding the relevant libraries on ubuntu 12.10 (and some earlier) on x86_64

to fix:

# commands for recent debian/ubuntu
sudo apt-get install libjpeg libjpeg-dev libfreetype6 libfreetype6-dev zlib1g-dev

for i in libjpeg.so libfreetype.so libz.so
    do ln -s /usr/lib/x86_64-linux-gnu/$i $VIRTUAL_ENV/lib/
done
pip uninstall pil
pip install pil
scytale
  • 12,346
  • 3
  • 32
  • 46
2

You get this error if PIL is compiled without jpeg support. I also got this when the destination directory was not writeable.

Once PIL has compiled, if you read

JPEG support not available

the library for handling JPEG files, or part of it, is missing. Fix this just installing the library (in my case it was libjpeg62-dev) and run pip again, maybe inside a virtualenv. If this is not enough, probably your system has some more quirk. Look at this post (ubuntuforums) for the fix.

Paolo
  • 20,112
  • 21
  • 72
  • 113
  • Yes, the problem was in fact lack of jpeg libraries. What is an efficient way to install them on Mac ? –  Dec 06 '11 at 16:02
  • 2
    @Vasco OK. Check this: [Installing PIL to use with Django on Mac OS X](http://stackoverflow.com/questions/5075620/installing-pil-to-use-with-django-on-mac-os-x). Next time be more accurate in your questions! – Paolo Dec 06 '11 at 16:12
1

My fix for this is to make sure you have packages libjpeg-dev and libpng-dev installed before doing the pip install of PIL.

sudo apt-get install libjpeg-dev libpng-dev

will probably do. Then pip gets PIL from source, compiles with jpeg and png support.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • Thank you for the valuable answer. However, what is an equivalent of apt-get for Mac OS X ? Or, in general, a way that I could get these libraries from an installer and not have to compile them? –  Dec 06 '11 at 16:02
  • Use `brew` (homebrew) or `port` (macports). – Calvin Cheng Apr 28 '12 at 04:05