python-3.x 导入错误:alpine-docker中未安装_imagingft C模块

r1zhe5dt  于 2023-02-26  发布在  Python
关注(0)|答案(2)|浏览(201)

我尝试导入和使用python pillow库,如下所示:

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

fjnneemd

fjnneemd1#

我们需要安装编译器(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
gg58donl

gg58donl2#

唉,不要用这些破Docker镜像了,一遍又一遍,用普通的alpine:3.7镜像,用apk安装需要的包就行了,Pillow for Python 3由包py3-pillow提供,Python 3由包python3提供。

相关问题