Can I get the kerning value of two characters using PIL?
Is it possible to get the kerning va开发者_如何转开发lue of two characters in a font using PIL?
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)
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.
There is kern-dump which will dump kern values for all glyph pairs in a font file:
https://github.com/adobe-type-tools/kern-dump
精彩评论