How to read an input image and print it into an array in matplotlib?

MatplotlibPythonData Visualization

To read an input image and print it into an array in matplotlib, we can take the following steps

Steps

  • Set the figure size and adjust the padding between and around the subplots.

  • Read an image from a file into an array. Use plt.imread() method.

  • Print the Numpy array of the image.

  • To turn off the axis, use axis('off') method.

  • To display the figure, use Show() method.

Example

from matplotlib import pyplot as plt

plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

im = plt.imread("forest.jpg")
print("Numpy array of the image is: ", im)

im = plt.imshow(im)
plt.axis('off')

plt.show()

Output

It will produce the following output −

On the console, you will get the NumPy array of this image −

Numpy array of the image is −

[[[ 47 129 211]
   [ 47 129 211]
   [ 49 130 212]
   ...
   [ 50 76 109]
   [ 52 77 108]
   [ 41 66 97]]

   [[ 47 129 211]
      [ 47 129 211]
      [ 49 130 212]
      ...
      [ 43 69 102]
      [ 51 76 107]
      [ 46 71 102]]

[[ 45 130 211]
   [ 45 130 211]
   [ 48 130 212]
   ...
   [ 42 67 98]
   [ 45 70 101]
   [ 49 74 105]]

   ...

[[102 53 21]
   [101 51 18]
   [111 59 22]
   ...
   [ 28 17 11]
   [ 31 16 9]
   [ 33 16 9]]

[[ 99 49 14]
   [ 85 35 0]
   [100 50 13]
   ...
   [ 29 16 10]
   [ 30 15 8]
   [ 33 16 9]]

[[ 93 45 7]
   [ 91 46 7]
   [ 93 47 11]
   ...
   [ 29 16 10]
   [ 36 19 12]
[ 39 20 14]]]
raja
Updated on 11-Oct-2021 08:27:39

Advertisements