如何在 Jupyter/IPython 中显示包含 PNG 图像的 BytesIO

在 IPython 中,你可以使用 Image() 函数显示图像:

display_bytesio_image.py
from IPython.display import Image
Image(filename="img1.png")

如果你没有文件中的 PNG 数据而是在 BytesIO 中怎么办?

使用 .getvalue()

display_bytesio_fromvalue.py
from IPython.display import Image

my_bio = ... # 在此插入你的代码
Image(my_bio.getvalue())

Check out similar posts by category: Python