Mahotas 简明教程

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.

保存图像指的是将图像以特定文件格式(如 PNG、JPEG 或 TIFF 等)存储起来的操作。这样一来,您可以保留图像的副本以便将来使用,无论是用于分析、处理还是简单地查看。

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

我们使用 imsave() 函数在 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

imsave() 函数允许您将表示为数组的图像保存到文件中。它支持多种文件格式,包括 PNG、JPEG、BMP 等。通过将所需的文件名和图像数组指定为 imsave() 的参数,您可以轻松地将图像数据存储到磁盘中。

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.

以下是 imsave() 函数在 Mahotas 中的语法 −

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

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

其中,

Where,

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

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

  3. 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'.

首先保存为 PNG 格式的图像。

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

Saving an Image as PNG

我们可以将扩展名为“png”的图像传递给 imsave() 函数,以便将图像保存为 PNG 格式。

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

以下是使用 Mahotas 将图像另存为 PNG 格式的基本示例 −

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.")

执行上述代码后,您将找到当前工作目录中保存的名为“sun_saved.png”的结果 PNG 文件 −

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

我们还可以通过将扩展名为“tiff”的图像传递给 imsave() 函数来将图像另存为 TIFF 格式。

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

在这里,我们最初加载 BMP 格式的图像,然后将其另存为 TIFF 格式的新图像,文件名“sea_save.tiff” −

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.")

执行上述代码后,您将找到当前工作目录中保存的名为“sea_saved.tiff”的结果 TIFF 文件 −

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.

要在 Mahotas 中保存灰度图像,首先需要指定图像尺寸并为图像中的每个点生成随机像素值。然后,我们需要使用这些像素值创建灰度图像并显示它。

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.

以下是保存 JPEG 格式灰度图像的示例。此处,我们使用 NumPy 生成了一个灰度图像,其维度为 256×256,像素值范围为 0 到 39(包括)-

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.")

执行以上代码之后,我们会在当前工作目录中找到保存的文件“natures.jpeg”中 JPEG 文件 -

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.

以下是原始图像(保存前) -

Following is the original image (before saving) −

saving an image

执行以上代码后,保存的图像如下 -

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

saving an image1