NumPy for Image Processing

NumPy for Image Processing
Image by freepik

NumPy is a robust tool for image processing in Python. It lets you manipulate images using array operations. This article explores several image processing techniques using NumPy.

Importing Libraries

We must import the required libraries: PIL, NumPy, and Matplotlib. PIL is used for opening images. NumPy allows for efficient array operations and image processing. Matplotlib is used for visualizing images

import numpy as np  from PIL import Image  import matplotlib.pyplot as plt

Crop Image

We define coordinates to mark the area we want to crop from the image. The new image contains only the selected part and discards the rest.

# Load the image using PIL (Python Imaging Library)  img = Image.open('cat.jpg')    # Convert the image to a NumPy array  img_array = np.array(img)    # Define the cropping coordinates  y1, x1 = 1000, 1000  # Top-left corner of ROI  y2, x2 = 2500, 2000  # Bottom-right corner of ROI  cropped_img = img_array[y1:y2, x1:x2]    # Display the original image and the cropped image  plt.figure(figsize=(10, 5))    # Display the original image  plt.subplot(1, 2, 1)  plt.imshow(img_array)  plt.title('Original Image')  plt.axis('off')    # Display the cropped image  plt.subplot(1, 2, 2)  plt.imshow(cropped_img)  plt.title('Cropped Image')  plt.axis('off')    plt.tight_layout()  plt.show()   

Cropped_image

Rotate Image

We rotate the image array 90 degrees counterclockwise using NumPy's 'rot90' function.

# Load the image using PIL (Python Imaging Library)  img = Image.open('cat.jpg')    # Convert the image to a NumPy array  img_array = np.array(img)    # Rotate the image by 90 degrees counterclockwise  rotated_img = np.rot90(img_array)    # Display the original image and the rotated image  plt.figure(figsize=(10, 5))    # Display the original image  plt.subplot(1, 2, 1)  plt.imshow(img_array)  plt.title('Original Image')  plt.axis('off')    # Display the rotated image  plt.subplot(1, 2, 2)  plt.imshow(rotated_img)  plt.title('Rotated Image (90 degrees)')  plt.axis('off')    plt.tight_layout()  plt.show()    

Rotated_image

Flip Image

We use NumPy's 'fliplr' function to flip the image array horizontally.

# Load the image using PIL (Python Imaging Library)  img = Image.open('cat.jpg')    # Convert the image to a NumPy array  img_array = np.array(img)    # Flip the image horizontally  flipped_img = np.fliplr(img_array)    # Display the original image and the flipped image  plt.figure(figsize=(10, 5))    # Display the original image  plt.subplot(1, 2, 1)  plt.imshow(img_array)  plt.title('Original Image')  plt.axis('off')    # Display the flipped image  plt.subplot(1, 2, 2)  plt.imshow(flipped_img)  plt.title('Flipped Image')  plt.axis('off')    plt.tight_layout()  plt.show() 

Flipped_image

Negative of an Image

The negative of an image is made by reversing its pixel values. In grayscale images, each pixel's value is subtracted from the maximum (255 for 8-bit images). In color images, this is done separately for each color channel.

# Load the image using PIL (Python Imaging Library)  img = Image.open('cat.jpg')    # Convert the image to a NumPy array  img_array = np.array(img)    # Check if the image is grayscale or RGB  is_grayscale = len(img_array.shape) < 3    # Function to create negative of an image  def create_negative(image):      if is_grayscale:          # For grayscale images          negative_image = 255 - image      else:          # For color images (RGB)          negative_image = 255 - image      return negative_image    # Create negative of the image  negative_img = create_negative(img_array)    # Display the original and negative images  plt.figure(figsize=(10, 5))  plt.subplot(1, 2, 1)  plt.imshow(img_array)  plt.title('Original Image')  plt.axis('off')    plt.subplot(1, 2, 2)  plt.imshow(negative_img)  plt.title('Negative Image')  plt.axis('off')    plt.tight_layout()  plt.show() 

Negative_image

Binarize Image

Binarizing an image converts it to black and white. Each pixel is marked black or white based on a threshold value. Pixels that are less than the threshold become 0 (black) and above those above it become 255 (white).

