【问题标题】:How to use MagickImage/Wand to create an image comprised of equally spaced bordered boxes of text in Python如何使用 MagickImage/Wand 在 Python 中创建由等距边框文本框组成的图像
【发布时间】:2026-01-27 20:45:02
【问题描述】:

我需要制作一个如下所示的图像:

为此,我实现了 MagickImage/Wand 的使用。这是我目前的实现

import re
from unicodedata import normalize

from docx import Document
from wand.image import Image
from wand.drawing import Drawing
from wand.font import Font

doc = Document("P.docx")
docText = []
for para in doc.paragraphs:
    docText.append(para.text)
fullText = "\n".join(docText)

ct = 242


def get(source, begin, end):
    try:
        start = source.index(len(begin)) + len(begin)
        finish = source.index(len(end), len(start))
        return source[start:finish]
    except ValueError:
        return ""


def capitalize(string):
    cap = ("".join(j[0].upper() + j[1:]) for j in string)
    return cap


def find_matches(text):
    return capitalize(
        [
            m
            for m in re.findall(
                r"^[^0-9]\s+([^.;]+\s*)+[.;]+", normalize("NFKD", text), re.MULTILINE
            )
        ]
    )

with Image(width=300, height=300, psuedo='xc:black') as canvas:
    left, top, width, height = 50, 10, 100, 150
    for match in find_matches(text=fullText):
        ct += 1
        match_words = match.split(" ")
        match = " ".join(match_words[:-1])
        with Drawing() as context:
            context.fill_color = 'white'
            context.rectangle(left=left, top=top, width=width, height=height)
            canvas.font = Font('/System/Library/Fonts/arial.ttf')
            context(canvas)
            canvas.caption(match + '\r' + 'ct', left=left, top=top, width=width, height=height, gravity='center')
        canvas.save(filename='patdrawTest.png')

我不太确定如何使用此工具创建边框或如何正确间隔,因此,这是我当前的输出:

我知道我需要一个可迭代的基础映像。我也明白我需要标志来跟踪高度/宽度/等。之前的文本块(除非使用此工具有更简单的方法)。但是,我的代码目前的工作方式是它从 word 文档中获取单词,解析它以获得特定的匹配,然后 supposed 将它放入一个图像中,就像我上面显示的第一张图像一样.然而,我不知所措。任何帮助将不胜感激。

【问题讨论】:

  • *.com/a/67982791/2836621 的相关问题中,我未设置height,以便ImageMagick 将标题设置为300px 宽,无论多高都是必要的。我不确定您将如何使用 wand caption() 方法执行此操作。也许@emcconville 会知道?

标签: python image imagemagick wand


【解决方案1】:

这是我为了制作等间距的文本框而编写的代码。

import re
from unicodedata import normalize

from docx import Document
from wand.image import Image
from wand.drawing import Drawing
from wand.font import Font

doc = Document("P.docx")
docText = []
for para in doc.paragraphs:
    docText.append(para.text)
fullText = "\n".join(docText)

ct = 242


def get(source, begin, end):
    try:
        start = source.index(len(begin)) + len(begin)
        finish = source.index(len(end), len(start))
        return source[start:finish]
    except ValueError:
        return ""


def capitalize(string):
    cap = ("".join(j[0].upper() + j[1:]) for j in string)
    return cap


def find_matches(text):
    return capitalize(
        [
            m
            for m in re.findall(
                r"^[^0-9]\s+([^.;]+\s*)+[.;]+", normalize("NFKD", text), re.MULTILINE
            )
        ]
    )


with Image(width=400, height=1000, pseudo='xc:white') as canvas:
    left, top, width, height = 2, 2, 395, 131
    for match in find_matches(text=fullText):
        ct += 1
        match_words = match.split(" ")
        match = " ".join(match_words[:-1])
        with Drawing() as context:
            context.fill_color = 'black'
            context.rectangle(left=left, top=top, width=width, height=height)
            context.fill_color = 'white'
            context.rectangle(left=(left+2), top=(top+2), width=(width-4), height=(height-4))
            canvas.font = Font('/System/Library/Fonts/timesnewroman.ttf')
            context(canvas)
            canvas.caption(match + '\n' + str(ct), left=(left+5), top=top, width=(width-10), height=height,
                           gravity='center')
        top += 135
    canvas.crop(bottom=top)
    canvas.save(filename='patdrawTest.png')

下面是这段代码的输出:

不过,我仍然有一些我想解决的问题。虽然文本框都是等距的并且看起来相当不错,但我仍然希望所有文本看起来都一样;那是相同的字体大小,唯一的方法是让边框等自动重新调整大小,使其能够以这种方式工作。我不知道如何做到这一点,但现在是这个,如果其他人遇到这样的事情。

【讨论】:

    最近更新 更多