I have a folder with a lot of images that I want to use to bild a classificator using a SVM model in python with sklearn. I've always used csv file as train/test set with sklearn, how can I make it? (a csv file with records corrisponding to images and a variable for every pixel)

  • 1
    $\begingroup$ Two questions: 1) What is the format (.png, .jpeg, .pdf, etc) of your images? 2) How are the labels stored (is it the name of the folder they are in)? $\endgroup$ Nov 30, 2018 at 19:38
  • $\begingroup$ 1)jpeg 2)yes, there are two classes stored in two different folders $\endgroup$
    – user254087
    Dec 1, 2018 at 8:29

2 Answers 2


You are describing a one-time pre-processing step that will crawl through your folder and turn each image into a line of data and then save the entire collection in a CSV file. In turn, that file becomes your gold standard dataset.

If I was in your position, I would look into the Keras pre-processing tools that already provide python libraries to quickly do this task. It's a common need for image processing, the Keras library is very mature and can do this for you.


It should be something like this:

  1. Read image with Image.open()
  2. Convert to np.array()
  3. Flat the previous 3D array (height x width x channels) into 1D array
  4. Collect all the 1D arrays into list
  5. Convert list into np.array, resulting in 2D array (images x pixels)

Note: the code below is not tested

import glob
import PIL
import numpy

data = np.array([ np.array(PIL.Image.open(f).convert("RGB")).ravel() 
                  for f in glob.glob("./folder/*.jpeg") ])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.