Display multiple images in one IPython Notebook cell?

后端 未结 10 1422
面向向阳花
面向向阳花 2020-12-12 12:26

If I have multiple images (loaded as NumPy arrays) how can I display the in one IPython Notebook cell?

I know that I can use plt.imshow(ima) to display

相关标签:
10条回答
  • 2020-12-12 13:11

    This is easier and works:

    from IPython.display import Image
    from IPython.display import display
    x = Image(filename='1.png') 
    y = Image(filename='2.png') 
    display(x, y)
    
    0 讨论(0)
  • 2020-12-12 13:12

    Somehow related to this question (and since I was directed to this answer when I was trying to solve it), I was able to solve a similar problem by simply typing the full file-path when calling Image(). In my case, I had to choose a random image from different folder paths stored in a list your_folder and display them.

    import random, os 
    for i in range(len(your_folder)):
       ra1 = "../"+your_folder[i]+"/"+random.choice(os.listdir(your_folder[i]))
       image = Image(ra1)
       display(image)
    
    0 讨论(0)
  • 2020-12-12 13:13

    from matplotlib.pyplot import figure, imshow, axis
    from matplotlib.image import imread
    
    mypath='.'
    hSize = 5
    wSize = 5
    col = 4
    
    def showImagesMatrix(list_of_files, col=10):
        fig = figure( figsize=(wSize, hSize))
        number_of_files = len(list_of_files)
        row = number_of_files/col
        if (number_of_files%col != 0):
            row += 1
        for i in range(number_of_files):
            a=fig.add_subplot(row,col,i+1)
            image = imread(mypath+'/'+list_of_files[i])
            imshow(image,cmap='Greys_r')
            axis('off')
    
    showImagesMatrix(listOfImages,col)
    

    based on @Michael answer

    0 讨论(0)
  • 2020-12-12 13:17

    The answers in this thread helped me: Combine several images horizontally with Python

    The problem of using matplotlib was the displayed images' definition was really bad. I adapted one of the answers there to my needs:

    The following code displays the images concatenated horizontaly in a jupyter notebook. Notice the commented line with the code to save the image if you'd like that.

    import numpy as np
    import PIL
    from IPython.display import display
    
    list_im = ['Test1.jpg', 'Test2.jpg', 'Test3.jpg']
    imgs    = [ PIL.Image.open(i) for i in list_im ]
    # pick the image which is the smallest, and resize the others to match it (can be arbitrary image shape here)
    min_shape = sorted( [(np.sum(i.size), i.size ) for i in imgs])[0][1]
    imgs_comb = np.hstack( (np.asarray( i.resize(min_shape) ) for i in imgs ) )
    
    # save that beautiful picture
    imgs_comb = PIL.Image.fromarray( imgs_comb)
    #imgs_comb.save( 'combo.jpg' )    
    
    display(imgs_comb)
    
    0 讨论(0)
提交回复
热议问题