How to Use NumPy for Basic Image Manipulation

Updated: January 22, 2024 By: Guest Contributor Post a comment

Introduction

NumPy is an essential library in the Python Data Science stack. Although it’s primarily known for its high-performance array operations in large-scale data processing, NumPy can also be effectively used for basic image manipulation tasks. In this tutorial, we’ll explore how to use NumPy to perform image manipulation ranging from simple to more complex transformations.

Setting Up Your Environment

Before we start, ensure you have Python installed on your machine, along with NumPy and an image processing library like Pillow (PIL Fork) which we’ll use to load and save images:

pip install numpy Pillow

Loading and Displaying an Image

First things first, let’s load an image and understand how NumPy represents it:

from PIL import Image
import numpy as np

# Load the image
image = Image.open('example.jpg')

# Convert the image to NumPy array
image_array = np.array(image)

# Display the array shape
print(image_array.shape)

Image as an Array of Pixels

An image in NumPy is simply a three-dimensional array where the dimensions represent the height, width, and color channels of the image. Now, let’s display the image using matplotlib:

import matplotlib.pyplot as plt

# Display the image
plt.imshow(image_array)
plt.show()

Basic Image Manipulations

Grayscale Conversion

To convert the image to grayscale using NumPy, we can average the color channels:

grayscale = image_array.mean(axis=2)

# Display the grayscale image
plt.imshow(grayscale, cmap='gray')
plt.show()

Cropping an Image

Cropping can be performed by slicing the array:

cropped_image = image_array[50:200, 50:200]

# Display the cropped image
plt.imshow(cropped_image)
plt.show()

Resizing an Image

Though not as powerful as dedicated libraries, with NumPy you can resize images using a simple function:

def resize_image(image_array, new_height, new_width):
    return np.resize(image_array, (new_height, new_width, image_array.shape[2]))

resized_image = resize_image(image_array, 100, 100)

# Display the resized image
plt.imshow(resized_image)
plt.show()

Flipping an Image

Flipping an image horizontally or vertically is straightforward. To flip an image horizontally, you invert the order of columns, and for vertical flipping, invert the rows:

# Horizontal flip
h_flipped = image_array[:, ::-1]

# Vertical flip
v_flipped = image_array[::-1, :]

# Show both flips
plt.subplot(1, 2, 1)
plt.imshow(h_flipped)
plt.title('Horizontally flipped')

plt.subplot(1, 2, 2)
plt.imshow(v_flipped)
plt.title('Vertically flipped')
plt.show()

Advanced Image Manipulations

Rotation

To rotate an image, we need to re-map the original pixel positions to their new locations. Here’s a simple 90 degrees rotation:

rotated_90_degree = np.rot90(image_array)

# Display the rotated image
plt.imshow(rotated_90_degree)
plt.show()

Color Channel Manipulation

We can manipulate the color channels of an image by isolating each channel:

red_channel = image_array.copy()
red_channel[:, :, 1:3] = 0

# Display the red channel
plt.imshow(red_channel)
plt.show()

Image Filtering

The following demonstrates a simple blur with NumPy, accomplished through a convolution with a uniform filter matrix:

def blur_image(image_array, blur_level):
    filter_size = (blur_level, blur_level)
    filter_matrix = np.full(filter_size, 1.0 / (blur_level ** 2))
    blurred = np.convolve(image_array, filter_matrix, mode='valid')
    return blurred

blurred_image = blur_image(image_array, 3)

# Display the blurred image
plt.imshow(blurred_image)
plt.show()

Conclusion

In this tutorial, we explored how to perform basic to intermediate image manipulation tasks using NumPy. From loading and displaying images to manipulating color channels and applying filters, the ease of using NumPy operations provides a quick passage into image processing. Whether for data visualization or as a stepping stone to more intricate image analysis, these techniques hold broad implications in the realm of computer vision and digital image processing.