Pillow
Pillow copied to clipboard
Unknown file format error on some `woff2` fonts on `ImageFont.truetype()`
What did you do?
I'm trying to convert woff and woff2 fonts to ImageFont
What did you expect to happen?
Return a FreeTypeFont object
What actually happened?
It raises OSError Issue, terminates program.
What are your OS, Python and Pillow versions?
ProductName: macOS
ProductVersion: 12.4
BuildVersion: 21F79
Python 3.9.13 (main, May 24 2022, 21:28:31)
[Clang 13.1.6 (clang-1316.0.21.2)] on darwin
Pillow==9.2.0
Here are some sample code represents my issue:
import requests
import os
from PIL import ImageFont
r = requests.get("https://cdn.jsdelivr.net/korean-webfonts/1/orgs/othrs/kywa/Youth/Youth.woff2", stream=True)
with open("Youth.woff2", "wb") as f:
f.write(r.content)
print("Trying to get Font ...\t", end="...")
try:
font = ImageFont.truetype("Youth.woff2")
print("Success")
os.remove("Youth.woff2")
except Exception as e:
print("Failed " + str(e))
os.remove("Youth.woff2")
raise e
Traceback (most recent call last):
File "/Users/mirusu400/kwoss/pillow-woff-issue/test2.py", line 18, in <module>
raise e
File "/Users/mirusu400/kwoss/pillow-woff-issue/test2.py", line 12, in <module>
font = ImageFont.truetype("Youth.woff2")
File "/usr/local/lib/python3.9/site-packages/PIL/ImageFont.py", line 959, in truetype
return freetype(font)
File "/usr/local/lib/python3.9/site-packages/PIL/ImageFont.py", line 956, in freetype
return FreeTypeFont(font, size, index, encoding, layout_engine)
File "/usr/local/lib/python3.9/site-packages/PIL/ImageFont.py", line 247, in __init__
self.font = core.getfont(
OSError: unknown file format
I don't know detailed reason this happen, So I created a repo about this issue. https://github.com/mirusu400/pillow-woff-issue Here are some samples about having ImageFont issue.
Thanks for your detailed example.
Looking at the output you've provided at https://github.com/mirusu400/pillow-woff-issue#result, it would seem that this is not failing for any of the woff files, only woff2. The font that you have trouble after fetching from a URL is also woff2.
When I try and run your repository code on my machine, all of the fonts pass.
So let me ask this - what version of freetype2 are you using? Support for woff2 was only added in freetype2 2.10.2
>>> from PIL import features
>>> features.version_module("freetype2")
'2.12.1'
Some anon posts lots of fonts which have errors: https://anonfiles.com/sc07oe67y1/fonts_error_zip
This all fonts have same OSErrors.
Also here is my freetype2 version:
Python 3.9.13 (main, May 24 2022, 21:28:31)
[Clang 13.1.6 (clang-1316.0.21.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import features
>>> features.version_module("freetype2")
'2.12.1'
Edit: There are mixed woff and woff2 files, but some anon renamed it to woff
Edit2: It seems all of woff files are woff2 files. Sorry for misinformation.
Did you install Pillow from the wheel, or did you build it from source?
I found https://lists.gnu.org/archive/html/freetype-commit/2019-08/msg00250.html
Brotli is required for decompressing WOFF2 font directory streams.
And brotli is currently disabled in our macOS wheel builds.
I installed it from pip(wheel).
Also, I tried at Windows environment, still not works.
Here is my info about window environment:
Windows 10 21H2 19044.1889
Pillow==9.2.0
Python 3.9.12 (tags/v3.9.12:b28265d, Mar 23 2022, 23:52:46) [MSC v.1929 64 bit (AMD64)] on win32
>>> from PIL import features
>>> features.version_module("freetype2")
'2.12.1'
Same problem and output with different Linux distributions in Docker:
FROM python:3.11-rc-bullseye
RUN set -ex \
&& apt-get update \
&& apt-get install -y -V libpng16-16 libpng-dev libbrotli1 libbrotli-dev libfreetype6 libfreetype-dev \
&& python -m pip install --upgrade pip \
&& pip install --no-cache-dir Pillow
FROM python:3.11-rc-buster
RUN set -ex \
&& apt-get update \
&& apt-get install -y -V libpng16-16 libpng-dev libbrotli1 libbrotli-dev libfreetype6 libfreetype6-dev \
&& python -m pip install --upgrade pip \
&& pip install --no-cache-dir Pillow
FROM python:3.11-rc-alpine
RUN set -ex \
&& apk update \
&& apk add --upgrade brotli brotli-dev freetype freetype-dev \
&& python -m pip install --upgrade pip \
&& pip install --no-cache-dir Pillow
https://github.com/python-pillow/pillow-wheels/pull/320 has been created to add brotli to our macOS and Linux wheels.
I've added brotli to the #6562 changes for Windows wheels.
Pillow 9.3.0 has now been released, so brotli should be included in the latest wheels now.
Hi @radarhere !
I got same error when build docker to deploy on aws layer lambda
When run python test.py with FONT_NAME="Karla-Regular.woff2" (sample.ttf work fine)
And I try to change dockerfile with from
RUN pip3 install --no-cache-dir Pillow
to
// .whl file from https://github.com/python-pillow/pillow-wheels/releases/tag/10.0.0
COPY build/Pillow-10.0.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl .
RUN pip3 install Pillow-10.0.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
It's still return error
bash-4.2# python test.py
Traceback (most recent call last):
File "/var/task/test.py", line 6, in <module>
font = ImageFont.truetype(
File "/var/lang/lib/python3.10/site-packages/PIL/ImageFont.py", line 791, in truetype
return freetype(font)
File "/var/lang/lib/python3.10/site-packages/PIL/ImageFont.py", line 788, in freetype
return FreeTypeFont(font, size, index, encoding, layout_engine)
File "/var/lang/lib/python3.10/site-packages/PIL/ImageFont.py", line 226, in __init__
self.font = core.getfont(
OSError: unknown file format
FONT_NAME="Karla-Regular.woff2"
Could you provide a link to the file or the file itself?
I've created https://github.com/python-pillow/pillow-wheels/pull/405 to resolve this.
In the meantime, I put together a wheel with GitHub Actions - this should work for you. Pillow-10.0.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.zip
Pillow 10.0.1 has now been released, so that wheel should now also be available through pip.