title: Image to matrix using python
body: <p>I am required to access all images in a folder and store it in a matrix. I was able to do it using matlab and here is the code:</p>
  
  <pre><code>input_dir = 'C:\Users\Karim\Downloads\att_faces\New Folder';
  image_dims = [112, 92];
  
  filenames = dir(fullfile(input_dir, '*.pgm'));
  num_images = numel(filenames);
  
  images = [];
  for n = 1:num_images
      filename = fullfile(input_dir, filenames(n).name);
      img = imread(filename);
      img = imresize(img,image_dims);   
  end
  </code></pre>
  
  <p>but I am required to do it using python and here is my python code:</p>
  
  <pre><code>import Image
  import os
  from PIL import Image
  from numpy import *
  import numpy as np
  
  
  #import images
  dirname = "C:\\Users\\Karim\\Downloads\\att_faces\\New folder"
  
  
  #get number of images and dimentions
  path, dirs, files = os.walk(dirname).next()
  num_images = len(files)
  image_file = "C:\\Users\\Karim\\Downloads\\att_faces\\New folder\\2.pgm"
  im = Image.open(image_file)
  width, height = im.size
  
  
  
  images = []
  
  for x in xrange(1, num_images):
      filename = os.listdir(dirname)[x]
      img = Image.open(filename)
      img = im.convert('L')
      images[:, x] = img[:]
  </code></pre>
  
  <p>but I am getting this error:
  IOError: [Errno 2] No such file or directory: '10.pgm'
  although the file is present. </p>