简体   繁体   English

使用 PIL 打开多个图像

[英]Open multiple images using PIL

I am using a python notebook.我正在使用 python 笔记本。 In it, I have code that creates images, and am using image.show() for debugging purposes.在其中,我有创建图像的代码,并使用image.show()进行调试。 However, this has the disadvantage that I cannot name the image, since it has a temporary file name.但是,这样做的缺点是我无法命名图像,因为它有一个临时文件名。 I am then using image.save("name.png") to save the images with different names, and then using Image.open("name.png") to open it.然后我使用image.save("name.png")以不同的名称保存图像,然后使用Image.open("name.png")打开它。 However, when I do this, only the last image is actually opened.但是,当我这样做时,实际上只打开了最后一个图像。 What do I need to do so that I can call Image.open on several images and they all open?我需要做什么才能在多个图像上调用 Image.open 并且它们都打开? For example if I do:例如,如果我这样做:

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

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

only "reconstruction.png" shows.只有“reconstruction.png”显示。

If I use Image.show() after each of them如果我在每个之后使用 Image.show()

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

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

it will work, but they will have a temporary name, which is meaningless, and if I end up with 7-8 open images I want an easy way to track what is what.它会工作,但它们会有一个临时名称,这是没有意义的,如果我最终打开 7-8 张图像,我想要一种简单的方法来跟踪什么是什么。

Image.show() is mainly for debugging use: Image.show()主要是调试用的:

Displays this image.显示此图像。 This method is mainly intended for debugging purposes.此方法主要用于调试目的。

On Unix platforms, this method saves the image to a temporary PPM file, and calls the xv utility.在 Unix 平台上,此方法将图像保存到临时 PPM 文件,并调用 xv 实用程序。

On Windows, it saves the image to a temporary BMP file, and uses the standard BMP display utility to show it (usually Paint).在 Windows 上,它将图像保存到一个临时 BMP 文件中,并使用标准的 BMP 显示实用程序来显示它(通常是画图)。

You can give a title parameter, but it doesn't show on Windows:您可以提供title参数,但它不会在 Windows 上显示:

title – Optional title to use for the image window, where possible. title - 用于图像窗口的可选标题,如果可能的话。

You can use different names for image variable to keep track of them inside Python:您可以为图像变量使用不同的名称以在 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")

Also another approach which I used within my IPython/Jupyter/Colab notebook was:我在 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM