简体   繁体   English

为什么python PIL不能在一个程序中显示两个图像

[英]Why cannot python PIL show two images in one program

Here is my code: 这是我的代码:

img = Image.open('data/img.jpg')
lb = Image.open('data/label.png')
img.show('img')
img.close()
lb.show('lb')
lb.close()

After running this program, the first image is successfully showed, but the second image will not be shown unless I comment the code associated with the first image. 运行此程序后,第一个图像成功显示,但第二个图像将不会显示,除非我评论与第一个图像相关的代码。 What is the cause of this problem. 造成这个问题的原因是什么。

You can multithread to display both at once: 您可以多线程同时显示两者:

#!/usr/local/bin/python3

from PIL import Image
from threading import Thread

def display(im):
    im.show()

im1 = Image.open('1.jpg')
im2 = Image.open('2.jpg')
t1=Thread(target=display,args=(im1,))
t1.start()
t2=Thread(target=display,args=(im2,))
t2.start()

在此输入图像描述


Or you can temporarily concatenate the images into one: 或者您可以暂时将图像连接成一个:

#!/usr/local/bin/python3

from PIL import Image
import numpy as np

im1 = Image.open('1.jpg')
im2 = Image.open('2.jpg')

Image.fromarray(np.hstack((np.array(im1),np.array(im2)))).show()

在此输入图像描述

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

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