VOOZH about

URL: https://www.geeksforgeeks.org/python/image-processing-with-scipy-and-numpy-in-python/

⇱ Image Processing with SciPy and NumPy in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Image Processing with SciPy and NumPy in Python

Last Updated : 15 Jan, 2026

Image processing is used in areas like computer vision and medical imaging, focusing on enhancing and analyzing digital images. In Python, NumPy treats images as arrays for efficient pixel-level operations, while SciPy’s ndimage module provides tools for filtering and transformations, enabling fast and lightweight processing.

Installation

Ensure you have the required libraries installed:

pip install numpy scipy matplotlib imageio scikit-image

  • NumPy: Handles arrays and numerical computations.
  • SciPy: Adds advanced scientific and mathematical functions.
  • Matplotlib: Creates plots and visualizations.
  • ImageIO: Reads and writes image files.
  • scikit-image: Provides tools for image processing and analysis.

To download the image used in this article, click here

Opening and Displaying Image

To begin any image processing task, the first step is to load and visualize the image. We'll use imageio.v3 to read an image and matplotlib to display it.

Output

πŸ‘ scipysaveimage
loaded image

Note: The image must be in the same folder as the Python script otherwise, provide a relative or full path.

Explanation:

  • iio.imread(): loads the image into a NumPy array.
  • plt.imshow(): visualizes it.
  • plt.axis('off'): hides axes for a cleaner look.

Creating NumPy array from Image

An image is essentially a multi-dimensional NumPy array. Knowing its shape and data type is important for applying filters and transformations.

Output

πŸ‘ Output
Pixel data type

Explanation: Shape helps understand the image layout (e.g., 266x341x3 for RGB). Data type (usually uint8) shows pixel value range (0-255).

Creating RAW file

A .raw file stores raw binary data from an image sensor or matrix. It's useful when dealing with uncompressed data in image pipelines.

Output

πŸ‘ File_Structure
RAW saved

Explanation: tofile() saves the image pixel data as a binary file, useful for low-level image processing.

Opening RAW File

To work with .raw files, we use np.fromfile() to reconstruct the image data into a usable NumPy array.

Output

πŸ‘ Output
Binary loaded

Explanation: fromfile() reads binary data and the array must be reshaped manually if you want to visualize it (e.g., reshape to original height Γ— width Γ— channels).

Getting Statistical Information

Understanding the min, max and average pixel intensity gives insight into brightness, contrast and histogram distribution of the image.

Output

πŸ‘ Output
Pixel stats

Explanation: Max and min values indicate contrast and Mean gives an overall idea of brightness.

Cropping the Image

Cropping helps focus on a particular region of interest (ROI) in an image by slicing the NumPy array.

Output

πŸ‘ Cropped_image

Explanation:

  • img.shape: gives image dimensions (height x, width y, channels _).
  • img[h//4 : 3*h//4, w//4 : 3*w//4]: selects a central region using slicing.
  • plt.imshow(): visualizes the cropped section.

Flipping Image (Vertical)

Flipping an image (up-down or left-right) is a common data augmentation technique in image preprocessing.

Output

πŸ‘ Flipped_image

Explanation: np.flipud() flips the image along the vertical axis.

Filtering images

Filtering is a fundamental technique in image processing used to enhance or suppress certain features. It helps in tasks like smoothing, sharpening and edge detection.

1. Gaussian Blur

Blurring helps reduce image noise and details using a Gaussian kernel. It’s useful in preprocessing steps like edge detection or thresholding.

Output

πŸ‘ Output

Explanation: gaussian_filter(img, sigma=5) smooths the image using a Gaussian kernel. sigma controls the intensity of blur and converts to uint8 before display to ensure proper color rendering.

2. Sharpening Image (Unsharp Masking)

Sharpening increases contrast between edges to enhance details and clarity. Unsharp masking subtracts a blurred version from the original.

Output

πŸ‘ sharpen_image

Explanation:

  • Converts image to grayscale using rgb2gray.
  • gray - gaussian_filter(blur, 1) extracts edge details and adds edge details back using alpha scaling Unsharp Masking.

Denoising Images

Image denoising removes random noise to enhance image quality, particularly useful in low-light photography or scanned documents.

1. Add noise

Artificial noise is added to simulate a noisy environment, commonly seen in real-world low-light or sensor-imperfect images.

Output

πŸ‘ noisy_image

Explanation: Adds random values scaled by image standard deviation to simulate real-world noise (e.g., from low-light sensors).

2. Gaussian Denoising

Gaussian filtering smooths the image by averaging pixel values with its neighbors using a Gaussian kernel, effectively reducing high-frequency noise.

πŸ‘ denoising_image

Explanation: Smooths the image using a Gaussian kernel to reduce high-frequency noise while preserving structure.

Edge Detection using Sobel Filter

Sobel edge detection identifies image edges by computing intensity gradients using 3Γ—3 kernels. It highlights boundaries by combining horizontal and vertical changes, aiding in tasks like segmentation and object detection.

Output

πŸ‘ Output
Original Synthetic Image
πŸ‘ Output
Sober Edge Detection

Explanation: Creates a synthetic image, applies Gaussian blur, then detects edges using Sobel filters by computing horizontal and vertical gradients and combining them to highlight edge intensity.

Related Articles:

Comment
Article Tags: