Search code examples
pythonpcascikit-image

Importing all pictures from the directory


In this directory "C:\Users\KG\Documents\R\data" I have 40 folders that are named from s1 to s40, where in each of the folder there are 10 pictures (.png) of faces named as (1,2,..10). How to import collection of pictures - faces as a flattened array? I use the code below, but it provide me with the mistake (does not download pictures):

from skimage import io
ic = io.ImageCollection('C:/Users/KG/Documents/R/data/*/*.png')
ic = np.array(ic)
ic_flat = ic.reshape((len(ic), -1))

Solution

  • Give this code a try:

    import os
    from skimage import io
    import numpy as np
    
    folder = 'C:/Users/KG/Documents/R/data'
    
    images = [os.path.join(root, filename)
              for root, dirs, files in os.walk(folder)
              for filename in files
              if filename.lower().endswith('.png')]
    
    ic = []
    for img in images:
        ic.append(io.imread(img).flatten())