Skip to content

Conversation

@fjhenigman
Copy link

@fjhenigman fjhenigman commented Feb 4, 2026

Facilitates creating an ImageFont from a PCF or BDF file on the fly, without creating any new files, with code like this:

def ifont(path):
    with open(path, 'rb') as f:
        ff = PcfFontFile.PcfFontFile(f)
    ff.compile()
    buf = io.BytesIO()
    ff.save_metrics(buf)
    buf.seek(0)
    imgfont = ImageFont.ImageFont()
    imgfont._load_pilfont_data(buf, ff.bitmap)
    return imgfont

This isn't ideal, creating a file in memory and using an underscore function. Maybe it's preferable to add functionality to ImageFont.py instead?

@radarhere radarhere changed the title Factor out saving metrics in FontFile. Factor out saving metrics in FontFile Feb 4, 2026
@radarhere
Copy link
Member

Hi. To outline the problem, you've said that you would like to convert FontFile instances to ImageFont instances.

I'm guessing you saw https://pillow.readthedocs.io/en/stable/reference/ImageFont.html#imagefont-module and https://github.com/python-pillow/pillow-scripts/blob/main/Scripts/pilfont.py. They've sort of presumed that you only need to convert once, or maybe only convert a small number of fonts, but presumably you would like your users to be able to make this conversion for any PCF or BDF font they might choose at runtime, and you would like to do so efficiently.

My suggestion would be adding a to_imagefont() method to FontFile. What do you think?

Also, just to properly understand the need, what is it that you want to do with the FontFile instances? Draw text?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants