Skip to content

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

@mirusu400

Description

@mirusu400

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions