Skip to main content
 首页 » 编程设计

python之如何将非抗锯齿字体渲染为内部图像

2025年05月04日17kerrycode

使用 Python 将非抗锯齿字体(例如 ttf 字体)渲染到内部图像(例如 PIL.Image,即我不需要显示它)?我说准确是因为我前一阵子用 pygame 试过它,我给它的渲染字体大小与 Word 或 Paint 中渲染的窗口不匹配。

请您参考如下方法:

Python 图像库 (PIL) 可以将文本呈现为图像——我不知道它不准确,但我还没有对其进行全面测试...

来自已有问题的示例:

from PIL import Image 
from PIL import ImageFont, ImageDraw 
 
image = Image.new("RGBA", (288,432), (255,255,255)) 
usr_font = ImageFont.truetype("resources/HelveticaNeueLight.ttf", 25) 
d_usr = ImageDraw.Draw(image) 
d_usr.fontmode = "1" # this apparently sets (anti)aliasing.  See link below. 
d_usr.text((105,280), "MYTEXT",(0,0,0), font=usr_font) 

另见:

http://www.pythonware.com/products/pil/

http://mail.python.org/pipermail/image-sig/2005-August/003497.html

Python Imaging Library - Text rendering