7

I have used the following code but it is displaying the images vertically. I want them to display side by side in a Jupyter Notebook.

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

I have tried using this code

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

but it is display the text display(Image.open(BytesIO(Item[jjj][b'imgs']))))

girlvsdata
  • 1,596
  • 11
  • 21
Harsha
  • 81
  • 1
  • 1
  • 6
  • 1
    Did you try also with html layout? display method also support html. – cosmoscalibur Jul 12 '18 at 23:04
  • Create a new image which has both images inside of it side by side. – Onasafari Jul 12 '18 at 23:14
  • Yeah I have tried using this code display(HTML("
    display(Image.open(BytesIO(Item[jjj][b'imgs'])))) but it is display the text . "display(Image.open(BytesIO(Item[jjj][b'imgs'])))) "
    – Harsha Jul 12 '18 at 23:26

4 Answers4

12

Using layout features of ipywidgets you can do that

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)
cosmoscalibur
  • 1,131
  • 1
  • 8
  • 17
  • Thanks for the answer . However I am unable to modify the height and width of the images. Any idea what might be causing that? – Resham Wadhwa Aug 16 '18 at 12:37
  • No problem, that is aesthetic not requiered. I want equal size widget images. – cosmoscalibur Aug 16 '18 at 14:54
  • Interesting alternative to `pyplot`. See also this [custom package IPyPlot](https://stackoverflow.com/a/61592610/774575). – mins Apr 09 '21 at 13:49
2

Displaying two images side by side

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

enter image description here

Avi Avidan
  • 866
  • 8
  • 18
  • Hiding ticks and labels would be better for images, and ensuring the pixel ratio is not altered would also be a good idea. – mins Apr 09 '21 at 13:46
1

you can slightly modify the code above to display a grid of images.

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

enter image description here

Avi Avidan
  • 866
  • 8
  • 18
  • The initial answer is already a grid of subplots, but it has only one row. If you populate a 2D grid you don't know if you have enough images to fill all the grid cells. What you need to do to hide the empty cells would be interesting too. – mins Apr 09 '21 at 13:51
0

You can use subplot.

import pylab 
pylab.subplot(1, 2, 1)
pylab.plot(range(10))
pylab.subplot(1, 2, 2)
pylab.plot(range(20))
idnavid
  • 1,795
  • 17
  • 20