# Load the image using PIL (Python Imaging Library)  img = Image.open('cat.jpg')    # Convert the image to grayscale  img_gray = img.convert('L')    # Convert the grayscale image to a NumPy array  img_array = np.array(img_gray)    # Binarize the image using a threshold  threshold = 128  binary_img = np.where(img_array < threshold, 0, 255).astype(np.uint8)    # Display the original and binarized images  plt.figure(figsize= (10, 5))    plt.subplot(1, 2, 1)  plt.imshow(img_array, cmap='gray')  plt.title('Original Grayscale Image')  plt.axis('off')    plt.subplot(1, 2, 2)  plt.imshow(binary_img, cmap='gray')  plt.title('Binarized Image (Threshold = 128)')  plt.axis('off')    plt.tight_layout()  plt.show() 

Binarize_image

Color Space Conversion

Color space conversion changes an image from one color model to another. This is done by changing the array of pixel values. We use a weighted sum of the RGB channels to convert a color image to a grayscale.

# Load the image using PIL (Python Imaging Library)  img = Image.open('cat.jpg')    # Convert the image to a NumPy array  img_array = np.array(img)    # Grayscale conversion formula: Y = 0.299*R + 0.587*G + 0.114*B  gray_img = np.dot (img_array[..., :3], [0.299, 0.587, 0.114])    # Display the original RGB image  plt.figure(figsize=(10, 5))  plt.subplot(1, 2, 1)  plt.imshow(img_array)  plt.title('Original RGB Image')  plt.axis('off')    # Display the converted grayscale image  plt.subplot(1, 2, 2)  plt.imshow(gray_img, cmap='gray')  plt.title('Grayscale Image')  plt.axis('off')    plt.tight_layout()  plt.show() 

Color_conversion

Pixel Intensity Histogram

The histogram shows the distribution of pixel values in an image. The image is flattened into a one-dimensional array to compute the histogram.

# Load the image using PIL (Python Imaging Library)  img = Image.open('cat.jpg')    # Convert the image to a NumPy array  img_array = np.array(img)    # Compute the histogram of the image  hist, bins = np.histogram(img_array.flatten(), bins=256, range= (0, 256))    # Plot the histogram  plt.figure(figsize=(10, 5))  plt.hist(img_array.flatten(), bins=256, range= (0, 256), density=True, color='gray')  plt.xlabel('Pixel Intensity')  plt.ylabel('Normalized Frequency')  plt.title('Histogram of Grayscale Image')  plt.grid(True)  plt.show() 

Histogram

Masking Image

Masking an image means showing or hiding parts based on rules. Pixels marked as 1 are kept while pixels marked as 0 are hidden.

# Load the image using PIL (Python Imaging Library)  img = Image.open('cat.jpg')    # Convert the image to a NumPy array  img_array = np.array(img)    # Create a binary mask  mask = np.zeros_like(img_array[:, :, 0], dtype=np.uint8)  center = (img_array.shape[0] // 2, img_array.shape[1] // 2)  radius = min(img_array.shape[0], img_array.shape[1]) // 2  # Increase radius for a bigger circle  rr, cc = np.meshgrid(np.arange(img_array.shape[0]), np.arange(img_array.shape[1]), indexing='ij')  circle_mask = (rr - center [0]) ** 2 + (cc - center [1]) ** 2 < radius ** 2  mask[circle_mask] = 1    # Apply the mask to the image  masked_img = img_array.copy()  for i in range(img_array.shape[2]):  # Apply to each color channel      masked_img[:,:,i] = img_array[:,:,i] * mask    # Displaying the original image and the masked image  plt.figure(figsize=(10, 5))    plt.subplot(1, 2, 1)  plt.imshow(img_array)  plt.title('Original Image')  plt.axis('off')    plt.subplot(1, 2, 2)  plt.imshow(masked_img)  plt.title('Masked Image')  plt.axis('off')    plt.tight_layout()  plt.show() 

Masked_image

Wrapping Up

This article showed different ways to process images using NumPy. We used PIL, NumPy and Matplotlib to crop, rotate, flip, and binarize images. Additionally, we learned about creating image negatives, changing color spaces, making histograms, and applying masks.

Jayita Gulati is a machine learning enthusiast and technical writer driven by her passion for building machine learning models. She holds a Master's degree in Computer Science from the University of Liverpool.

More On This Topic

  • Transfer Learning for Image Recognition and Natural Language Processing
  • How to Convert an RGB Image to Grayscale
  • Image Classification with Convolutional Neural Networks (CNNs)
  • Find a Picture in an Image Without Marking it Up
  • 8 Best Python Image Manipulation Tools
  • A Guide to Train an Image Classification Model Using Tensorflow
Follow us on Twitter, Facebook
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 comments
Oldest
New Most Voted
Inline Feedbacks
View all comments

Latest stories

You might also like...