我尝试了很多在网上发布的解决方案,它们都不起作用。
>>> 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")
最佳答案
在 Ubuntu 上,您需要在编译 PIL 之前安装 libfreetype-dev。
即
$ sudo apt-get install libfreetype6-dev
$ sudo -s
\# pip uninstall pil
\# pip install --no-cache-dir pil
PS!在大多数 Ubuntu 版本上,以 sudo 运行 pip install 通常会将软件包安装到/usr/local/lib。您可以考虑将 Pil 安装在用户拥有的路径中的虚拟环境(virtualenv 或 venv)中。
您也可以考虑安装枕头而不是 pil,我认为它与 API 兼容:https://python-pillow.org .请注意,Pillow 还需要 libfreetype-dev,如果在初始安装期间没有 libfreetype-dev,您可能需要执行相同的卸载/安装步骤。
关于Python:未安装 _imagingft C 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4011705/