首页 > 解决方案 > 尝试显示简单的空图像时出现 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'

它有效。我在这里错过了什么吗?这是显示简单图像的预期方式吗?

标签: python-3.xwand

解决方案


我在这里错过了什么吗?这是显示简单图像的预期方式吗?

您提出的解决方案是正确的。对于 Windows / macOS,假定您没有运行 X11 服务器。将图像写入磁盘并要求操作系统在默认图像查看器中显示它要容易得多。

问题

创建画布或伪图像格式(PLASMA:例如)时,内部“图像信息”和编码器尚未定义。您的简单图像是 CMYK 还是 RGB?调色板、字节顺序等怎么样。ImageMagick 抛出异常是正确的,直到用户至少指定一个编码器/委托来编写。

修复

幸运的是,对于大多数开源项目,一个月前 Wand 报告并修补了这一点(?)。如果未定义图像的编码器(格式), Wand 的display()方法会将 PNG 文件移交给操作系统。

不幸的是,对于大多数开源项目,发布和分发补丁版本可能需要数周时间。最好留意Wand 的发行说明


推荐阅读