For those who want to display images side by side as soon as possible with Python's matplotlib

Source code

import matplotlib.pyplot as plt
import cv2
import os

root = "./data" #The folder that contains the images. Please change accordingly
lsdir = os.listdir(root)

imgs = []
for l in lsdir:
    target = os.path.join(root,l)
    img = cv2.imread(target)
    img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) #Color conversion for display in pyplot
    imgs.append(img)

shownumber = 10 #Number of images to arrange
showaxis = 1

while(showaxis*showaxis < shownumber):
    showaxis += 1

cnt = 0
while(1):
    #limit = 30
    #if cnt >= limit:
    #    break
    fig,axs = plt.subplots(showaxis,showaxis)
    ar = axs.ravel()
    for i in range(showaxis*showaxis):
        ar[i].axis('off')
        if i < shownumber:
            ar[i].imshow(imgs[cnt])
            cnt += 1
    plt.show()

Display result

shownumber = 9

image.png

shownumber = 10

image.png

Why i wrote this article

――I feel like I'm wondering about matplotlib every time I display an image, so I wanted to set a standard for myself. --Since specifying row and column is subtly annoying, I wanted the code to work just by specifying the number of displays. ――I wanted to create a page where such source code would hit immediately

Code description

--Specify a folder and read it with cv2.imread, and make it an array called ʻimgs. --OpenCV is read in BGR format, so convert it to RGB format with cv2.cvtColor --If you specify the display numbershow number, find the minimum square that satisfies it, and set one side as show axis`.

while(showaxis*showaxis < shownumber):
    showaxis += 1

--Prepare a graph with plt.subplots with as many rows and columns as show axis. ――Since it is troublesome to specify the vertical position and horizontal position of ʻaxs, make a series of arrays with ʻaxs.ravel (). --Display images with ʻimshow and ʻaxis ('off') as many as show number per loop --Keep spinning the loop until ʻimgs` runs out

Note that due to the structure of the code, an out-of-range reference error will definitely occur in the end. I wrote the escape process in the comments.

Recommended Posts

For those who want to display images side by side as soon as possible with Python's matplotlib
I want to display multiple images with matplotlib.
For those who want to write Python with vim
For those who want to start machine learning with TensorFlow2
[Google Colab] I want to display multiple images side by side in tiles
How to display images continuously with matplotlib Note
Join Azure Using Go ~ For those who want to start and know Azure with Go ~
For those who want to learn Excel VBA and get started with Python
5 Reasons Processing is Useful for Those Who Want to Get Started with Python
Anxible points for those who want to introduce Ansible
The first step of machine learning ~ For those who want to implement with python ~
Environment construction for those who want to study python easily with VS Code (for Mac)
Reference reference for those who want to code in Rhinoceros / Grasshopper
A modern environment building procedure for those who want to get started with Python right away
Introducing how to use argparse for people who are laid back with as little effort as possible
[Short sentence] easygui for those who want to use a simple GUI with Python very easily
[2020 version for beginners] Recommended study method for those who want to become an AI engineer by themselves
PyPI registration steps for those who want to make a PyPI debut
Loose articles for those who want to start natural language processing
Python techniques for those who want to get rid of beginners
A memo for those who want quick socket communication with netcat
I analyzed Airbnb data for those who want to stay in Amsterdam
Decide who to vote for by lottery
[python] How to display list elements side by side
For those who are new to programming but have decided to analyze data with Python