使用PIL打开多张图片

3

我正在使用Python笔记本。其中,我有创建图像的代码,并且正在使用image.show()进行调试。但是,这样做的缺点是,我无法为图像命名,因为它具有临时文件名。然后,我使用image.save("name.png")以不同的名称保存图像,然后使用Image.open("name.png")来打开它。然而,当我这样做时,实际上只打开了最后一个图像。我需要做些什么才能调用Image.open打开多个图像并将它们全部打开?例如,如果我执行以下操作:

image = Image.fromarray( ... )
image.save("original.png")
Image.open("original.png")

image = Image.fromarray( ... )
image.save("reconstruction.png")
Image.open("reconstruction.png")

仅"reconstruction.png"显示。

如果我在它们后面使用Image.show()

image = Image.fromarray( ... )
image.show()

image = Image.fromarray( ... )
image.show()

它可以工作,但他们会有暂时的名称,这些名称毫无意义,如果我最终拥有7-8个打开的图像,我希望有一种简单的方法来跟踪每个图像。


为什么不能使用 Image.open(temporary_name)?你的问题不清楚。请展示代码。 - furas
我可以使用Image.show(),它会打开一个带有图像的窗口,但是临时名称没有任何意义。我想要一个有意义的临时名称,这样如果我最终有7-8张图片,我就可以理解它们,而不必跟踪哪个是哪个。 - user
当你在编程中打开一个文件时,你需要将其赋值给某个变量,以便稍后使用。根据你提供的代码,我不知道你如何让show函数工作起来的。 - Mark Ransom
我只是使用了 image = Image.fromarray( tile_raster_images( Train, img_shape=(28, 28), tile_shape=(10, 10), tile_spacing=(1, 1) ) ) image.show() - user
2个回答

2

Image.show() 方法主要用于调试:

显示此图像。此方法主要用于调试目的。

在Unix平台上,此方法将图像保存到临时PPM文件中,并调用xv实用程序。

在Windows上,它将图像保存到临时BMP文件中,并使用标准BMP显示实用程序显示它(通常为Paint)。

您可以提供一个 title 参数,但在Windows上不会显示:

title - 可选标题,用于尽可能使用图像窗口。

您可以使用不同的图像变量名称来跟踪它们在Python内部的情况:

image1 = Image.fromarray( ... )
image1.save("original.png")
Image1.open("original.png")

image2 = Image.fromarray( ... )
image2.save("reconstruction.png")
Image2.open("reconstruction.png")

image1.show("image1")
image2.show("image2")

如果.show主要用于调试,那么打开图像的推荐方法是什么? - freshWoWer
如果您不介意临时文件或Windows上的标题限制,可以使用Image.show(),否则请将其保存到文件并使用其他方式打开。 - Hugo

0

我在我的IPython/Jupyter/Colab笔记本中使用的另一种方法是:

import requests
from IPython.display import Image
from IPython.display import display

img0 = Image("img0_path", width = 140) 
img1 = Image("img1_path", width = 140) 
img2 = Image("img2_path", width = 140) 
img3 = Image("img3_path", width = 140) 

display(img0,img1,img2,img3)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接