Python Imaging Library-文本渲染 [英] Python Imaging Library - Text rendering

查看:110
本文介绍了Python Imaging Library-文本渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PIL渲染一些文本,但是坦率地说,结果是胡扯.

I'm trying to render some text using PIL, but the result that comes out is, frankly, crap.

例如,这是我在Photoshop中写的一些文字:

For example, here's some text I wrote in Photoshop:

以及PIL产生的结果

如您所见,PIL的结果令人不满意.也许我只是很挑剔,但是有什么方法可以使用PIL绘制文本,使结果更接近我的参考图像?

As you can see, the results from PIL is less than satisfactory. Maybe I'm just being picky, but is there any way to draw text using PIL that gets results more close to my reference image?

这是我在带有PIL 1.1.7的Python 2.7上使用的代码

Here's the code I'm using on Python 2.7 with PIL 1.1.7

image = Image.new("RGBA", (288,432), (255,255,255))
usr_font = ImageFont.truetype("resources/HelveticaNeueLight.ttf", 25)
d_usr = ImageDraw.Draw(image)
d_usr = d_usr.text((105,280), "Travis L.",(0,0,0), font=usr_font)

推荐答案

我提出了自己认为可以接受的解决方案.

I came up with my own solution that I find acceptable.

我所做的是将文本放大,例如需要放大3倍,然后使用抗锯齿将其缩放,虽然不是100%完美,但比默认情况好很多,而且没有需要开罗或pango.

What I did was render the text large, like 3x the size it needs to be then scale it resize it down with antialiasing, it's not 100% perfect, but it's a hell of a lot better than default, and doesn't require cairo or pango.

例如,

image = Image.new("RGBA", (600,150), (255,255,255))
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("resources/HelveticaNeueLight.ttf", fontsize)

draw.text((10, 0), txt, (0,0,0), font=font)
img_resized = image.resize((188,45), Image.ANTIALIAS)

您最终得到这个结果,

这比我以前使用相同字体要好得多.

which is a lot better than what I was getting before with the same font.

这篇关于Python Imaging Library-文本渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