首页 > 解决方案 > ImageMagick 的类型错误

问题描述

当我尝试运行以下代码时出现 TypeError:

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 = 10, 10, 10, 10
    for match in find_matches(text=fullText):
        ct += 1
        match_words = match.split(" ")
        match = " ".join(match_words[:-1]) + 'ct'
        with Drawing() as context:
            context.fill_color = 'white'
            context.rectangle(left=left, top=top, width=width, height=height)
            font = Font('/System/Library/Fonts/arial.ttf')
            context(canvas)
            canvas.caption(match)
        canvas.save(filename='patdrawTest.png')

确切的错误如下:

 Traceback (most recent call last):
  File "C:\Users\crazy\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\wand\image.py", line 3221, in caption
    raise TypeError()
TypeError
 
 During handling of the above exception, another exception occurred:
 
 Traceback (most recent call last):   File
 "C:/Users/crazy/PycharmProjects/Patents/PatDraw.py", line 54, in
 <module>
     canvas.caption(match)   File "C:\Users\crazy\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\wand\image.py",
 line 1061, in wrapped
     result = function(self, *args, **kwargs)   File "C:\Users\crazy\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\wand\image.py",
 line 3223, in caption
     raise TypeError('font must be specified or existing in image') TypeError: font must be specified or existing in image

我完全不知所措,无法弄清楚。任何帮助都会很棒。

标签: pythonimagemagicktypeerror

解决方案


推荐阅读