tags:

views:

39

answers:

2

Is it possible to get the kerning value of two characters in a font using PIL?

+1  A: 

No, PIL can only be used to render text, not get its metrics. You'll need to use either the appropriate Windows API functions, or the FreeType library (for which no stable Python bindings exist) in order to get kerning info.

Ignacio Vazquez-Abrams
isn't PIL built on freetype?
Geo
Yes, but it doesn't expose the metrics functions.
Ignacio Vazquez-Abrams
+1  A: 

The answer is, as Ignacio said, no.

That was the serious part of this answer. And now, for something completely different:

you can approximate a (not the, since you can't know by using PIL alone what is the design em size of the font) kerning value using something like:

import ImageFont
SAMPLE_SIZE= 128 # should provide sufficient resolution for kerning values

def get_a_kerning_value(font_descriptor, char1, char2):
    """See if there is some kerning value defined for a pair of characters
    Return the kerning value as an approximated percentage of the row height."""

    imagefont= ImageFont.truetype(font_descriptor, SAMPLE_SIZE)

    width1= imagefont.getsize(char1)[0]
    width2= imagefont.getsize(char2)[0]
    widths, height= imagefont.getsize(char1 + char2)

    return (widths - (width1 + width2))/float(height)
ΤΖΩΤΖΙΟΥ