魔杖 - 文本包装 - 基于图像文件大小

时间:2015-12-29 19:28:47

标签: python python-3.x wand magickwand

我正在尝试在图像上写文字。但文字溢出。有没有办法将这些文字内容包装到图像中?

from wand.image import Image
from wand.drawing import Drawing
from wand.color import Color

with Image(filename='/home/path/image.jpg').clone() as img:
    print('width =', img.width)
    print('height =', img.height)
    quote = input('quote:')
    with Drawing() as draw:
        draw.font = 'wandtests/assets/League_Gothic.otf'
        draw.fill_color=Color('red')
        draw.text_alignment= 'center'
        img.font_size=50
        draw.text(int(img.width/2),int(img.height/2), quote)
        print(draw.get_font_metrics(img,quote))
        draw(img)
        print('text drawn')
    img.save(filename='/home/path/image01.jpg')
给出的输入是:

"Success makes so many people hate you. I wish it wasn't that way. It would be wonderful to enjoy success without seeing envy in the eyes of those around you."

output image

2 个答案:

答案 0 :(得分:2)

使用Image继承自BaseImage的{​​{3}}方法,而不是绘制文字。

答案 1 :(得分:0)

快速而肮脏的解决方案是将字符串分成两部分:

draw.text(int(img.width/3),int(img.height/3), quote[0:len(quote)//2])
draw.text(int(2 * img.width/3),int(2 * img.height/3), quote[len(quote)//2:-1])

如果文字仍然很长,可能需要分成3个或更多。