1

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))
Alberto Alvarez
  • 805
  • 3
  • 11
  • 20

2 Answers2

2

You can use PIL library :

from PIL import Image 
import numpy as np 

ic = []
for i in folders:
    for j in images:
        image = Image.open(i + j)
        ic.append(np.asarray(image))

ic = np.array(ic)

where folders and images are arrays of string with names

Yoann A.
  • 413
  • 4
  • 16
1

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())
Tonechas
  • 13,398
  • 16
  • 46
  • 80
  • thank you for the reply. The code that you posted gives the following thread - ImportError: cannot import name '_validate_lengths' from 'numpy.lib.arraypad' (C:\Users\KG\Anaconda3\lib\site-packages\numpy\lib\arraypad.py). I there is a way to deal with it? – Alberto Alvarez Apr 15 '19 at 13:48
  • I'm afraid you need to update `scikit-image`. Take a look at [this thread](https://stackoverflow.com/questions/54241226/importerror-cannot-import-name-validate-lengths). – Tonechas Apr 15 '19 at 14:02