Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 765
coin cheung Avatar asked Nov 13 '18 11:11

coin cheung


1 Answers

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

enter image description here


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

enter image description here

like image 144
Mark Setchell Avatar answered Oct 13 '22 00:10

Mark Setchell