【问题标题】:Image creation with Wand in Python在 Python 中使用 Wand 创建图像
【发布时间】:2021-09-25 01:21:36
【问题描述】:

我正在进行的项目的目标是从 word 文档中获取内容,然后输出一个包含带边框的框的单个图像,其中包含我通过代码自动汇集的文本片段.这是我需要的示例:

由于我自己的一点点知识和其他人的大量知识,我已经为这个项目完成了 90% 的步法。我的代码已经从 word 文档中获取文本并自动获取我想要的内容。尝试模仿此图像时会出现问题。我正在使用 Wand 创建图像,但我仍然无法让它正常工作。我知道我很接近,但我完全不确定我错过了什么。以下是严格的图像创建代码:

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

target_width = 500
target_height = 0
y_offset = 0
y_padding = 4
x_padding = 5

with Image(width=2000, height=2000, pseudo='xc:white') as img:
    for match in find_matches(text=fullText):
        ct += 1
        with Drawing() as ctx:
            ctx.font_size = 20
            ctx.text_alignment = 'center'
            words = match.split(" ")
            words.append("\n" + str(ct))
            word_count = len(words)
            while True:
                temp_text = rebuild_text(words, word_count)
                metrics = ctx.get_font_metrics(img, temp_text, multiline=True)
                if metrics.text_width > target_width:
                    word_count -= 1
                else:
                    text = temp_text
                    target_height = int(metrics.text_height + 0.5)
                    break
            ctx.push()
            ctx.fill_color = 'white'
            ctx.stroke_width = 3
            ctx.stroke_color = 'black'
            ctx.rectangle(2, y_offset + y_padding, width=2*x_padding+target_width,
                          height=6*y_padding+target_height)
            ctx.pop()
            ctx.text(x_padding + (target_width // 2), 16 + 6*y_padding+y_offset, text)
            ctx(img)
            y_offset = target_height + 100*y_padding + 7
    img.trim()
    img.save(filename='patdrawdemoTest.png')

我一遍又一遍地尝试弄乱此代码,但无济于事。我知道我想要的所有文本都在通过,因为我添加了一个打印语句来确保这一点,并且在到处乱七八糟的代码之后,有时我发现有很多重叠的文本,但我永远不能得到超过三个有边框的盒子。以下是我通过更改此处和此处的值可以获得的输出示例:

我无法复制文本重叠输出,但是,以上只是一些示例。我不确定它是否有帮助,但它们之间的区别在于文本和矩形等与 y 轴相关的值。

我需要的文本行是用 python 中的 docx 库获取的,然后放入变量中;这不是一个简单的字符串。此外,此代码必须适用于任何场合;无论是 5 个文本框、2 个、8 个还是 100 个文本框,它都必须创建具有那么多文本框的图像。以下是我已解析并通过我上面的代码传递的文本:

在第一区域中存储与交通工具的运行方式相关的第一数据 243

在第二区域中存储与交通工具的操作方式相关的第二数据
244

根据综合能源消耗效率修改运输功能
245

确定组合的能源消耗效率,其中该确定包括对等组之间的区块链共识,该对等组包括传输、服务器和至少一种其他传输中的一项或多项 246

根据区块链共识执行智能合约以记录区块链上的综合能源消耗效率
247

我将非常感谢我能得到的任何帮助,因为我完全被难住了。请让我知道我是否可以更清楚,或者我是否缺少一些东西来帮助您回答我的问题。谢谢。

【问题讨论】:

    标签: python image image-processing wand magickwand


    【解决方案1】:

    @emconnville 在我发布这个问题之前在另一篇文章中给出了我需要的答案,但是有一个错误让我花了很长时间才看到,但这就是我的问题的答案:

    def to_chunks(words, size):
        for idx in range(0, len(words), size):
            yield words[idx:idx + size]
    
    
    def rebuild_text(words, size, ct):
        ret = "\n".join([" ".join(w) for w in to_chunks(words, size)])
        ret += "\n" + ct
        return ret
    
    
    target_width = 375
    target_height = 0
    y_offset = 0
    y_padding = 5
    x_padding = 6
    
    with Image(width=2000, height=5000, pseudo='xc:white') as img:
        for match in find_matches(text=fullText):
            ct += 1
            with Drawing() as ctx:
                ctx.font_size = 20
                ctx.text_alignment = 'center'
                words = match.split(" ")
                word_count = len(words)
                while True:
                    temp_text = rebuild_text(words, word_count, str(ct))
                    metrics = ctx.get_font_metrics(img, temp_text, multiline=True)
                    if metrics.text_width > target_width:
                        word_count -= 1
                    else:
                        text = temp_text
                        target_height = int(metrics.text_height + 1)
                        break
                ctx.push()
                ctx.fill_color = 'white'
                ctx.stroke_width = 3
                ctx.stroke_color = 'black'
                ctx.rectangle(2, y_offset + y_padding, width=2*x_padding+target_width,
                              height=2*y_padding+target_height)
                ctx.pop()
                ctx.text(x_padding+2 + (target_width // 2), 10 + 4*y_padding+y_offset, text)
                ctx(img)
                y_offset += target_height + 4*y_padding - 2
        img.trim()
        img.save(filename='patdrawdemoTest.png')
    

    错误出现在这个特定的代码块中:

            ctx.text(x_padding+2 + (target_width // 2), 10 + 4*y_padding+y_offset, text)
            ctx(img)
            y_offset += target_height + 4*y_padding - 2
    img.trim()
    img.save(filename='patdrawdemoTest.png')
    

    “y_offset += target_height + 4*y_padding - 2”最初只是“y_offset = target...”所以...基本上我只是错过了 +=

    我希望我不是瞎子。

    【讨论】:

      猜你喜欢
      • 2013-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      • 2013-04-15
      相关资源
      最近更新 更多