Python PIL减少字母间距

时间:2018-03-28 09:03:10

标签: python python-imaging-library

如何减少此文字的字母间距?我希望通过几个像素使文本更加模糊。

我正在尝试制作一个透明图像,上面有文字,我希望将它们拼凑在一起。像这样,但透明:

image

from PIL import Image, ImageDraw, ImageFont

(W, H) = (140, 40)

#create transparent image
image = Image.new("RGBA", (140, 40), (0,0,0,0))

#load font
font = ImageFont.truetype("Arial.ttf", 30)
draw = ImageDraw.Draw(image)

text = "kpy7n"
w,h = font.getsize(text)

draw.text(((W-w)/2,(H-h)/2), text, font=font, fill=0)

image.save("transparent-image.png")

1 个答案:

答案 0 :(得分:0)

您必须逐个字符地绘制文本,然后在绘制下一个

时更改x坐标

代码示例:

w,h = font.getsize("k")
draw.text(((W,H),"K", font=font, fill=0)
draw.text(((W+w)*0.7,H),"p", font=font, fill=0)
draw.text(((W+w*2)*0.7,H),"y", font=font, fill=0)
draw.text(((W+w*3)*1,H),"7", font=font, fill=0)
draw.text(((W+w*4)*0.8,H),"n", font=font, fill=0)