python PIL ImportError: The _imagingft C module is not installed

# import _imaging : No module named _imaging
# 需要先安裝jpeg庫
wget http://www.ijg.org/files/jpegsrc.v7.tar.gz
tar -zxvf jpegsrc.v7.tar.gz
cd jpeg-7
CC="gcc -arch x86_64"
./configure --enable-shared --enable-static
make
make install
# 然後再安裝PIL庫
wget http://effbot.org/downloads/Imaging-1.1.6.tar.gz
tar -zxvf Imaging-1.1.6.tar.gz
cd Imaging-1.1.6
rm -rf build
# 設置JPEG庫的路徑
vim setup.py :
    JPEG_ROOT = libinclude("/usr/local")
python2.5 setup.py build
python2.5 setup.py install
# 把JPEG加入到系統庫路徑
echo '/usr/local/lib' >> /etc/ld.so.conf
ldconfig
# OK,完成

上述操作完成後就不會再報找不到 _imaging 的錯誤了。

別高興太高,如果用到了freetype2,還會報_imagingft找不到。解決辦法類似,先安裝freetype2,在安裝PIL裏指定freetype2路徑,將freetype2加到系統庫路徑中即可。




Building PIL with Freetype2 on Snow Leopard

7 july 2011
13:25

I had some trouble getting the venerable Python Imaging Library (PIL) compiled with the Freetype feature on Mac OS X 10.6. Doing a simple pip install or easy_install will build it with just the basic options. But if you need to generate Captchas and the like (with django-simple-captcha, for example) you'll need the Freetype option, too.

In fact, OS X comes with Freetype2 installed (as part of the X11 package), just not in a location the PIL build script knows about. That is simple to fix, however:

$ cd ~/Downloads
$ curl -LO http://effbot.org/downloads/Imaging-1.1.7.tar.gz
$ tar -zxf Imaging-1.1.7.tar.gz
$ cd Imaging-1.1.7
$ echo 'FREETYPE_ROOT = "/usr/X11/lib", "/usr/X11/include"' > setup_site.py
$ python setup.py build

The setup summary printed out at the end of the build should now say “FREETYPE2 support available”, which would mean this procedure worked as designed.





CODE: SELECT ALL
#!usr/bin/env python

from PIL import Image, ImageDraw, ImageFont

scribble = "Hello World"

img = Image.new("RGB", (200,200))
canvas = ImageDraw.Draw(img)
the_font = ImageFont.load_default() # truetype("Arial.ttf", 24)
canvas.text((0,0), scribble, font=the_font)
img.save(open("weather.png", "wb"), "PNG")

Hi, the code above works just fine, but when I replace load_default() with truetype("Arial.ttf", 24) I get the error...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/dist-packages/PIL/ImageFont.py", line 218, in truetype
return FreeTypeFont(filename, size, index, encoding)
File "/usr/lib/python2.6/dist-packages/PIL/ImageFont.py", line 134, in __init__
self.font = core.getfont(file, size, index, encoding)
IOError: cannot open resource

...so it looks like it has a problem with opening the resource, which I'm thinking is something do with finding Arial.ttf. I'm not really knowledgeable on how linux handles fonts. I did look in the various fonts folders and found there is Ariel.ttf so I'm kind of stuck about how to make things play nice. 

Any ideas how I can get python to see my fonts? Is there any python code I could run to see which fonts it can see in Linux (I'm running Linux Mint 9)?

Thanks in advance.
User avatar
leke
New Python User
New Python User
 
Posts: 31
Joined: Sun Apr 11, 2010 10:40 am
Location: Oulu, Finland

Re: Python, Linux and the PIL

Postby mariostg » Sun Dec 05, 2010 6:55 pm

Specify the full path
CODE: SELECT ALL
the_font = ImageFont.truetype("/usr/share/fonts/TTF/arial.ttf", 24)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章