【问题标题】:Display list of Python PIL.Image.Image as grid of images in Jupyter Notebook在 Jupyter Notebook 中将 Python PIL.Image.Image 列表显示为图像网格
【发布时间】:2018-06-29 14:27:11
【问题描述】:

我有一个名为all_images 的 PIL.Image.Image 对象的 Python 列表,如下所示:

[<PIL.Image.Image image mode=RGB size=192x256 at 0x10855CF8>,
 <PIL.Image.Image image mode=RGB size=192x256 at 0x1053AE10>,
 <PIL.Image.Image image mode=RGB size=192x256 at 0x106445C0>,
 <PIL.Image.Image image mode=RGB size=192x256 at 0x10644E48>,
 <PIL.Image.Image image mode=RGB size=192x256 at 0x10644BE0>,
 <PIL.Image.Image image mode=RGB size=192x256 at 0x10644C18>,
 <PIL.Image.Image image mode=RGB size=192x256 at 0x106447F0>,
 <PIL.Image.Image image mode=RGB size=192x256 at 0x146D32B0>,
 <PIL.Image.Image image mode=RGB size=192x256 at 0x146D37F0>,
 <PIL.Image.Image image mode=RGB size=192x256 at 0x146D3EB8>,
 <PIL.Image.Image image mode=RGB size=192x256 at 0x146D3668>]

我想将它们全部显示在一个漂亮的网格中,就像在这个tutorial 中一样。在教程中,他只是将图像保存到磁盘;我想在 Jupyter Notebook 中显示它们。

当我执行Image.open(all_images[0]) 时,出于某种原因,它会在 Photoshop 中打开。我也试过了

from IPython.display import Image 
Image(all_images[0])

PIL.Image.Image 是无效输入。

【问题讨论】:

  • 您要保存图像网格,还是显示它们?
  • 只显示它们
  • 我知道matplotlib 可以在 Jupyter Notebook 中显示图像。 Here's 另一个 StackOverflow 帖子可能会帮助您使用该软件包。

标签: python jupyter-notebook python-imaging-library


【解决方案1】:

您可以使用IPyPlot package 来节省一些麻烦。 使用 IPyPlot,您可以使用如下简单的方式显示图像(numpy.arrays、PIL 对象或简单的 URI):

import ipyplot

ipyplot.plot_images(images_list)

【讨论】:

    【解决方案2】:

    您可以创建自己的Image 对象网格,将其保存为.png.jpeg/.jpg,然后使用另一个包显示它。

    使用PIL,您可以创建一个新的Image 对象并将各个Image 对象粘贴到它上面。例如,假设您想要一个 4x4 网格。您创建了一个新的 Image width=4*width_of_smaller_imageheight=4*height_of_smaller_image。然后,在适当的像素位置上循环,pasteall_images 到它。

    【讨论】:

    • 如果您对此感兴趣,请告诉我,我可以提供一些示例代码。
    猜你喜欢
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多