PIL切断字母的顶部 [英] PIL cuts off top of letters

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

问题描述

我已经花了很多时间使用Python创建我的第一个Web应用程序,并且正在使用用于生成图像.经过大量阅读后,我设法实现了正确的文本对齐,自动换行,生成具有许多扩展名的文件等.

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 aligning, wrapping, generating files with many extensions etc.

但是,PIL生成的所有文本都在顶部被截断.这是一个示例.

However, all the text generated by PIL is cut off at the top. Here's a sample.

它应该以多种字体显示ŻÓĆjygpq(字体名称在左侧).

It should say ŻÓĆjygpq in a variety of fonts (the font names are on the left).

我在这里发现了几篇文章:使用PIL进行字体剪裁, 但我想避免使用另一个模块(aggdraw);因为我已经在PIL中找到了很多东西,所以我要坚持下去.

I've found few posts here: fonts clipping with PIL, but I'd like to avoid using another module (aggdraw); since I've figured out so many things in PIL already I'd like to stick to that.

我尝试了许多不同大小的字体,但是文本仍然被截断.我什至尝试使用PIL字体,但仍然无法正常工作. [也将OTF转换为BDF和PIL].

I've tried many fonts in different sizes, but text is still cut off. I even tried to use PIL fonts, but it still doesn't work. [Also converting OTF to BDF, and to PIL].

这是在Ubuntu上.我下一步该怎么做?

This is on Ubuntu. What should I try next?

推荐答案

我希望这是错误的,但是唯一正确的解决方法取决于修补_imagingft.c的方式 呈现文本. PIL依赖于FreeType来完成此任务,但是PIL似乎在错误地计算位置.另外,getsize中的高度被高估了(尽管这不会引起问题).目前,我已在以下位置放置了一个补丁程序来处理这些问题: http://pastebin.com/jP2iLkDN(似乎是修补渲染代码的更好方法).

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:

   

   

使用链接的讨论中提供的代码得出结果.在OSX上:

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

   

   

在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])

底部图形是根据链接的主题中有关PIL截取部分文本的代码生成的.

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

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

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