2
0

More than 3 years have passed since last update.

OpenCVで指定ディレクトリの画像を一覧表示する

Posted at
import cv2
import math
import matplotlib.pyplot as plt
%matplotlib inline

# 指定パスのファイルを一括取得
import glob
import re

def show_all_img(img_path, size):
  #img_path:画像の格納されたパスを指定

  #サイズ
  plt.figure(figsize=size)

  #拡張子がpng, jpgのファイルを抽出
  img_file_names = [p for p in glob.glob('{}/**'.format(img_path), recursive=True)
                    if re.search('.*(.png|.jpg)', p)]

  img_dic = {}
  for i in img_file_names:
    img_dic[i] = cv2.cvtColor(cv2.imread(i), cv2.COLOR_BGR2RGB)

  rows = math.ceil(math.sqrt(img_file_num))
  cols = rows

  for i,k in enumerate(img_dic.keys()): 
    plt.subplot(rows,cols,i+1)
    plt.title(k)
    plt.imshow(img_dic[k])

#####
img_path = "./data"
show_all_img(img_path, (15, 10))


2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0