开发者

Size differences between TrueType and OpenType fonts when using PIL ImageFont

开发者 https://www.devze.com 2022-12-14 18:49 出处:网络
I\'m trying to create PNGs of letters from the fonts on my system. Seems to work okay with TrueType fonts (.ttf) but not OpenType (.otf). The images formed are the same size but the actual letters app

I'm trying to create PNGs of letters from the fonts on my system. Seems to work okay with TrueType fonts (.ttf) but not OpenType (.otf). The images formed are the same size but the actual letters appear much smaller with OpenType - see below.

I'm using the 开发者_C百科ImageFont module from the Python Imaging library. There doesn't seem to be a way to distinguish between the two formats; both are imported with

ImageFont.truetype(filename, size)

What am I missing?


example code:

try:
 font = ImageFont.truetype(name, size)
 for text in string.letters:

  size2 = font.getsize(text)
  
  im = Image.new('RGBA', size2, (0, 0, 0, 0))
  draw = ImageDraw.Draw(im)
  draw.text((0, 0), text, font=font, fill="black")
  
  if not os.path.exists(name[:-4]):
   os.makedirs(name[:-4])
  
  if text in string.lowercase:
   im.save(name[:-4]+".png")
  else:
   im.save(name[:-4]+"_u.png")
except:
 pass

Size differences between TrueType and OpenType fonts when using PIL ImageFont

Size differences between TrueType and OpenType fonts when using PIL ImageFont

Images of Adobe Caslon and Arial for comparsion.

0

精彩评论

暂无评论...
验证码 换一张
取 消