ImportError:_imagingft C模块未安装在alpine-docker中

时间:2018-03-26 19:27:52

标签: python python-3.x docker pillow alpine

我正在尝试导入并使用这样的python枕头库:

from urllib.request import urlopen
import PIL
from PIL import Image, ImageFont, ImageDraw
import re
image = Image.open(urlopen("http://randomurl.com/randomimage.jpg"))
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("sans-serif.ttf", 16)
draw.text((0, 0),"This is the future liberals want",(255,255,255),font=font)
image.save('sample-out.jpg')

但是,我收到以下错误:

/opt/bot/app # python cli.py 
Retrieving column Image ID on worksheet History
Traceback (most recent call last):
  File "cli.py", line 38, in <module>
    font = ImageFont.truetype("sans-serif.ttf", 16)
  File "/usr/local/lib/python3.7/site-packages/PIL/ImageFont.py", line 260, in truetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "/usr/local/lib/python3.7/site-packages/PIL/ImageFont.py", line 135, in __init__
    if core.HAVE_RAQM:
  File "/usr/local/lib/python3.7/site-packages/PIL/ImageFont.py", line 39, in __getattr__
    raise ImportError("The _imagingft C module is not installed")
ImportError: The _imagingft C module is not installed

我知道之前在StackOverflow上已经提出了类似的问题。但是,所有答案似乎都涉及我没有针对的执行环境。我在python:3.7-rc-alpine3.7泊坞窗图片中运行此功能,现有的答案无效。以下是我安装的pip和apk软件包:http://dpaste.com/3EBW3A1

2 个答案:

答案 0 :(得分:2)

我们需要安装编译器(g++),TrueType字体渲染库(freetype-dev)和加速基线JPEG压缩和解压缩库(jpeg-dev)来编译pillow高山平台。

Dockerfile的部分是:

FROM python:3.7-rc-alpine3.7
RUN apk add --no-cache g++ freetype-dev jpeg-dev
RUN pip install pillow

答案 1 :(得分:0)

叹息,不要使用这些破碎的Docker镜像。它一遍又一遍。只需使用普通alpine:3.7图片,然后使用apk安装所需的软件包即可。 Pillow for Python 3由包py3-pillow提供,Python 3由包python3提供。