简体   繁体   English

无法使用Pillow和Python居中字符

[英]Can't center characters using Pillow and Python

I am trying to write characters in specific locations in an image. 我正在尝试在图像的特定位置写入字符。 I am using Pillow v6, Python 3.6. 我正在使用Pillow v6,Python 3.6。

Here is my code, I draw char by char, passing the top left point that I calculated. 这是我的代码,我按字符绘制char,并传递我计算出的左上角。

   font = ImageFont.truetype('platechar.tff', 500)

   def draw_single_char(img, font, val, tl): #tl = (x,y)
        pil_img = Image.fromarray(np.uint8(img))
        draw = ImageDraw.Draw(pil_img)
        draw.text(tl, val, (0,0,0), font=font)
        img = np.array(pil_img)
        return img

The output is not centered, I got the character width and height from the font, then with my top left point I draw the rectangle enclosing the character. 输出未居中,我从字体中获得了字符的宽度和高度,然后用左上角绘制了包围字符的矩形。 The character is not centered inside the rectangle. 字符不在矩形内居中。

Font: https://drive.google.com/open?id=1N9rN-AgjK83U9ZDycLKxjeMP3o36vbfg 字体: https//drive.google.com/open?id = 1N9rN-AgjK83U9ZDycLKxjeMP3o36vbfg 在此处输入图片说明

I want it to be like this (another font) 我希望它像这样(另一种字体) 另一种字体


EDIT Using Bidu Font editor I was able to remove the horizontal space (blue line). 编辑使用Bidu字体编辑器,我可以删除水平空间(蓝线)。 How can I center it vertically?. 如何垂直居中? 在此处输入图片说明

Result so far ... 到目前为止的结果... 在此处输入图片说明

It looks like the font you are using contains non-centered numbers inside originally. 看起来您正在使用的字体最初包含非中心数字。 So you should choose another font or you can modify your placechar.tff in a special editor for fonts. 因此,您应该选择其他字体,或者可以在特殊的字体编辑器中修改placechar.tff

Also you can calculate coordinate offsets for each symbol manually, store them into a dictionary and apply it for your text before drawing. 另外,您可以手动计算每个符号的坐标偏移,将它们存储在字典中,然后在绘图之前将其应用于文本。 It doesn't look like a good idea, but it would work also. 看起来这不是一个好主意,但也可以。

Calculate the width and height of the text to be drawn: 计算要绘制的文本的宽度和高度:

from PIL import Image, ImageDraw, ImageFont

txt='7'

font = ImageFont.truetype('platechar.ttf', 250)
(W, H) = font.getsize(txt)
image = Image.new('RGB', (256, 256), (63, 63, 63, 0))
drawer = ImageDraw.Draw(image)

(offset_w, offset_h) = font.getoffset(txt)
(x, y, W_mask, H_mask) = font.getmask(txt).getbbox()
drawer.text((10, 10 - offset_h), txt, align='center', font=font)
drawer.rectangle((10, 10, W + offset_w, 10 + H - offset_h), outline='black')
drawer.rectangle((x+10, y+10, W_mask+10, H_mask+10), outline='red')
image.show()
image.save('example.png', 'PNG')

结果

After taking the path that @Fomalhaut suggested, using font editor. 在使用@Fomalhaut建议的路径后,使用字体编辑器。 I found Bidu font editor (link in the question). 我找到了Bidu字体编辑器(问题中的链接)。 I was able to fix the horizontal space (also shown in the question). 我能够固定水平空间(也显示在问题中)。 For vertical space, after searching the menus, I found setting option to change the ascent . 对于垂直空间,在搜索菜单后,我发现了用于更改ascent设置选项。

在此处输入图片说明

I decreased it to 1440, and it worked. 我将其降低到1440,并且可以正常工作。 在此处输入图片说明

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

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