Convert image to a matrix in python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3493092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:25:14  来源:igfitidea点击:

Convert image to a matrix in python

pythonimage-processingnumpypython-imaging-library

提问by hatmatrix

I want to do some image processing using Python.

I want to do some image processing using Python.

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

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

采纳答案by ptomato

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

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

回答by Katriel

im.loadin PILreturns a matrix-like object.

im.loadin PILreturns a matrix-like object.

回答by Nikolaus Gradwohl

you can use PyGame imageand use PixelArrayto access the pixeldata

you can use PyGame imageand use PixelArrayto access the pixeldata

回答by Salvador Dali

Up till now no one told about matplotlib.image:

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

Now the image would be a 3D numpy array

print image.shape

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

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

回答by anon

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

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

回答by FellerRock

Definitely try

Definitely try

from matplotlib.image import imread

image  = imread(filename)  

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

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

image.shape

This would return :

This would return :

  • for a black and white or grayscale imageAn (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 imageIt 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.

  • for a black and white or grayscale imageAn (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 imageIt 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.