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)?
Convert Image To Matrix in PythonImport Image module from PILLOW library of Python as PIL. Import array module from NUMPY library of Python. These two libraries are for Image extraction from the source file and defining the dimensions of the matrix.
imread() function is used to load the image and It also reads the given image (PIL image) in the NumPy array format. Then we need to convert the image color from BGR to RGB. imwrite() is used to save the image in the file.
scipy.misc.imread()
will return a Numpy array, which is handy for lots of things.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With