Python Imaging Library - Text rendering

前端 未结 6 942
终归单人心
终归单人心 2020-11-28 03:49

I\'m trying to render some text using PIL, but the result that comes out is, frankly, crap.

For example, here\'s some text I wrote in Photoshop:

6条回答
  •  孤城傲影
    2020-11-28 04:11

    I came up with my own solution that I find acceptable.

    What I did was render the text large, like 3x the size it needs to be then scale it resize it down with antialiasing, it's not 100% perfect, but it's a hell of a lot better than default, and doesn't require cairo or pango.

    for example,

    image = Image.new("RGBA", (600,150), (255,255,255))
    draw = ImageDraw.Draw(image)
    font = ImageFont.truetype("resources/HelveticaNeueLight.ttf", fontsize)
    
    draw.text((10, 0), txt, (0,0,0), font=font)
    img_resized = image.resize((188,45), Image.ANTIALIAS)
    

    and you endup with this result,

    final result

    which is a lot better than what I was getting before with the same font.

提交回复
热议问题