Thursday, March 18, 2021

Python exercise: get RGB array of image using matplotlib

Python code to get get RGB array of image using matplotlib:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

"""
ref:
A short tutorial on plotting images with Matplotlib:
https://matplotlib.org/stable/tutorials/introductory/images.html

numpy.ndarray:
https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html

"""
imageFile = '/home/pi/Desktop/image.jpg'
img = mpimg.imread(imageFile)

print(type(img))
print(dir(img))

print('shape (= length of each dimension):')
print(img.shape)
print()
print(img[0][0])
print(img[0][249])
print(img[249][0])
print(img[249][249])

print()
print(img)

imgplot = plt.imshow(img)
plt.show()




No comments: