
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
SciPy - Reading and Writing Images
In SciPy Reading and Writing images is typically done using the scipy.ndimage module in combination with other libraries such as PIL (Python Imaging Library) or imageio. While scipy.ndimage is powerful for image processing it doesn't naively handle image I/O (input/output).
For reading and writing images the SciPy relies on the imageio and PIL libraries which allow handling different image formats such as PNG, JPEG, TIFF, etc. Heres an in-depth explanation of how to read and write images in Python using the tools available through SciPy and related libraries.
Image Formats
There are several common image formats are supported for reading and writing operations. Following is an overview of the image formats and related functionalities in scipy −
- JPEG (.jpg, .jpeg): Joint Photographic Experts Group (JPEG) is used for compressing photographic images and the lossy compression supports varying quality levels.
- PNG (.png): Portable Network Graphics ideal for lossless compression. This supports transparency i.e., alpha channel.
- BMP (.bmp): Bitmap is used for uncompressed or minimally compressed image format. This is simple and widely supported.
- TIFF (.tif, .tiff): Tagged Image File Format is highly flexible format which supports lossless compression and suitable for high-quality images.
- GIF (.gif): Graphics Interchange Format is often used for simple animations or static images and limited to 256 colors.
- PPM/PGM (.ppm, .pgm): Portable pixmap format is simple, raw image format which is primarily used in academic and research contexts.
Reading Images in SciPy
Scipy uses the scipy.ndimage.imread() function for reading the images but now it is deprecated so we can use Pillow or imageio to read color images along with the scipy library.
Reading Images Using PIL
Pillow is another popular library for image I/O in Python which is a fork of the original Python Imaging Library (PIL). As we all know that SciPy doesn't have built-in functions for reading and writing images so Pillow is often used for these tasks.
Here is an example of reading the image with the help of PIL library along with the scipy library −
from PIL import Image import numpy as np # Load the image using Pillow img = Image.open("/Images/images.jpeg") # Convert to a NumPy array img_array = np.array(img) print(f"Image shape: {img_array.shape}") # e.g., (height, width, 3) for RGB
Following is the output of reading an image with the help op PIL library −
Image shape: (162, 311)
Reading Images Using imageio
imageio is a Python library that supports reading and writing images in various formats such as PNG, JPEG, TIFF and others. It is easy to use and works well with SciPys image processing tools and which is the most recommended one.
Following is the example which uses the imageio library for reading the image along the scipy library −
import imageio.v2 as imageio # Reading an image image = imageio.imread("/Images/images.jpeg") print(f"Image shape: {image.shape}") # e.g., (height, width, 3) for RGB
Following is the output of reading an image with the help op PIL library −
Image shape: (162, 311, 3)
Writing Images in Scipy
Once the image has been processed or transformed then we can write the result to a new file. As with reading we will typically use imageio or Pillow to save images.
Writing Images Using imageio
The imwrite() function from imageio writes the NumPy array image to a file in the specified format such as PNG, JPEG.
Here is the example of writing the image with the help of imageio library imwrite() function −
import imageio.v2 as imageio import numpy as np # Reading an image img = imageio.imread("/Images/images.jpeg") # Convert the image to a numpy array for further processing image = np.array(img) # Write image to a file imageio.imwrite("/Images/output_image.jpg", image) print("Writing image is completed")
Here is the output after writing the image in the defined file −
Writing image is completed
Writing Images Using PIL(pillow)
With Pillow library we can easily convert the NumPy array back into a PIL.Imageobject and save it using the save() function. Following is the example which shows how to write the image with the help of pillow library −
from PIL import Image import numpy as np # Load the image using Pillow img = Image.open("/Images/images.jpeg") # Convert to a NumPy array img_array = np.array(img) # Convert the NumPy array back to a PIL Image object img = Image.fromarray(img_array) # Save the image to a file img.save('/Images/output_image.png') print("Writing into the file is completed")
Here is the output after writing the image in the defined file using the PIL −
Writing into the file is completed
Handling Color Images
Handling color images in SciPy involves working with image data in the form of multi-dimensional arrays where each dimension corresponds to image attributes such as height, width and color channels. Here's a guide on how to process color images using SciPy and related libraries −
Structure of Color Images
A color image is typically represented as a 3D NumPy array which can be defined as follows −
- Shape: This consists of three attributes namely height, width and channels.
- Channels: The channels are of two types such as RGB(Red, Green, Blue)which is the common format for color image and RGBA which includes an additional alpha channel for transparency.
- Grayscale: It is a single channel typically with shape (height, width)
Converting Color Formats
Converting color formats is a fundamental task in image processing. It involves changing the representation of image colors between different models or formats such as RGB to grayscale, RGBA to RGB or converting images for specific processing tasks.
While scipy itself doesnt directly provide utilities for advanced color format conversion it can be achieved easily using libraries such as Pillow (PIL) and OpenCV often in conjunction with scipy.ndimage module for further processing.
Let's see the common color format conversions using scipy along with other libraries −
RGB to Grayscale
Grayscale reduces the color channels from three namely, Red, Green, Blue to one which represent the intensity. Here is the example of converting the RGB image to grayscale with the help of pillow library −
from PIL import ImageOps from PIL import Image # Load an RGB image img = Image.open("/Images/images.jpeg") # Convert to grayscale gray_img = ImageOps.grayscale(img) # Save or display gray_img.save("/Images/gray_example.jpg") gray_img.show()
Following is the output of the Greyscale image −

