python - 在 Wand 中为文本动画构建框架

标签 python wand

我正在尝试在 Python 上的 Wand 中编写一个自动化脚本,通过一次一个字母地编写图像标题来构建文本动画的框架。

问题是,当我使用标题命令(此处的文档 http://docs.wand-py.org/en/0.4.4/wand/image.html )写一个字母时,它会写一个巨大的字母,而当我写整个文本时,它会很好地适合图像。

我想到了一个可能的解决方案:将第一个字母写成彩色,其余字母透明,然后循环执行,但是据我所知,标题命令无法执行彩色文本。

如果有人可以建议我另一种选择,我将不胜感激。我可以使用draw.text,但是据我所知,它不会自动计算何时进入下一行...

我的代码如下所示:

imgname = random.choice(os.listdir('/home/gionny/Downloads/HighResImg'))
text = 'Hello, world! This is a slightly longer sentence.'
fontname = random.choice(os.listdir('/home/gionny/Downloads/font'))
with Image(filename='HighResImg/'+imgname) as i:    
    font = Font(path = 'font/'+fontname, color = Color('#fff'))
    textWidth = i.width*2/3
    textHeight = i.height*2/3
    offsetLeft = (i.width - textWidth)/2
    offsetTop = (i.height - textHeight)/2
    with Image(filename='logo.gif') as l:
        l.resize(80,80)
        l.transparentize(0.7)
        with Drawing() as draw:
            draw.composite(operator='atop', left=i.width-90, top=i.height-90, width=l.width, height=l.height, image=l)
            for c in range(0, len(text)):
                caption = i.caption(text = text[c], left = offsetLeft, top = offsetTop, width=textWidth, height=textHeight, font = font, gravity = 'center')
                print(caption)
                cl = i.clone()
                cl.format = 'jpeg'
                cl.save(filename='Text/text'+str(c)+'.jpg')
                cl.destroy()

最佳答案

If someone could suggest me another option I would be grateful. I could use draw.text, however that doesn't automatically calculate when to go on the next line as far as I know...

没有快速解决方法,您负责计算每次迭代的 x,y 坐标。 尤其当使用随机提取的混合字体时。

已经为此类事情提供了方法wand.drawing.Drawing.get_font_metrics。只需保留一个累加器并在每次迭代时更新即可。

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


with Image(width=400, height=250, background=Color("skyblue")) as background:
    leftOffset = 35  # <= Starting position.
    topOffset = background.height/2;
    for letter in "Hello World":
        with Drawing() as ctx:
            ctx.font = "TimesNewRoman"
            ctx.font_size = 64.0
            metrics = ctx.get_font_metrics(background, letter)
            ctx.text(leftOffset, int(topOffset+metrics.text_height/4), letter)
            with Image(width=background.width,
                       height=background.height,
                       background=Color("transparent")) as frame:
                ctx.draw(frame)
                background.sequence.append(frame)
            leftOffset += int(metrics.text_width)  # <= Adjust for next iteration.
    background.save(filename="output.gif")

output.gif

现在,为了重复下一行过程,如果 leftOffset 大于 Canvas 宽度,只需将 topOffset 增加字体规范 text_height 即可。

关于python - 在 Wand 中为文本动画构建框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48010090/

相关文章:

重定向到文件时 Python 日志出现较晚

python - 无法在 ARM 处理器上运行 Python3 HTTPServer

Python 脚本按流使用 Azure 存储 Blob 中的数据,并按流更新 Blob,无需本地文件读取和上传

python - Imagemagick 魔杖使用深度不像命令行那样工作

python魔杖错误 "wand.resource.DestroyedResourceError: <wand.image.Image: (closed)> is destroyed already"

python - 如何将convert命令更改为python代码

python - 将特定日期作为默认日期时间添加到 Django 中的 DateTimeField

python - 是否可以从 ObjC 调用 Python 模块?

python - 将openCV numpy数组转换为Wand Image格式

python - 定义形态:compose for merging results of a multi-morphology kernel using Wand