简体   繁体   中英

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()

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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