ImportError: The _imagingft C module is not installed in alpine-docker

Question:

I am attempting to import and use the python pillow library like this:

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')

However, I get the following error:

/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

I am aware that similar questions have been asked on StackOverflow before. However, all of the answers seem to deal with execution environments I am not targeting. I am running this in the python:3.7-rc-alpine3.7 docker image and the existing answers do not work. Here are the pip and apk packages I have installed: http://dpaste.com/3EBW3A1

Asked By: DMCoding

||

Answers:

We need to install compiler (g++), TrueType font rendering library (freetype-dev) and accelerated baseline JPEG compression and decompression library (jpeg-dev) to compile pillow on Alpine platform.

The part of Dockerfile is:

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

Sigh, don’t use these broken Docker images. It’s over and over again. Just use plain alpine:3.7 image and install needed packages using apk. Pillow for Python 3 is provided by package py3-pillow, Python 3 by package python3.

Answered By: Jakub Jirutka