NumPy Essentials for Advanced Image Processing in Python

Image processing is a vital skill in the realm of data science and computer vision, and mastering it requires a robust understanding of tools like NumPy.

As one of Python’s most powerful libraries, NumPy offers unparalleled capabilities for handling and processing large arrays of data, making it an essential tool for any advanced image processing task.

Whether you’re manipulating pixels, applying filters, or analyzing image patterns, NumPy provides the flexibility and efficiency needed to work with complex image data.

In this guide, we’ll explore the essential techniques and applications of NumPy for advanced image processing in Python, equipping you with the skills to tackle even the most challenging image-related projects.

To get started, we need to import the necessary libraries: PIL, NumPy, and Matplotlib. The PIL (Pillow) library is essential for opening and manipulating image files. NumPy provides powerful tools for efficient array operations and image processing, making it indispensable for handling image data.

Finally, Matplotlib is used for visualizing the images, allowing us to display and analyze the results of our image processing tasks effectively.

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

To crop an image, we specify the coordinates that outline the desired area. The resulting image retains only the selected portion, while the remaining parts are discarded.

# Import the necessary libraries
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

# Load the image using PIL (Python Imaging Library)
img = Image.open('un.jpg')

# Convert the image to a NumPy array
img_array = np.array(img)

# Get image dimensions
height, width, _ = img_array.shape

# Define the cropping coordinates (initial guesses for the face of the dog)
# You will need to adjust these coordinates according to your specific image
y1, x1 = 200, 300  # Top-left corner of ROI (Region of Interest)
y2, x2 = 800, 900  # Bottom-right corner of ROI

# Ensure coordinates are within image bounds
y1 = min(max(0, y1), height - 1)
x1 = min(max(0, x1), width - 1)
y2 = min(max(y1 + 1, y2), height)  # Ensure y2 > y1
x2 = min(max(x1 + 1, x2), width)   # Ensure x2 > x1

# Crop the image using adjusted coordinates
cropped_img = img_array[y1:y2, x1:x2]

# Display the original image and the cropped image
plt.figure(figsize=(12, 6))

# 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')

# Adjust layout and show the plot
plt.tight_layout()
plt.show()

Output:

Rotate Image

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

# Import the necessary libraries
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

# Load the image using PIL (Python Imaging Library)
img = Image.open('un.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()

Output:

Flip Image

We will use NumPy’s ‘fliplr’ function to flip the image array horizontally.

# Import the necessary libraries
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
# Load the image using PIL (Python Imaging Library)
img = Image.open('un.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() 

Output:

Negative of an Image

Creating the negative of an image involves inverting its pixel values. For grayscale images, this is achieved by subtracting each pixel’s value from the maximum possible value (255 for 8-bit images). In the case of color images, the inversion is performed independently for each color channel (Red, Green, Blue).

# Import the necessary libraries
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
# Load the image using PIL (Python Imaging Library)
# Load the image using PIL (Python Imaging Library)
img = Image.open('un.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() 

output:

Binarize Image

Binarizing an image transforms it into a black-and-white format. This process involves assigning each pixel a value of either black or white based on a specified threshold. Pixels with values below the threshold are set to 0 (black), while those above the threshold are set to 255 (white).

# Import the necessary libraries
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
# Load the image using PIL (Python Imaging Library)
# Load the image using PIL (Python Imaging Library)
# Load the image using PIL (Python Imaging Library)
img = Image.open('un.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() 

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.

# Import the necessary libraries
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
# Load the image using PIL (Python Imaging Library)
# Load the image using PIL (Python Imaging Library)
# Load the image using PIL (Python Imaging Library)
# Load the image using PIL (Python Imaging Library)
img = Image.open('un.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() 

Output:

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.

# Import the necessary libraries
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
# Load the image using PIL (Python Imaging Library)
# Load the image using PIL (Python Imaging Library)
img = Image.open('un.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() 

Output:

Pixel Intensity Histogram

A pixel intensity histogram illustrates the distribution of pixel values within an image. To generate this histogram, the image is first converted into a one-dimensional array, which then allows for the calculation and visualization of the pixel value distribution.

# Import the necessary libraries
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
# Load the image using PIL (Python Imaging Library)
# Load the image using PIL (Python Imaging Library)
# Load the image using PIL (Python Imaging Library)
img = Image.open('un.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() 

Output:

Mastering NumPy is essential for advanced image processing in Python. Its ability to handle large arrays and matrices with high efficiency makes it a powerful tool for manipulating images at a pixel level. From basic tasks like cropping and resizing to more complex operations like filtering, enhancing, and analyzing image data, NumPy provides the foundation for building sophisticated image processing workflows. By leveraging its capabilities, you can unlock new possibilities in image analysis, computer vision, and beyond, making it an indispensable skill for anyone serious about working with images in Python.

Author

Sona Avatar

Written by

Leave a Reply

Trending

CodeMagnet

Your Magnetic Resource, For Coding Brilliance

Programming Languages

Web Development

Data Science and Visualization

Career Section

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4205364944170772"
     crossorigin="anonymous"></script>