【发布时间】: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 宽,无论多高都是必要的。我不确定您将如何使用 wandcaption()方法执行此操作。也许@emcconville 会知道?
标签: python image imagemagick wand