python - 导入错误 : The _imagingft C module is not installed in alpine-docker

标签 python python-3.x docker python-imaging-library alpine-linux

我正在尝试像这样导入和使用 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 docker 图像中运行它,现有的答案不起作用。这是我安装的 pip 和 apk 包:http://dpaste.com/3EBW3A1

最佳答案

我们需要安装编译器(g++)、TrueType字体渲染库(freetype-dev)和加速基线JPEG压缩解压库(jpeg-dev ) 在 Alpine 平台上编译 pillow

Dockerfile部分是:

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

关于python - 导入错误 : The _imagingft C module is not installed in alpine-docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49499184/

相关文章:

python - 脚本即使在异步运行时也执行得非常慢

windows - 在 server core 2019ltsc 容器镜像中添加字体

docker - 如何对 Windows 应用程序进行 Docker 化

python - 根据属性值使用 lxml 对子元素进行排序

python - 为 Python 制作外部枚举 "public"?

python - 基于 for 循环将 True 或 False 添加到新列

python-3.x - 如何使用包含 numpy.ndarrays 的列/列对 pandas 数据框执行 StandardScaler?

docker - 在 kubernetes 内运行的 docker 容器内没有互联网连接,使用 weave 作为网络

python - 多处理: fork 的缺点?

python - 将 cython 函数传递给 cython 函数