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

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

问题描述

我花了很多时间使用 Python 制作我的第一个 Web 应用程序,我正在使用 ,但我想避免使用另一个模块(aggdraw);因为我已经在 PIL 中发现了很多东西,所以我想坚持下去.

我尝试了许多不同大小的字体,但文本仍然被切断.我什至尝试使用 PIL 字体,但它仍然不起作用.[还将 OTF 转换为 BDF 和 PIL].

这是在 Ubuntu 上.接下来我应该尝试什么?

解决方案

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

以下是我在没有补丁和有补丁的情况下分别获得的一些输出示例:

   

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

   

在 Ubuntu 上:

   

这是生成顶部数字的代码:

# -*- 编码:utf8 -*-导入系统导入图像、ImageDraw、ImageFontim = Image.new("RGBA", (1000, 1000), '白色')draw = ImageDraw.Draw(im)开始_y = 7文本 = u'u00d1u00d3yŻu00d4Ćgpu010cu0137'对于 xrange(28, 46, 2) 中的 i:font = ImageFont.truetype('Junicode-Bold.ttf', i)宽度,高度 = 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 += 高度 + 7im.crop((0, 0, width + 1, start_y + 2)).save(sys.argv[1])

底部数字是根据有关 PIL 切断部分文本的链接主题中的代码生成的.

I've spent a lot of time making my first web application using Python, and I'm using for generating images. After reading a lot, I've managed to implement proper text aligning, wrapping, generating files with many extensions etc.

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

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

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.

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].

This is on Ubuntu. What should I try next?

解决方案

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'u00d1u00d3yŻu00d4Ćgpu010cu0137'
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.

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

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