How do I make 2 images appear side by side in Jupyter notebook (iPython)?

Question:

I want to display 2 PNG images in iPython side by side.

My code to do this is:

from IPython.display import Image, HTML, display

img_A = 'pathtoimg_A.png'
img_B = 'pathtoimg_B.png'

display(HTML("<table><tr><td><img src=img_A></td><td><img src=img_B></td></tr></table>"))

But it doesn’t output the images, and instead only displays placeholders for the 2 images:

enter image description here

I tried the following as well:

s = """<table>
<tr>
<th><img src="%s"/></th>
<th><img src="%s"/></th>
</tr></table>"""%(img_A, img_B)
t=HTML(s)
display(t)

But the result is the same:

enter image description here

The images are in the path for sure, because I verified by displaying them in a pop up:

plt.imshow(img_A)
plt.imshow(img_B)

and they do appear in the pop ups.

How do I make the 2 images appear side by side in iPython?

Asked By: Kristada673

||

Answers:

You can try using matplotlib. You can read image to numpy array by using mpimg.imread (documentation) from matplotlib, then you can use subplots (documentation) and for creating two columns for figures and finally imshow (documetation) to display images.

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from matplotlib import rcParams

%matplotlib inline

# figure size in inches optional
rcParams['figure.figsize'] = 11 ,8

# read images
img_A = mpimg.imread('pathtoimg_A.png')
img_B = mpimg.imread('pathtoimg_B.png')

# display images
fig, ax = plt.subplots(1,2)
ax[0].imshow(img_A)
ax[1].imshow(img_B)
Answered By: niraj

matplotlib is a very good tool for plotting but I found it very heavy and slow for scenarios where I simply need a fast and easy way to display bigger number of images.
To solve this I’m using IPyPlot package:

import ipyplot

ipyplot.plot_images(images_list, max_images=20, img_width=150)

You would get a plot similar to this:
enter image description here

Answered By: Karol Żak

This easy solution worked great for me:

from IPython.display import Video, Image, HTML, display

image_path1 = "/myfolder/my_img1.jpg"
image_path2 = "/myfolder/my_img2.jpg"


HTML(f"""
    <div class="row">
            <img src={image_path1} style="width:30%"> </img>
            <img src={image_path1} style="width:53.2%"> </img>
    </div>
    """)

I put in different widths for if you have a portrait and a landscape picture, but that is up to you depending on the images and the aspect ratios.

Answered By: n4321d

Kind of late but i found out about this over here

You can do it via Hbox.
Hbox is a special container where you can add widgets. It aims at providing an efficient way to lay out, align and distribute space among items in a given space. Although its a bit cumbersome to define so many elements just to display 2 images you can add a lot more functionality like sliders, dropdown menus as well as buttons making your Jupiter notebook more interactive.

import IPython.display as display
import ipywidgets as widgets

img1=open('path_to_image','rb').read()
wi1 = widgets.Image(value=img1, format='jpg', width=300, height=400)
img2=open('path_to_image','rb').read()
wi2 = widgets.Image(value=img2, format='jpg', width=300, height=400)
a=[wi1,wi2]
wid=widgets.HBox(a)
display.display(wid)
Answered By: Shreyas H.V

For some guys who may want to place an image and a text snippet side by side(adapted from this answer):

import base64
from IPython.display import HTML, display
b64_img =  base64.b64encode(open('./path', 'rb').read()).decode('ascii')
txt = "sjfa;nakd;fnsdfa"
display(HTML(f"""
    <div class="row">
    <img style="float:left;margin-right:30px;" src="data:image/jpeg;base64,{b64_img}" width="400" height="300" />
    <div style="font-size:10px;white-space:pre;">{txt}</div>
    </div>
    """))
Answered By: Lerner Zhang

It should be like this:

from IPython.display import Image, HTML, display

img_A = 'pathtoimg_A.png'
img_B = 'pathtoimg_B.png'

display(HTML("<table><tr><td><img src={0}></td><td><img src={1}></td></tr></table>".format(image_A,img_B)))

You mistook the variable name as variable itself.

Answered By: Benjamin Zhang