2

Using ubuntu 13.10, python 2.7.5:

>>> import _imaging, Image
>>> from PIL import Image, ImageDraw, ImageFilter, ImageFont
>>> im = Image.new('RGB', (300,300), 'white')
>>> draw = ImageDraw.Draw(im)
>>> font = ImageFont.truetype('arial.ttf', 14)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/PIL/ImageFont.py", line 218, in truetype
    return FreeTypeFont(filename, size, index, encoding)
  File "/usr/local/lib/python2.7/dist-packages/PIL/ImageFont.py", line 134, in __init__
    self.font = core.getfont(file, size, index, encoding)
  File "/usr/local/lib/python2.7/dist-packages/PIL/ImageFont.py", line 34, in __getattr__
    raise ImportError("The _imagingft C module is not installed")
**ImportError: The _imagingft C module is not installed**
>>>

So why does this happen? And how can I fix it? I followed the following instructions which I found at the bottom of this thread:

pip uninstall PIL
apt-get install libjpeg-dev
apt-get install libfreetype6-dev
apt-get install zlib1g-dev
apt-get install libpng12-dev
pip install PIL --upgrade

But while upgrading PIL, look what I saw:

--------------------------------------------------------------------
PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version       1.1.7
platform      linux2 2.7.5+ (default, Sep 19 2013, 13:48:49)
              [GCC 4.8.1]
--------------------------------------------------------------------
*** TKINTER support not available
*** JPEG support not available
*** ZLIB (PNG/ZIP) support not available
*** FREETYPE2 support not available
*** LITTLECMS support not available
--------------------------------------------------------------------

Is it normal for support for all these items not to be available? how do I fix that?

Community
  • 1
  • 1
