13rac1 / twemoji-color-font

Twitter Unicode emoji color OpenType-SVG font for Linux/MacOS/Windows
Other
1.72k stars 74 forks source link

Python PIL OSError: invalid pixel size #142

Open youasscoder opened 11 months ago

youasscoder commented 11 months ago

font = ImageFont.truetype(font_path, font_size) no size gives me no "OSError: invalid pixel size" python PIL

HinTak commented 9 months ago

I think that's an error from freetype - and it is what it says, invalid pixel size. Some bitmap color fonts (sbix and CBDT) contains bitmaps designed for specific sizes, and if you ask for pixel sizes not in one of the designed values, FreeType gives you that error. You can use "ftdump" to get a list of what valid sizes are.

13rac1 commented 9 months ago

How is this error related to this font?

HinTak commented 9 months ago

From that error message, I would assume that PIL's ImageFont.truetype() uses freetype to load this font, and PIL can't use the SVG table, so it is reading the sbix or CBDT table only of this font, which are of specific pixel sizes.

I thought I have written a README.md about it, but perhaps I have only written to freetype-devel: if you have either pycairo or skia-python available, you can make any freetype-py using program (and I believe PIL being one) support SVG-OT with either https://github.com/HinTak/harfbuzz-python-demos/blob/master/otsvg.py or https://github.com/HinTak/harfbuzz-python-demos/blob/master/skia_ot_svg_module.py .

These two basically enable any freetype-py using python programs to read SVG-OT fonts. And SVG-OT being any-pixel-size, it gets around the sbix/CBDT fixed-pixel-size limitations.

You might have to ask PIL people how to hook the two modules in - it depends on how PIL uses freetype-py/freetype.

HinTak commented 9 months ago

Okay, I looked at PIL in my hard disk - it uses freetype directly via _imagingft.cpython.....so. You'll need to contact the PIL people directly, essentially with the above info. They need to hook up the C-equivalent of that python code. There are again two versions of it, the cairo-dependent version is upstream in ft2-demos, while the skia-dependent version only exists as a patch and is posted at https://github.com/HinTak/harfbuzz-python-demos/tree/master/skia-adventure, with a README.md describing it.