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?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
anastasya
  • 9
  • 1
  • 2
  • Check out [`plt.subplots`](https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.subplots.html). I find this to be one of the easiest ways. There are quite a few posts on this I'm sure. Here's [one](https://stackoverflow.com/a/42818501/6942527) that I posted previously. – busybear Oct 12 '19 at 23:03
  • You don't need to import PIL, matplotlib has `plt.imread`. – flurble Oct 18 '19 at 19:00

2 Answers2

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

Karol Żak
  • 2,158
  • 20
  • 24
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()
flurble
  • 1,086
  • 7
  • 21