如何使用纯 python(而非 ipython)查看或保存 <IPython.core.display.Image object>?

How to view or save an <IPython.core.display.Image object> using plain python (not ipython)?

在学习dask的过程中,我尝试使用"visualize" method/function生成程序图可视化。但是,我没有使用笔记本。据我所知,除了 IPython.core.display.Image 对象之外,没有办法让 dask 将这些图形输出到任何其他对象,我不知道如何在常规 python 中查看。由于各种原因,使用 IPython.

运行 我的代码是不切实际的

有什么方法可以让这些对象正常显示吗Pythonscript/shell?或者至少,将它们保存到磁盘上的标准图像文件中?

谢谢!

.visualize 方法允许您指定要输出到的文件名,以及要传递给的其他参数 dot/graphviz:

d.visualize(filename='dask.pdf')

生成 PDF 文件输出,而不是尝试内联表示。支持各种其他图形格式,例如 PNG(尽管它可能取决于您安装 graphviz 的方式)。

-编辑-

您还可以从 Image 实例中提取图像字节

im = d.visualize()
open('output.png', 'wb').write(im.data)

这将是 PNG 格式(也由 im.format 给出)。