Installing PIL (the python image library) on my Mac is non-trivial. I get this error when I try to read or write a jpeg image:
IOError: encoder jpeg not available
and this error when I try to use any fonts:
ImportError: The _imagingft C module is not installed
I understand that Pillow is a much friendlier version of PIL, but even it does not help here, as its documentation simply states “Once you have installed the prerequisites” – with no further explanation of how to do that. Also, I have never used homebrew, and am not sure how it works with virtualenv, so I’d prefer not to use it.
Here are the steps I have followed, which solve both of these problems. They took a long time to discover!
- Uninstall any existing PIL you may have. I’m afraid this is easier said than done. Fortunately I had installed PIL in a
virtualenv
, so I could just change to a new one and go from there. If you installed it using Pillow, you should be able to justpip uninstall Pillow
, but I have not tried it. - Install the jpeg library. The way to do this is partly given by this stackoverflow post, though it unfortunately misses the last step, which you can find here. To summarise:
- Download libjpeg from http://www.ijg.org/files/jpegsrc.v8c.tar.gz
- Unpack it (either using the Finder or something like
tar zxvf jpegsrc.v8c.tar.gz
) ./configure
make
sudo make install
cp -r ~/Downloads/jpeg-XX/ /usr/local/jpeg
- Install the freetype library. Get this from http://www.freetype.org/download.html, and follow the same procedure as for jpeg above (I didn’t do step 5 and it worked fine). Copy it into /usr/local/freetype. This time it will work.
- Install any other libraries you may want (e.g. see this list).
pip install Pillow
To show this works, I get this output from the final command:
-------------------------------------------------------------------- SETUP SUMMARY (Pillow 2.0.0 fork, originally based on PIL 1.1.7) -------------------------------------------------------------------- version 2.0.0 (Pillow) platform darwin 2.7.1 (r271:86832, Jul 31 2011, 19:30:53) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] -------------------------------------------------------------------- --- TKINTER support available *** JPEG support available --- ZLIB (PNG/ZIP) support available *** TIFF G3/G4 (experimental) support not available --- FREETYPE2 support available *** LITTLECMS support not available *** WEBP 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/pilconvert.py from 644 to 755 changing mode of build/scripts-2.7/pildriver.py from 644 to 755 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/pilprint.py from 644 to 755 warning: no previously-included files found matching '.hgignore' warning: no previously-included files found matching '.hgtags' warning: no previously-included files found matching 'BUILDME.bat' warning: no previously-included files found matching 'make-manifest.py' warning: no previously-included files found matching 'SHIP' warning: no previously-included files found matching 'SHIP.bat' warning: no files found matching '*.html' under directory 'docs' warning: no files found matching 'README' under directory 'docs' warning: no files found matching 'CHANGES' under directory 'docs' warning: no files found matching 'CONTENTS' under directory 'docs' changing mode of .../ENV-PIL/bin/pilconvert.py to 755 changing mode of .../ENV-PIL/bin/pildriver.py to 755 changing mode of .../ENV-PIL/bin/pilfile.py to 755 changing mode of .../ENV-PIL/bin/pilfont.py to 755 changing mode of .../ENV-PIL/bin/pilprint.py to 755 Successfully installed Pillow Cleaning up...
That helps me with running Django on my dev server, so that I can upload JPEGs properly. But if I use PIL to draw directly, I find the output looks very grainy (e.g. from the code below), and the colours are feathered. I have tried saving as both PNG and GIF.
img = Image.new('RGB', (500,500), (255, 255, 255)) draw = ImageDraw.Draw(img) draw.ellipse((x-r, y-r, x+r, y+r)) font = ImageFont.truetype("examplefont.tff", 25) w, h = draw.textsize(msg) draw.text((x-w/2, y-h/2), msg, (0, 0, 0), font=font) img.save(filename, fmt)
So I am happy to have Django working with images nicely, but I will stick to client-side drawing as much as possible.
Very helpful! Many thanks!