Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display images in a row with IPython display?

Tags:

python

ipython

If I execute the following command:

for file in files:
    display(Image(filename=os.path.join(folder,file)))

I get a list of images in a column:

enter image description here

How to put them in a row (horizontally)?

like image 481
Suzan Cioc Avatar asked Mar 15 '16 08:03

Suzan Cioc


People also ask

How do I display an image in IPython?

Python solution We could use the Image class of IPython. display to load and display a local image in the IPython notebook. Here, I have used the Image( ) function, where one needs to supply the filename i.e., the location path for the image file. Additionally, we can vary the width and height to adjust the image size.

Which method is used to display image in the Jupyter?

To display the image, the Ipython. display() method necessitates the use of a function. In the notebook, you can also specify the width and height of the image.

How do you pass the image path in Jupyter Notebook?

Insert image from URL Create some repository and upload an image file there (this can be easily done on the GitHub website). Then click on the image and click on the Download button. The GitHub will open a new tab with your image. You can use the URL address in the Jupyter Notebook.


2 Answers

This worked for me:

from matplotlib.pyplot import figure, imshow, axis
from matplotlib.image import imread

def showImagesHorizontally(list_of_files):
    fig = figure()
    number_of_files = len(list_of_files)
    for i in range(number_of_files):
        a=fig.add_subplot(1,number_of_files,i+1)
        image = imread(list_of_files[i])
        imshow(image,cmap='Greys_r')
        axis('off')

enter image description here

like image 99
BarzinM Avatar answered Sep 19 '22 19:09

BarzinM


You can also use HTML:

from IPython.display import display, HTML
def make_html(folder, image):
     return '<img src="{}" style="display:inline;margin:1px"/>'
            .format(os.path.join(folder, image))

display(HTML(''.join(make_html(f, x)) for x in files))

In my case, by setting a margin will fix the un-alignment (and IMHO produce nicer results).

like image 25
AkiRoss Avatar answered Sep 21 '22 19:09

AkiRoss