python - 在 Pillow 中确定带有换行符的字符串的像素高度

标签 python python-2.7 python-imaging-library pillow

我正在尝试在空白图像上绘制文本(可以是任意长度,具体取决于用户输入的内容)。我需要在换行符中拆分文本以避免创建太大的图像,我还需要创建一个图像,其大小与用户输入的字符数相关,避免出现任何空白。这是我到目前为止想出的:

import PIL
import textwrap
from PIL import ImageFont, Image, ImageDraw

#Input text
usrInput = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam sit amet scelerisque nulla. Pellentesque mollis tellus ut arcu malesuada auctor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Ut tristique purus non ultricies vulputate"
text = textwrap.fill(usrInput,50)

#font size, color and type
fontColor = (255,255,255)
fontSize = 40
font = ImageFont.truetype("/System/Library/Fonts/AppleGothic.ttf",fontSize)

#Image size and background color
background = (200,255,0)
#imgSize = font.getsize(text)
imgSize = ImageDraw.Draw.textsize(text, font)

def CreateImg ():
    img = Image.new("RGBA", imgSize, background)
    draw = ImageDraw.Draw(img)
    draw.text((0,0), text, fontColor, font)
    img.save("test.png")


CreateImg()

现在我遇到了一个问题。如果我使用 font.getsize 来确定图像应该有多大,它会完全按照我想要的方式工作,但前提是文本不会换行。如果是这样,它会给我单行的高度和没有换行符的全文宽度。 所以我认为这可能不是正确的方法,我决定尝试 ImageDraw.Draw.textsize(应该检测线条并使用 ImageDraw.Draw.multiline_textsize 如果有多个),但它不起作用并且我得到这个错误:

Traceback (most recent call last):
File "pil.py", line 17, in <module>
    imgSize = ImageDraw.Draw.textsize(text, font)
AttributeError: 'function' object has no attribute 'textsize'

我做错了什么?我很好地处理了这个问题,还是有更好的解决方案?

最佳答案

我觉得你把语句的顺序搞混了。

在应该刚好足够大的图像上绘制文本需要两个步骤:首先,确定文本大小,然后创建该大小的图像。

这是一个工作示例:

from PIL import ImageFont, Image, ImageDraw
import textwrap

# Source text, and wrap it.
userinput = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam sit amet scelerisque nulla. Pellentesque mollis tellus ut arcu malesuada auctor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Ut tristique purus non ultricies vulputate"
text = textwrap.fill(userinput, 50)

# Font size, color and type.
fontcolor = (255, 255, 255)
fontsize = 40
font = ImageFont.truetype("/System/Library/Fonts/AppleGothic.ttf", fontsize)

# Determine text size using a scratch image.
img = Image.new("RGBA", (1,1))
draw = ImageDraw.Draw(img)
textsize = draw.textsize(text, font)

# Now that we know how big it should be, create
# the final image and put the text on it.
background = (200, 255, 0)
img = Image.new("RGBA", textsize, background)
draw = ImageDraw.Draw(img)
draw.text((0, 0), text, fontcolor, font)

img.show()
img.save("seesharp.png")

关于python - 在 Pillow 中确定带有换行符的字符串的像素高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33126308/

相关文章:

python - 如何使用检查模块检查可调用对象是否异步? - Python

python - 将SQLalchemy查询结果传递给scrapy中的start_urls

python - "rank"的 Numpy/scipy 弃用警告

python-3.x - 在 Python 中将 RGBA 转换为 RGB

python - 使用 python 和 PIL 如何抓取图像中的文本 block ?

python - 字典理解中的嵌套循环

python - future 警告 : Method . ptp

python - 从 headless Pygame 实例获取图像

python - IO错误 : [Errno 2] No such file or directory: 'data.json'

python-2.7 - Tkinter : _tkinter. TclError 的 Python 3 错误:未知选项 "-padding"