I am using Python's image processing libraries to render images of characters using different fonts.
Here's a snippet of the code that I'm using to iterate through a list of fonts and a list of characters to output an image of that character for each font.
from PIL import Image, ImageFont, ImageDraw
...
image = Image.new('L', (IMAGE_WIDTH, IMAGE_HEIGHT), color=0)
font = ImageFont.truetype(font, 48)
drawing = ImageDraw.Draw(image)
w, h = drawing.textsize(character, font=font)
drawing.text(
((IMAGE_WIDTH-w)/2, (IMAGE_HEIGHT-h)/2),
character,
fill=(255),
font=font
)
However, in some cases, the font doesn't support a character and renders either a black image or a default/invalid character. How can I detect that the character isn't supported by a font and handle that case separately?