Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用PIL ImageFont时TrueType和OpenType字体之间的大小差异_Python_Image_Fonts_Python Imaging Library - Fatal编程技术网

Python 使用PIL ImageFont时TrueType和OpenType字体之间的大小差异

Python 使用PIL ImageFont时TrueType和OpenType字体之间的大小差异,python,image,fonts,python-imaging-library,Python,Image,Fonts,Python Imaging Library,我正在尝试从我的系统上的字体创建字母的PNG。似乎可以使用TrueType字体(.ttf),但不能使用OpenType(.otf)。形成的图像大小相同,但使用OpenType时实际的字母看起来要小得多-见下文 我正在使用Python图像库中的ImageFont模块。似乎没有办法区分这两种格式;两者都是进口的 ImageFont.truetype(filename, size) 我错过了什么 示例代码: try: font = ImageFont.truetype(name, size)

我正在尝试从我的系统上的字体创建字母的PNG。似乎可以使用TrueType字体(.ttf),但不能使用OpenType(.otf)。形成的图像大小相同,但使用OpenType时实际的字母看起来要小得多-见下文

我正在使用Python图像库中的ImageFont模块。似乎没有办法区分这两种格式;两者都是进口的

ImageFont.truetype(filename, size)
我错过了什么


示例代码:

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


Adobe Caslon和Arial的图像用于比较。

你能告诉我你给这两种字体的大小吗?我用72作为大小变量。你能给出一些示例代码吗?font.getsize()返回什么?(从这个问题:)font.getsize()返回我期望的元组。它只用于计算画布的大小,这似乎是正确的-在我给出的示例中,您可以看到Adobe Caslon“B”的画布的大小与Arial相似,但字母本身太小。我已经用一些示例代码更新了我的问题。这是旧的,但是你能在PIL 1.1.7上重试吗?这对我来说很好。