如何找出 PIL 无法正确绘制字体的原因?
这是我正在使用的代码:
from PIL import Image
import ImageFont, ImageDraw
import sys
import pdb
img = Image.new("RGBA",(300,300))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype(sys.argv[1],30)
draw.text((0,100),"world",font=font,fill="red")
del draw
img.save(sys.argv[2],"PNG")
这是生成的图像: img http://www.freeimagehosting.net/image.php?976a0d3eaa.png (由于某种原因,我无法让它在 SO 上显示,所以链接是 http://www.freeimagehosting.net/image.php?976a0d3eaa.png )
问题是,我不明白为什么它没有正确绘制字体?我应该能从上面读出“世界”这个词。就像图片被切成两半一样。有人有任何线索吗?
编辑:在 balpha 的评论之后,我决定尝试另一种字体。我只对 ttf 字体感兴趣,所以我尝试了另一种字体,它成功了。这有点奇怪。我尝试运行的原始字体是 Beautiful ES。我很好奇你们是否可以在计算机上重现相同的图像,并且您是否碰巧知道其原因。
Here's the code I'm using:
from PIL import Image
import ImageFont, ImageDraw
import sys
import pdb
img = Image.new("RGBA",(300,300))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype(sys.argv[1],30)
draw.text((0,100),"world",font=font,fill="red")
del draw
img.save(sys.argv[2],"PNG")
and here's the image that results:
img http://www.freeimagehosting.net/image.php?976a0d3eaa.png ( for some reason, I can't make it show on SO, so the link is http://www.freeimagehosting.net/image.php?976a0d3eaa.png )
The thing is, I don't understand why it isn't drawing the font correctly? I should be able to read the word "world" off of it. It's like the picture's been cut in half or something. Does anyone have any clue?
EDIT: after balpha's comment, I decided to try another font. I'm only interested in ttf fonts, so I tried with another one, and it worked. This is kind of strange. The original font I tried to run this with is Beautiful ES. I'm curious if you guys can reproduce the same image on your computers, and if you happen to know the reason for why that is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PIL使用freetype2库,所以很可能是字体文件的问题;例如,它可能定义了错误的指标(例如,请参阅使用 FontForge 打开字体的 OS/2 相关指标)。
PIL uses the freetype2 library, so most possibly it is an issue with the font file; for example, it could have bad metrics defined (e.g see the OS/2 related ones opening the font with FontForge).