PIL cuts off top of letters

帅比萌擦擦* 提交于 2019-11-28 01:21:28

I hope to be wrong on this one, but the only correct fix relies on patching how _imagingft.c renders the text. PIL depends on FreeType for this task, but PIL seems to be miscalculating the positioning. Also, the height in getsize is overestimated (although that doesn't cause problem). For the moment, I've put a patch to handle these issues at: http://pastebin.com/jP2iLkDN (there seems to be a better way to patch the render code).

Here are some examples of the output I get without the patch and with the patch, respectively:

   

Results using the code present in the linked discussion. On OSX:

   

On Ubuntu:

   

Here is the code to generate the top figures:

# -*- encoding: utf8 -*-
import sys
import Image, ImageDraw, ImageFont

im = Image.new("RGBA", (1000, 1000), 'white')
draw = ImageDraw.Draw(im)

start_y = 7
text = u'\u00d1\u00d3yŻ\u00d4Ćgp\u010c\u0137'
for i in xrange(28, 46, 2):
    font = ImageFont.truetype('Junicode-Bold.ttf', i)
    width, height = font.getsize(text)
    draw.rectangle((0, start_y, width, height + start_y), outline='blue')
    draw.text((0, start_y), text, font=font, fill='black')
    start_y += height + 7

im.crop((0, 0, width + 1, start_y + 2)).save(sys.argv[1])

The bottom figures were generated according to code present in the linked topic about PIL cutting off parts of the text.

Not the best solution but I see people have solved this by adding a leading a trailing space to their text.

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