Python字体显示为全正方形

Python字体显示为全正方形,python,python-3.x,image,fonts,Python,Python 3.x,Image,Fonts,我有一个将文本转换为图像的文件 from PIL import Image from PIL import ImageDraw from PIL import ImageFont from random import seed from random import randint '''Returns the text size in terms of width and height.''' def getSize(txt, font): testImg = Image.new('

我有一个将文本转换为图像的文件

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

from random import seed
from random import randint

'''Returns the text size in terms of width and height.'''
def getSize(txt, font):
    testImg = Image.new('RGB', (1, 1))
    testDraw = ImageDraw.Draw(testImg)
    return testDraw.textsize(txt, font)

text = "As fast as thou shalt wane so fast thou grow'st,
In one of thine, from that which thou departest,
And that fresh blood which youngly thou bestow'st,
Thou mayst call thine, when thou from youth convertest,
Herein lives wisdom, beauty, and increase,
Without this folly, age, and cold decay,
If all were minded so, the times should cease,
And threescore year would make the world away:
Let those whom nature hath not made for store,
Harsh, featureless, and rude, barrenly perish:
Look whom she best endowed, she gave thee more;
Which bounteous gift thou shouldst in bounty cherish:
She carved thee for her seal, and meant thereby,
Thou shouldst print more, not let that copy die."

fontname = "MSGEOTX1.TTF" # Times New Roman Special G1 Font
fontsize = randint(10,50)  
colorText = "black"
colorOutline = "white"
colorBackground = "white"


font = ImageFont.truetype(fontname, fontsize)     
width, height = getSize(text, font)    
img = Image.new('RGB', (width+4, height+4), colorBackground)
d = ImageDraw.Draw(img)
d.text((0, 0), text, fill=colorText, font=font)
d.rectangle((0, 0, width+3, height+3), outline=colorOutline)

img.save( "image.png")
但渲染图像时,它会显示所有方形框。为什么


这可能是因为字体是“currupted”字体基本上没有这些字符。因为字体无法呈现它没有的内容,所以它会显示正方形,所以您的字体可能有问题。
尝试更改为其他字体,或尝试使用其他方法显示它。

其他字体正常工作,但我希望此特定字体正常工作。如果其他字体使用完全相同的代码,则问题在于您要使用的字体,不是代码本身。因为其他字体正在工作,所以这种特殊字体肯定有问题。但是如果你真的想要这种字体,我会尝试从其他网站获取这种字体。这种字体缺少这些字符的字形。最常用的字体有广泛的字形,包括常见的脚本(拉丁语、希腊语、希伯来语、西里尔语、阿拉伯语等)。字体没有“损坏”,只是没有覆盖脚本。完全不相关,但在Python中,注释是使用
#
符号完成的,而不是三重引号的字符串。Brunodesshuilliers我们可以在代码中添加多行字符串(三重引号),并将注释放在其中。@Brunodesshuilliers-->我没有说使用字符串(尽管它们被引用为FWIW)在技术上是非法的,我说这些不是注释。除非用作,否则使用literal字符串作为注释被认为是糟糕的编码风格。