Saqib Ali
  • 11,931
  • 41
  • 133
  • 272
  • It may be worth looking at AskUbuntu rather than here. For example, [this question](http://askubuntu.com/questions/156484/how-do-i-install-python-imaging-library-pil) is a similar, but Ubuntu-specific question (although it's also a bit out of date, since it seems to still be suggesting PIL instead of Pillow). – abarnert Dec 24 '13 at 01:43
  • Meanwhile, if Ubuntu has a Pillow package (whether `python-imaging` has been updated, or it's a new package named something like `python-pillow`), you probably want to use that instead of `pip` (assuming you're installing into the default Ubuntu Python, that is). – abarnert Dec 24 '13 at 01:43
  • And Ubuntu _does_ have a Pillow package: in your version of Ubuntu, `python-imaging` version 1.1.7+2.0.0-1ubuntu1 is Pillow 2.0.0. (The weird version number is to fill the prerequisites for various other packages that think they want PIL 1.1.7.) So, just do that. – abarnert Dec 24 '13 at 03:25

3 Answers3

9

PIL is essentially dead, the fork Pillow is really what you should be using. You need to import it via

from PIL import Image, ...

It maintains all of the functionality of PIL, and adds a lot more, including Python 3 compatibility. Additionally, it's under active development, unlike PIL, so any bugs you find actually have a chance of getting fixed - same with feature requests.

You can install it (after uninstalling PIL) by running

pip install Pillow

I believe there's a package for Ubuntu, but my VM is giving me headaches at the moment and I can't check...

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • +1. Also, `Pillow` documents that all you need of JPEG support is `libjpeg` version 6b, 8, or 9; `PIL` never clearly documented what you needed, and it was a matter of "install everything that seems relevant and cross your fingers"… – abarnert Dec 24 '13 at 01:40
  • Thanks! I installed Pillow and it all seemingly worked.... EXCEPT: Now the Captcha appears, but it is basically unreadable: http://predictstat.com/accounts/register – Saqib Ali Dec 24 '13 at 01:53
  • when are you seeing a Captcha? – MattDMo Dec 24 '13 at 03:11
  • 1
    When I go to that page, it tells me that I've reached an invalid page, and to make sure I'm "singed in properly". I don't want to be set on fire. :) – abarnert Dec 24 '13 at 03:16
  • Anyway, if you're trying to use this code to generate a home-made Captcha equivalent, that's a really bad idea. There are all kinds of edge cases, problems with colorblind people, etc. you need to deal with—and, once you do, someone cracks your algorithm every 2 months and you need to detect that, figure out how, and rewrite your code to deal with that. Captcha and their competitors do that for a living, for millions of websites; you doing it as a quick hack, for one site, are not going to accomplish anything. – abarnert Dec 24 '13 at 03:17
  • Or search for alternatives; I turned up [this blog post](http://econsultancy.com/us/blog/63144-six-alternatives-to-using-the-dreaded-captcha-images) that has some interesting ideas for doing something _different_ from Captcha instead of trying (badly) to clone it. – abarnert Dec 24 '13 at 03:19
2

According to this question at AskUbuntu:

It turns out that the APT installations put the libraries under /usr/lib/x86_64-linux-gnu and PIL will search for them in /usr/lib/. So you have to create symlinks for PIL to see them.

In other words, PIL apparently doesn't understand modern Ubuntu, or in fact most 64-bit linuxes.


What you probably want to do is:

  • Switch to Pillow as MattDMo suggests,
  • Use an Ubuntu package for PIL (python-imaging) instead of trying to install via pip, or
  • Do both—use an Ubuntu package for Pillow, if there is one.

Checking on packages.ubuntu.com, the python-imaging package is in fact Pillow 2.0.0 on Saucy (13.10). In fact, it's Pillow on anything Raring or later; it's only people still using 12.x or earlier versions who are stuck with PIL. So, if you just do this:

$ sudo pip uninstall PIL
$ sudo apt-get install python-imaging

… that will get you Pillow, and pull in any dependencies it needs, all in one step.


If you don't want to do any of those, you should first reconsider that decision because it's probably a mistake, and then look at the workarounds suggested on that answer. In particular, symlinking the libs you installed into your /usr/lib directory is probably what you need. So, for example:

$ sudo pip uninstall PIL
$ sudo apt-get install libjpeg-dev
$ sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib/libjpeg.so
$ # repeat for the various other libraries, but JPEG is the one you asked about
$ sudo pip install PIL

Alternatively, you could patch PIL itself, as some of the answers on the linked question show. In particular, add these two lines:

add_directory(library_dirs, "/usr/lib/x86_64-linux-gnu")
add_directory(library_dirs, "/lib/x86_64-linux-gnu")

… after the lines that add /usr/local/lib and /usr/lib.

However, if you're going to use a patched PIL, why not use Pillow, which has already solved this problem and many others?

Community
  • 1
  • 1
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • so it looks like the Ubuntu package `python-imaging` is still PIL. Oh well, there's always pip :) – MattDMo Dec 24 '13 at 03:09
  • @MattDMo: I don't actually have Ubuntu, but I've seen at least two other distros leave `python27-imaging` as PIL but add a new `python27-pillow` for Pillow. (Of course one of them had a nice bug where `python27-pillow` neither obsoleted `python27-imaging` nor supplied the abstract requisite… but that would be an easy bug to fix, compared to a "please add a package for Pillow" feature request.) – abarnert Dec 24 '13 at 03:13
  • @MattDMo: Actually, looking [here](http://packages.ubuntu.com/raring/python-imaging), it looks like Ubuntu's `python-imaging` is Pillow in raring and later, and only real PIL in quantal and earlier. Since the OP is using 13.10, which is saucy, he should be fine. – abarnert Dec 24 '13 at 03:21
  • it claims that, but there's no `Image.PILLOW_VERSION` like there is in my copy of Pillow 2.2.2 on OS X. Do you know offhand of any functions that were added in Pillow that aren't in PIL? – MattDMo Dec 24 '13 at 03:40
  • @MattDMo: There aren't too many (remember, up to 2.0 the goal was 100% feature compatibility with PIL), and there's no nice "what's new in 2.3" documentation, so the only way to find out is to go through the [commit log](https://github.com/python-imaging/Pillow/commits/master), or scrub through the [reference docs](http://pillow.readthedocs.org/en/latest/reference/index.html) looking for "New in version". Anyway, as I said, I don't have Ubuntu to check it out, but if you look at the file like from the raring `python-imaging` package, it includes the Pillow-2.0.0.egg-info directory. – abarnert Dec 24 '13 at 18:56
0

Additional notes for Mac OS X

On Mac OS X you will usually install additional software such as libjpeg or freetype with the "fink" tool, and then it ends up in "/sw". If you have installed the libraries elsewhere, you may have to tweak the "setup.py" file before building.


Additional notes for Windows

On Windows, you need to tweak the ROOT settings in the "setup.py" file, to make it find the external libraries. See comments in the file for details.

Make sure to build PIL and the external libraries with the same runtime linking options as was used for the Python interpreter (usually /MD, under Visual Studio).

Note that most Python distributions for Windows include libraries compiled for Microsoft Visual Studio. You can get the free Express edition of Visual Studio from:

http://www.microsoft.com/Express/

To build extensions using other tool chains, see the "Using non-Microsoft compilers on Windows" section in the distutils handbook:

http://www.python.org/doc/current/inst/non-ms-compilers.html

For additional information on how to build extensions using the popular MinGW compiler, see:

http://mingw.org (compiler)
http://sebsauvage.net/python/mingw.html (build instructions)
http://sourceforge.net/projects/gnuwin32 (prebuilt libraries)
J A S K I E R
  • 1,976
  • 3
  • 24
  • 42