Akhil
Akhil

Reputation: 77

How to generate array of images folder-wise?

segmentedchars folder has 3000 folders with names(1,2,...,3000). Each folders has different number of images. I am able to generate array of all the images but how can I generate array of individual folders,so that by looking at an array I can know that a particular folder contains this much number of images?

import os
import cv2
import glob
import random
import numpy as np
from pickle import dump

imglist = []
scan_files = glob.glob('segmentedchars\\*\\*.png')
for imgfile in scan_files:
    img = cv2.imread(imgfile)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    imglist.append(img)
dump(imglist, open('img.pkl', 'wb'))

Upvotes: 0

Views: 714

Answers (1)

ebeneditos
ebeneditos

Reputation: 2612

Following @Evert suggestion, you can do it like:

dirdict = {}

for imgdir in os.listdir('segmentedchars'):
    imglist = []
    for imgfile in glob.glob(os.path.join('segmentedchars', imgdir, '*.png')):
        img = cv2.imread(imgfile)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        imglist.append(img)

    dirdict[imgdir] = imglist

Edit

If you need specific sorting, you can do it with a list, as dictionnaries are not sorted. Keep in mind that '10' comes before '2' when you sort numbers as strings, that is why you'd need sort(key=float).

dirlist = []

imgdirs = os.listdir('segmentedchars')
imgdirs.sort(key=float)
for imgdir in imgdirs:
    imglist = []
    for imgfile in glob.glob(os.path.join('segmentedchars', imgdir, '*.png')):
        img = cv2.imread(imgfile)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        imglist.append(img)

    dirlist.append(imglist)

Upvotes: 1

Related Questions