Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unknown file format error on some woff2 fonts on ImageFont.truetype() #6554

Closed
mirusu400 opened this issue Sep 1, 2022 · 13 comments · Fixed by #6562 or python-pillow/pillow-wheels#405
Labels

Comments

@mirusu400
Copy link

mirusu400 commented Sep 1, 2022

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 error, 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.

@mirusu400 mirusu400 changed the title Unknown file format error on some woff and woff2 files on ImageFont.truetype() Unknown file format error on some woff and woff2 fonts on ImageFont.truetype() Sep 1, 2022
@radarhere radarhere added the Fonts label Sep 1, 2022
@radarhere
Copy link
Member

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'

@mirusu400
Copy link
Author

mirusu400 commented Sep 1, 2022

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.

@radarhere radarhere added the macOS label Sep 1, 2022
@mirusu400 mirusu400 changed the title Unknown file format error on some woff and woff2 fonts on ImageFont.truetype() Unknown file format error on some woff2 fonts on ImageFont.truetype() Sep 1, 2022
@radarhere
Copy link
Member

radarhere commented Sep 2, 2022

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.

@mirusu400
Copy link
Author

mirusu400 commented Sep 2, 2022

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'

@radarhere radarhere removed the macOS label Sep 2, 2022
@lystor
Copy link

lystor commented Sep 3, 2022

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

@radarhere
Copy link
Member

radarhere commented Sep 7, 2022

python-pillow/pillow-wheels#320 has been created to add brotli to our macOS and Linux wheels.

@nulano
Copy link
Contributor

nulano commented Sep 7, 2022

I've added brotli to the #6562 changes for Windows wheels.

@radarhere
Copy link
Member

Pillow 9.3.0 has now been released, so brotli should be included in the latest wheels now.

@natuan62
Copy link

natuan62 commented Sep 7, 2023

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)

123

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

@homm
Copy link
Member

homm commented Sep 7, 2023

FONT_NAME="Karla-Regular.woff2"

Could you provide a link to the file or the file itself?

@natuan62
Copy link

natuan62 commented Sep 7, 2023

Hi @homm
Here the file Karla-Regular.woff2.zip

And my Dockerfile

image

@radarhere
Copy link
Member

I've created python-pillow/pillow-wheels#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

@radarhere
Copy link
Member

Pillow 10.0.1 has now been released, so that wheel should now also be available through pip.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
6 participants