Display multiple images in one IPython Notebook cell?

后端 未结 10 1447
面向向阳花
面向向阳花 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: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)
    

提交回复
热议问题