Mahotas - Saving an Image



Once you have loaded an image and performed various operations on it, you would need to save the image to preserve modifications.

Saving an image refers to the storing of an image in a specific file formats such as PNG, JPEG, or TIFF, among others. This allows you to keep a copy of the image for future use, whether it's for analysis, processing, or simply viewing.

Saving an Image in Mahotas

We use the imsave() function to save images in mahotas. The process of saving an image involves two steps− converting the image array to an appropriate format and then saving it to disk.

Using the imsave() Function

The imsave() function allows you to save an image represented as an array into a file. It supports multiple file formats, including PNG, JPEG, BMP, and more. By specifying the desired filename and the image array as arguments to imsave(), you can easily store the image data on disk.

Following is the syntax of imsave() function in Mahotas −

mahotas.imsave(filename, arr, format=None)

Where,

  • filename − It is a string representing the filename or path of the file where the image will be saved.

  • arr − It is a NumPy array representing the image data that will be saved.

  • Format − It is an optional string representing the file format to use for saving the image. If not specified, Mahotas will attempt to infer the format based on the file extension of the provided 'filename'.

Let's start by saving an image in PNG Format.

Saving an Image as PNG

We can pass the image with the '.png' extension to the imsave() function in order to save an image as PNG format.

Example

Following is the baic example of saving an image as PNG format in Mahotas −

import mahotas as ms
image = ms.imread('sun.png')
# saving the image to a file
ms.imsave('sun_saved.png', image)
print ("The image data is saved.")
Output

After executing the above code, you will find the resulting PNG file 'sun_saved.png' saved in the current working directory −

The image data is saved.

Saving an Image as TIFF

We can also save the image as TIFF formart by passing the image with the '.tiff' extension to the imsave() function.

Example

In here, we are initially loading the image in BMP format, and then saving it as a new image in TIFF format with the filename 'sea_save.tiff' −

import mahotas as ms
# Loading the image in BMP format
image_bmp = ms.imread('sea.bmp')
# Saving the image in TIFF format
ms.imsave('sea_save.tiff', image_bmp)
print ("The filename sea_save.tiff is saved.")
Output

After executing the above code, you will find the resulting TIFF file 'sea_saved.tiff' saved in the current working directory −

The filename sea_save.tiff is saved.

Saving a Grayscale Image in JPEG Format

The Grayscale images are black and white images, where each pixel represents the intensity or brightness of that particular point. It does not have any color information. Think of it as a black and white photograph.

To save a grayscale image in Mahotas, we need to first need to specify the dimensions of the image and generate random pixel values for each point in the image. Then, we need to create the grayscale image using these pixel values and display it.

Example

Following is an example to save a grayscale image in JPEG format. Here, we are generating a random grayscale image using NumPy, with dimensions 256×256 and pixel values ranging from 0 to 39 (inclusive)−

import mahotas as mh
import numpy as np
# Creating a random grayscale image
image = np.random.randint(40, 100, size=(256, 256), dtype=np.uint8)
# Saving the image
file_path = 'natures.jpeg'
mh.imsave(file_path, image)
print("Grayscale image is saved.")
Output

Once we execute the above code, we will find the resulting JPEG file 'natures.jpeg' saved in the current working directory −

Grayscale image is saved.

Let us look at difference between both the images.

Input Image

Following is the original image (before saving) −

Saving an Image

Output Image

After executing the above code, the image saved is as follows −

Saving an Image1
Advertisements