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()
Â
Â
Â
Â
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()
Â
Â
Â
Â
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()
Â
Â
Â
Â
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)
Â
Â
Â
Â
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
Â
Â
Â
Â
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()
Â
Â
Â
Â
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()
Â
Â
Â
Â
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
Â
Â
Â
Â
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.