PIL cuts off top of letters

后端 未结 2 671
青春惊慌失措
青春惊慌失措 2020-12-06 19:42

I\'ve spent a lot of time making my first web application using Python, and I\'m using pil for generating images. After reading a lot, I\'ve managed to implement proper text

相关标签:
2条回答
  • 2020-12-06 20:03

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

    0 讨论(0)
  • 2020-12-06 20:07

    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:

    enter image description here     enter image description here

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

    enter image description here     enter image description here

    On Ubuntu:

    enter image description here     enter image description here

    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.

    0 讨论(0)
提交回复
热议问题