python - 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
我完全不知所措,无法弄清楚。任何帮助都会很棒。
解决方案
推荐阅读
- nlp - 删除 NLTK 停用词
- php - 用PHP上传和显示图片
- spring-boot - 在 Spring Boot 日志记录控制台的开始/结束处需要方括号
- python - 计算空行和列
- servicestack - ServiceStack 结构化日志记录
- java - 如何显示来自另一个类的新 JPanel?
- google-apps-script - 调用 setValues() 时日期更改
- python - pip uninstall tensorflow 抛出 _csv.Error: line contains NUL
- reactjs - 渲染的组件然后消失(react/redux/Firebase)
- oracle - 使用游标在 Oracle SQL 中执行 2 个 for 循环