0

I am trying to plot multiple images from a file in jupyter notebook. The images are displayed, but they are in one column. I'm using:

%matplotlib inline
from os import listdir
form PIL import image as PImage
from matplotlib import pyplot as plt

def loadImages(path):
    imagesList = listdir(path)
    loadedImages = []
    for image in imagesList:
        img = PImage.open(path+image)
        LoadedImages.append(img)
        Return loadedImages
path = 'C:/Users/Asus-PC/flowers/'
imgs = loadImages(path)

for img in imgs
    plt.figure()
    plt.imshow(img)

I would like them to appear in a grid layout (row and column). Part of the problem is that I do not know what the arguments to add_subplot mean. How can I achieve this?

2
  • Check out plt.subplots. I find this to be one of the easiest ways. There are quite a few posts on this I'm sure. Here's one that I posted previously. Commented Oct 12, 2019 at 23:03
  • You don't need to import PIL, matplotlib has plt.imread. Commented Oct 18, 2019 at 19:00

2 Answers 2

1

You can go with matplotlib but it tends to be really slow and inefficient for displaying big number of images. So if you want to do it much faster and easier - I would recommend using my package called IPyPlot:

import ipyplot

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

It supports images in following formats:
- string file paths
- PIL.Image objects
- numpy.ndarray objects representing images

And it's capable of displaying hundreds of images in just ~70ms

Sign up to request clarification or add additional context in comments.

Comments

0

You can create multiplesubplots using plt.subplots. Determine the number of columns and rows on the number of loaded images. Something like:

from os import listdir
import matplotlib.pyplot as plt

path = 'C:/Users/Asus-PC/flowers/'
imagesList = listdir(path)
n_images = len(imagesList)    

figure, axes = plt.subplots(n_images, 1)   # (columns, rows)    
for ax, imgname in zip(axes, imagesList):  # Iterate over images and
        img = plt.imread(path+imgname)     # axes to plot one image on each.
        ax.imshow(img)                     # You can append them to an empty
                                           # list if you need them later.
plt.show()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.