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

To read an input image and print it into an array in matplotlib, we can use plt.imread() to load the image as a NumPy array and plt.imshow() to display it.

Steps

  • Import matplotlib.pyplot

  • Read an image from a file using plt.imread() method

  • Print the NumPy array representation of the image

  • Display the image using plt.imshow()

  • Use plt.axis('off') to hide axis labels

  • Show the plot using plt.show()

Example with Sample Data

Since we need a complete runnable example, let's create a sample image array and demonstrate the concept ?

import matplotlib.pyplot as plt
import numpy as np

# Create a sample RGB image (3x3 pixels for demonstration)
sample_image = np.array([
    [[255, 0, 0], [0, 255, 0], [0, 0, 255]],     # Red, Green, Blue
    [[255, 255, 0], [255, 0, 255], [0, 255, 255]], # Yellow, Magenta, Cyan  
    [[128, 128, 128], [64, 64, 64], [192, 192, 192]] # Gray shades
], dtype=np.uint8)

print("NumPy array of the image:")
print(sample_image)
print("\nImage shape:", sample_image.shape)

# Display the image
plt.figure(figsize=(6, 6))
plt.imshow(sample_image)
plt.title("Sample RGB Image")
plt.axis('off')
plt.show()
NumPy array of the image:
[[[255   0   0]
  [  0 255   0]
  [  0   0 255]]

 [[255 255   0]
  [255   0 255]
  [  0 255 255]]

 [[128 128 128]
  [ 64  64  64]
  [192 192 192]]]

Image shape: (3, 3, 3)

Reading an External Image File

For reading actual image files, use the following approach ?

import matplotlib.pyplot as plt

# Read image from file (requires an actual image file)
image_array = plt.imread("your_image.jpg")  # or .png, .gif etc.

print("Image shape:", image_array.shape)
print("Data type:", image_array.dtype)
print("Min value:", image_array.min())  
print("Max value:", image_array.max())

# Display the image
plt.figure(figsize=(8, 6))
plt.imshow(image_array)
plt.title("Loaded Image")
plt.axis('off')
plt.show()

# Print a portion of the array (first 3x3 pixels)
print("\nFirst 3x3 pixels:")
print(image_array[:3, :3])

Understanding Image Arrays

Images are represented as NumPy arrays with different structures ?

  • Grayscale images: 2D array (height, width) with values 0-255

  • RGB images: 3D array (height, width, 3) where the third dimension represents R, G, B channels

  • RGBA images: 3D array (height, width, 4) with an additional alpha channel for transparency

Creating a Grayscale Example

import matplotlib.pyplot as plt
import numpy as np

# Create a simple grayscale gradient
grayscale_image = np.array([
    [0, 64, 128, 192, 255],
    [32, 96, 160, 224, 255], 
    [64, 128, 192, 255, 255],
    [96, 160, 224, 255, 255]
], dtype=np.uint8)

print("Grayscale image array:")
print(grayscale_image)
print("Shape:", grayscale_image.shape)

# Display grayscale image
plt.figure(figsize=(6, 4))
plt.imshow(grayscale_image, cmap='gray')
plt.title("Grayscale Image")
plt.colorbar()
plt.axis('off')
plt.show()
Grayscale image array:
[[  0  64 128 192 255]
 [ 32  96 160 224 255]
 [ 64 128 192 255 255]
 [ 96 160 224 255 255]]
Shape: (4, 5)

Key Points

  • plt.imread() returns a NumPy array representing pixel values

  • RGB values range from 0-255 for uint8 arrays or 0.0-1.0 for float arrays

  • Use cmap='gray' parameter in imshow() for grayscale images

  • The array shape tells you the image dimensions and number of channels

Conclusion

Use plt.imread() to load images as NumPy arrays and plt.imshow() to display them. The array structure depends on whether the image is grayscale (2D) or colored (3D with RGB channels).

Updated on: 2026-03-26T15:21:37+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements