PIL字体剪裁 [英] fonts clipping with PIL

查看:119
本文介绍了PIL字体剪裁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此图像是使用PIL创建的.看看这张图片中的g和y是如何截取的吗?我该如何预防?

http://img109.imageshack.us/img109/8874/screenshotep.png

创建此图像的代码非常简单(缩写):

import Image, ImageDraw, ImageFont

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

font = ImageFont.truetype("VeraSe.ttf", 12)

draw.text(
           (1, 1),
           " %s: " % "ggjyfFwe__",
           font=font,
           fill='black'
)

draw.text(
           (1, 30),
           " %s" % 15,
           font=font,
           fill='black'
)

im.show()

我尝试了几种不同的字体,但它总是被裁剪.令人惊讶的是,y,在Google上搜索"PIL字体剪裁"返回的有用信息很少...我在Ubuntu 9.10上使用python 2.6.4和PIL 1.1.6

解决方案

这是一个较早回答的较晚答案.

问题似乎是PIL和Pillow会剪切渲染文本的边缘.这通常在尾随宽字符和减号(如"y")上显示.这也可能会出现在某些字体的顶部.至少十年来,这一直是一个问题.无论调用text()的图像大小如何,都会发生这种情况.冲突似乎是将边界矩形选择为"font.size * number_chars",而不是我实际需要呈现的内容",并且这发生在堆栈的深处(_imagingft.c).解决此问题会导致其他问题,例如逐行排列呈现的文本.

一些解决方案包括:

  • 在字符串的末尾添加一个空格. im.text(xy, my_text + ' ', ...)
  • 对于高度问题,获取文本的宽度(font.getsize()),第二次渲染文本,再加上良好的上升和下降效果,将渲染的文本切成第一个报告的宽度和第二个实际高度.
  • 使用其他库,例如 AggDraw 使用PIL进行字体剪切.这些问题引用了相同的潜在问题,但并非重复

    This image was created with PIL. See how the g's and the y's are cut off in this image? How can I prevent this?

    http://img109.imageshack.us/img109/8874/screenshotep.png

    The code that created this image is pretty straight forward (abbreviated):

    import Image, ImageDraw, ImageFont
    
    im = Image.new("RGBA", (200, 200), 'white')
    draw = ImageDraw.Draw(im)
    
    font = ImageFont.truetype("VeraSe.ttf", 12)
    
    draw.text(
               (1, 1),
               " %s: " % "ggjyfFwe__",
               font=font,
               fill='black'
    )
    
    draw.text(
               (1, 30),
               " %s" % 15,
               font=font,
               fill='black'
    )
    
    im.show()
    

    I tried it with a few different fonts, and it always gets clipped. Surprising;y, googleing "PIL font clipping" returns very few useful hits... I'm using python 2.6.4 and PIL 1.1.6 on Ubuntu 9.10

    解决方案

    Here's a late answer for this older question.

    The problem appears to be that PIL and Pillow will clip the edges of rendered text. This most often shows on trailing wide characters and decenders (like 'y's). This can also appear on the top of some fonts. This has been a problem for at least ten years. It happens regardless of the size of the image on which text() is called. The conflict appears to choosing the bounding rectangle as "font.size * number_chars" instead of "whatever I actually need to render" and this occurs deep in the stack (_imagingft.c). Fixing this causes other problems, like lining up text rendered letter by letter.

    Some solutions include:

    • Append a space to the end of your string. im.text(xy, my_text + ' ', ...)
    • For height issues, get the width of your text (font.getsize()), second render the text plus a good ascender and descender, chop the rendered text to the first reported width and the second actual height.
    • Use a different library such as AggDraw or pyvips.

    This is referenced in various questions fonts clipping with PIL, PIL cuts off top of letters, Properly render text with a given font in Python and accurately detect its boundaries. These questions reference the same underlying issue but are not duplicates

    这篇关于PIL字体剪裁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