简体   繁体   English

如何在 Python Pillow 中设置字体的字距?

[英]How to set font's kerning in Python Pillow?

For Pillow class ImageDraw ( https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html#PIL.ImageDraw.PIL.ImageDraw.ImageDraw.text ) I found param features .对于枕头 class ImageDrawhttps://pillow.readthedocs.io/en/stable/reference/ImageDraw.html#PIL.ImageDraw.PIL.ImageDraw.ImageDraw.text )我找到了参数特征 It can tune this font parameters: https://docs.microsoft.com/en-us/typography/opentype/spec/featurelist它可以调整这个字体参数: https://docs.microsoft.com/en-us/typography/opentype/spec/featurelist

How to set kerning (kern) param?如何设置字距(kern)参数? I don't know syntax.我不知道语法。

This is not work:这是行不通的:

draw.text((61, 386), text, (0, 0, 0), font=font, features={"kern": 1.0})

I hit this same issue recently.我最近遇到了同样的问题。 I decided the best approach was to draw each letter individually and calculate the space between needed to make it even.我认为最好的方法是单独绘制每个字母并计算使其均匀所需的空间。 This is the code I got it working with.这是我使用的代码。

total_text_width, total_text_height = draw.textsize("Sample Text", font=font)
width_difference = desired_width_of_text - total_text_width
gap_width = int(width_difference / (len(total_text_width) - 1))
xpos = left_side_padding
for letter in header_text:
    draw.text((xpos,0),letter, (0,0,0), font=font)
    letter_width, letter_height = draw.textsize(letter, font=font)
    xpos += letter_width + gap_width

default spacing vs drawing each letter and calculating spacing默认间距与绘制每个字母并计算间距

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM