33

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)?

0

6 Answers 6

34

scipy.misc.imread() will return a Numpy array, which is handy for lots of things.

Sign up to request clarification or add additional context in comments.

2 Comments

matplotlib.imread reads .png s (only) even without PIL installed.
this answer is 13 years old so it's not surprising that scipy.misc.imread() is deprecated. the docs suggest to use imageio.imread ; any option using PIL is preferable
14

Up till now no one told about matplotlib.image:

import matplotlib.image as img
image = img.imread(file_name)

Now the image would be a 3D numpy array

print image.shape

Would be something like: (317, 504, 3)

3 Comments

it is giving image dimensions not converting image into matrix bro
@Salvador This is returning the shape of the image in which the first two arguments in the result give you the length and width respectively while the third one give you the dimension. and the question is asking about the matrix.
'function' object has no attribute 'imread' ? x_test = [img.imread.imread(path, 'r') for path in x_test_paths]
7

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

Comments

6

im.load in PIL returns a matrix-like object.

Comments

2

you can use PyGame image and use PixelArray to access the pixeldata

1 Comment

thanks - i will have to look into that. for now, i happened to have scipy and PIL installed already...
1

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.