【问题标题】:How to display the images side by side in jupyter notebook如何在 jupyter notebook 中并排显示图像
【发布时间】:2018-12-21 06:29:54
【问题描述】:

我使用了以下代码,但它是垂直显示图像。我希望它们在 Jupyter Notebook 中并排显示。

display(Image.open(BytesIO(Item[iii][b'imgs'])))
display(Image.open(BytesIO(Item[jjj][b'imgs'])))

我已尝试使用此代码

display(HTML("<table><tr><td>display(Image.open(BytesIO(Item[jjj][b'imgs'])))) 

但它显示文本display(Image.open(BytesIO(Item[jjj][b'imgs']))))

【问题讨论】:

  • 您是否也尝试过使用 html 布局?显示方式也支持html。
  • 创建一个新图像,其中两个图像并排放置。
  • 是的,我已经尝试使用此代码 display(HTML("
    display(Image.open(BytesIO(Item[jjj][b'imgs']))) ) 但它是显示文本 . "display(Image.open(BytesIO(Item[jjj][b'imgs'])))) "

标签: python jupyter-notebook


【解决方案1】:

您可以使用子图。

import pylab 
pylab.subplot(1, 2, 1)
pylab.plot(range(10))
pylab.subplot(1, 2, 2)
pylab.plot(range(20))

【讨论】:

    【解决方案2】:

    使用 ipywidgets 的布局功能可以做到这一点

    import ipywidgets as widgets
    import IPython.display as display
    ## Read images from file (because this is binary, maybe you can find how to use ByteIO) but this is more easy
    img1 = open('image1.jpeg', 'rb').read()
    img2 = open('image2.jpeg', 'rb').read()
    ## Create image widgets. You can use layout of ipywidgets only with widgets.
    ## Set image variable, image format and dimension.
    wi1 = widgets.Image(value=img1, format='png', width=300, height=400)
    wi2 = widgets.Image(value=img2, format='png', width=300, height=400)
    ## Side by side thanks to HBox widgets
    sidebyside = widgets.HBox([wi1, wi2])
    ## Finally, show.
    display.display(sidebyside)
    

    【讨论】:

    • 感谢您的回答。但是我无法修改图像的高度和宽度。知道是什么原因造成的吗?
    • 没问题,不需要审美。我想要大小相同的小部件图像。
    • pyplot 的有趣替代品。另请参阅custom package IPyPlot
    【解决方案3】:

    并排显示两个图像

    import matplotlib.pyplot as plt
    img1 = plt.imread('<PATH_TO_IMG1>')
    img2 = plt.imread('<PATH_TO_IMG2>')
    
    NUM_ROWS = 1
    IMGs_IN_ROW = 2
    f, ax = plt.subplots(NUM_ROWS, IMGs_IN_ROW, figsize=(16,6))
    
    ax[0].imshow(img1)
    ax[1].imshow(img2)
    
    ax[0].set_title('image 1')
    ax[1].set_title('image 2')
    
    title = 'side by side view of images'
    f.suptitle(title, fontsize=16)
    plt.tight_layout()
    plt.show()
    

    【讨论】:

    • 隐藏刻度和标签对于图像来说会更好,并确保像素比不改变也是一个好主意。
    【解决方案4】:

    您可以稍微修改上面的代码以显示图像网格。

    import matplotlib.pyplot as plt
    img1 = plt.imread('<PATH_TO_IMG1>')
    img2 = plt.imread('<PATH_TO_IMG2>')
    images = [img1, img2]*3
    
    NUM_ROWS = 2
    IMGs_IN_ROW = 3
    
    f, ax_arr = plt.subplots(NUM_ROWS, IMGs_IN_ROW, figsize=(16,8))
    
    for j, row in enumerate(ax_arr):
        for i, ax in enumerate(row):
            ax.imshow(images[j*IMGs_IN_ROW+i])
            ax.set_title(f'image {j*IMGs_IN_ROW+i+1}')
    
    title = 'displaying images matrix'
    f.suptitle(title, fontsize=16)
    plt.show()  
    

    【讨论】:

    • 最初的答案已经是一个子图网格,但它只有一行。如果您填充 2D 网格,您不知道是否有足够的图像来填充所有网格单元。你需要做什么来隐藏空单元格也会很有趣。
    猜你喜欢
    • 2018-11-06
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 2020-01-19
    相关资源
    最近更新 更多