python PIL ImportError: The _imagingft C module is not installed

本文详细介绍了如何在Linux环境下安装Python Imaging Library (PIL),并解决了安装过程中遇到的_imaging和_imagingft模块缺失的问题。同时,还提供了如何配置Freetype2以使PIL能够使用系统字体的方法。
摘要由CSDN通过智能技术生成
# 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
Top

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)


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值