python render non-anti-aliased font to internal image

烂漫一生 提交于 2019-12-13 00:46:00

问题


What is the most speedy way and accurate to render non-anti-aliased fonts (e.g. ttf fonts) using Python to an internal image (e.g. to a PIL.Image, i.e. I don't need to display it)? I say accurate because I tried it with pygame a while back and the rendered fonts at the size I gave it didn't match what windows rendered in Word or Paint.


回答1:


Python Imaging Library (PIL) can render text to an image--I'm not aware of it being inaccurate, but I haven't fully tested it yet...

Example from a pre-existing question:

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)

See also:

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

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

Python Imaging Library - Text rendering




回答2:


Ahh this is how it's done... should render the same as windows at is uses its algorithm.



来源:https://stackoverflow.com/questions/5747739/python-render-non-anti-aliased-font-to-internal-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!