Python: _imagingft C模块未安装

81 投票
17 回答
94369 浏览
提问于 2025-04-16 06:02

我在网上试过很多解决办法,但都没有用。

>>> import _imaging
>>> _imaging.__file__
'C:\\python26\\lib\\site-packages\\PIL\\_imaging.pyd'
>>>

所以系统能找到_imaging,但还是不能使用truetype字体。

from PIL import Image, ImageDraw, ImageFilter, ImageFont


im = Image.new('RGB', (300,300), 'white')
draw = ImageDraw.Draw(im)
font = ImageFont.truetype('arial.ttf', 14)
draw.text((100,100), 'test text', font = font)

出现了这个错误:

ImportError: The _imagingft C module is not installed

File "D:\Python26\Lib\site-packages\PIL\ImageFont.py", line 34, in __getattr__
  raise ImportError("The _imagingft C module is not installed")

17 个回答

55

以下内容在我使用的Ubuntu 14.04.1 64位系统上有效:

sudo apt-get install libfreetype6-dev

然后,在虚拟环境中:

pip uninstall pillow
pip install --no-cache-dir pillow
86

在Ubuntu系统上,你需要先安装libfreetype-dev这个软件包,才能编译PIL。

也就是说:

$ sudo apt-get install libfreetype6-dev
$ sudo -s
\# pip uninstall pil
\# pip install --no-cache-dir pil

另外,注意!如果你用sudo来运行pip install,通常会把软件包安装到大多数Ubuntu版本的/usr/local/lib目录下。你可以考虑在一个用户自己拥有的路径下,使用虚拟环境(virtualenv或venv)来安装PIL。

你也可以考虑安装pillow,代替PIL,我认为它们的接口是兼容的:https://python-pillow.org。请注意,Pillow同样需要libfreetype-dev,如果在最开始安装时没有这个软件包,你可能需要按照相同的卸载/安装步骤来处理。

57

你安装的PIL(Python图像库)是没有包含libfreetype这个库的。

你可以在这里找到包含libfreetype的PIL预编译安装包(还有很多其他预编译的Python C模块):

http://www.lfd.uci.edu/~gohlke/pythonlibs/

撰写回答