RGBA to RGB
If an image includes an alpha channel i.e., transparency then converting it to RGB removes this channel. Below is the example of converting the RGBA image to RGB image using the Pillow library −
from PIL import ImageOps from PIL import Image # Load an RGB image img = Image.open("/Images/images.jpeg") # Convert RGBA to RGB rgb_img = img.convert("RGB") rgb_img.save("/Images/rgb_example.jpg") rgb_img.show()
Here is the output of the RGB image after converting from RGBA image −

Converting Formats for Specific Libraries
When working with image data in Python different libraries have unique requirements for image formats. To ensure compatibility we often need to convert images into the specific formats required by these libraries. Here's an overview of how to handle converting formats for specific libraries like SciPy, Pillow, OpenCV and others.
SciPy (NumPy Array Format)
SciPy primarily works with images as NumPy arrays where we can convert between image file formats and NumPy arrays using libraries like Pillow or imageio.
Following is the example which convert an image to Numpy array using pillow library −
from PIL import Image import numpy as np # Load the image using Pillow img = Image.open("/Images/gray_example.jpeg") # Convert to NumPy array img_array = np.array(img) print(img_array.shape)
Here is the output of the converting the image into numpy array −
(162, 311)
Here is the another example which convert Numpy array into image using pillow library −
from PIL import Image import numpy as np # Load the image using Pillow img = Image.open("/Images/gray_example.jpg") # Convert to NumPy array img_array = np.array(img) # Convert NumPy array back to an image img_reconstructed = Image.fromarray(img_array) # Save the image img_reconstructed.save("/Images/reconstructed.jpg") img_reconstructed.show()
Here is the output of the converting the image into numpy array −

Pillow (Image Object Format)
Pillow uses its own Image object format to ensure compatibility with other libraries where we might need to convert formats.
Below is the example which converts the numpy array to pillow −
from PIL import Image import numpy as np # Create a dummy NumPy array array = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8) # Convert to Pillow Image img = Image.fromarray(array) img.show()
Here is the output of the converting the array into pillow image −

Following is the example which shows how to convert the pillow image into array −
from PIL import Image import numpy as np # Create a dummy NumPy array array = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8) # Convert to Pillow Image img = Image.fromarray(array) # Convert Pillow Image to NumPy array array_from_pillow = np.array(img) print(array_from_pillow.shape)
Here is the output of the converting the pillow image into array −
(100, 100, 3)
Image Processing Libraries in Python
Here are the different common libraries where we can use on images −
S.No | Library & Description |
---|---|
1 |
SciPy
Works with images as NumPy arrays (H W C). Use Pillow or imageio for conversion. |
2 |
Pillow
Uses Image objects. Convert to/from NumPy using Image.fromarray() and np.array(). |
3 |
OpenCV
Uses NumPy arrays in BGR format (H W C). Convert to/from RGB using cv2.cvtColor(). |
4 |
matplotlib
Expects NumPy arrays in RGB format for visualization. Convert using cv2.cvtColor() if needed. |
5 |
imageio
Supports multi-format reading/writing. Outputs images as NumPy arrays directly. |
6 |
TensorFlow/Keras
Expects images as tensors (H W C). Convert from NumPy using tf.convert_to_tensor(). |
7 |
PyTorch
Expects images as tensors in C H W format. Use .permute() for channel order conversion. |