python-3.x - 尝试显示简单的空图像时出现 Python Wand 错误:“此图像格式没有编码委托”
问题描述
我是 Wand 的新手,我尝试了以下非常简单的代码来显示红色图像:
from wand.color import Color
import wand.display
with Image(width=100, height=100, background=Color("red")) as img:
wand.display.display(img)
但我收到了这个错误:
MissingDelegateError Traceback (most recent call last)
<ipython-input-24-6badc0244551> in <module>
1 with Image(width=100, height=100, background=Color("red")) as img:
----> 2 wand.display.display(img)
3
~/dev/.venv/lib/python3.7/site-packages/wand/display.py in display(image, server_name)
60 ext = '.' + image.format.lower()
61 path = tempfile.mktemp(suffix=ext)
---> 62 image.save(filename=path)
63 os.system(('start ' if system == 'Windows' else 'open ') + path)
64 else:
~/dev/.venv/lib/python3.7/site-packages/wand/image.py in save(self, file, filename, adjoin)
8916 r = library.MagickWriteImage(self.wand, filename)
8917 if not r:
-> 8918 self.raise_exception()
8919
8920
~/dev/.venv/lib/python3.7/site-packages/wand/resource.py in raise_exception(self, stacklevel)
228 warnings.warn(e, stacklevel=stacklevel + 1)
229 elif isinstance(e, Exception):
--> 230 raise e
231
232 def make_blob(self, format=None):
MissingDelegateError: no encode delegate for this image format `' @ error/constitute.c/WriteImage/1240
查看堆栈跟踪,我意识到 Wand 在显示之前将图像临时存储在文件中,因此我尝试在调用之前添加以下行display()
:
img.format = 'png'
它有效。我在这里错过了什么吗?这是显示简单图像的预期方式吗?
解决方案
我在这里错过了什么吗?这是显示简单图像的预期方式吗?
您提出的解决方案是正确的。对于 Windows / macOS,wand假定您没有运行 X11 服务器。将图像写入磁盘并要求操作系统在默认图像查看器中显示它要容易得多。
问题
创建画布或伪图像格式(PLASMA:
例如)时,内部“图像信息”和编码器尚未定义。您的简单图像是 CMYK 还是 RGB?调色板、字节顺序等怎么样。ImageMagick 抛出异常是正确的,直到用户至少指定一个编码器/委托来编写。
修复
幸运的是,对于大多数开源项目,一个月前 Wand 报告并修补了这一点(?)。如果未定义图像的编码器(格式), Wand 的display()
方法会将 PNG 文件移交给操作系统。
不幸的是,对于大多数开源项目,发布和分发补丁版本可能需要数周时间。最好留意Wand 的发行说明
推荐阅读
- javascript - 在 React.js 中的另一个组件中从一个组件修改 HTML 元素
- ruby-on-rails - Collection_select 助手无法在 ruby on rails erb 中构建禁用的 html 标记
- php - PHP - fwrite() 不写入套接字
- c# - 您可以将属性值传递到 C# .NET 中 Web 属性中的“特定页面”URL 路径吗
- python - 无法在 Django TCP/IP 连接上运行服务器?
- python - 多码头工人应用程序中的芹菜
- linux - 我只想将目录及其子目录的内容复制到一个目录而不复制目录和子目录
- google-sheets - 如何参考列在工作表之间传输数据?
- asp.net-mvc - 调用所有按钮的 onclick 事件的问题,而不是被点击的按钮
- java - 将参数传递给@AndroidFindBy Annotation Java 中的 xpath