Convert image to a matrix in python

Viewed 79501

I want to do some image processing using Python.

Is there a simple way to import .png image as a matrix of greyscale/RGB values (possibly using PIL)?

6 Answers

scipy.misc.imread() is deprecated now. We can use imageio.imread instead of that to read it as a Numpy array

Definitely try

from matplotlib.image import imread

image  = imread(filename)  

The filename preferably has to be an .jpg image. And then, try

image.shape

This would return :

  • for a black and white or grayscale image An (n,n) matrix where n represents the dimension of the images (pixels) and values inside the matrix range from 0 to 255. Typically 0 is taken to be black, and 255 is taken to be white. 128 tends to be grey!

  • For color or RGB image It will render a tensor of 3 channels. Each channel is an (n,n) matrix where each entry represents the respectively the level of Red, Green or Blue at the actual location inside the image.

Related