Scikit-image 简明教程

Scikit Image - Writing Images

编写或保存图像在图像处理和计算机视觉任务中起着至关重要的作用。在执行图像处理操作(例如裁剪、缩放、旋转或应用各种滤镜)时,保存图像很有必要。

Writing or Saving images play a crucial role in image processing and computer vision tasks. It is necessary to save images when performing image processing operations such as cropping, resizing, rotating, or applying various filters.

编写图像指的是将图像保存或存储为磁盘上或内存中的外部文件的过程。在此过程中,我们通常会指定所需的图像格式(例如 JPEG、PNG、TIFF 等)并提供文件名或路径。

Writing an image refers to the process of saving or storing an image as an external file on a disk or in memory. During this process, we usually specify the desired file format (such as JPEG, PNG, TIFF, etc.) and provide the file name or path.

在 scikit-image 库中,io 模块提供了一个名为 imsave() 的函数,专门用于将图像作为外部文件写入并存储。该函数对于保存图像处理操作的结果很有用。

In the scikit-image library, the io module offers a function called imsave() specifically for writing and storing images as external files. This function is useful for saving the results of image manipulation operations.

The io.imsave() method

通过使用 imsave() 方法,可以将图像写入外部文件,并指定所需的文件格式、文件名和所需位置。以下是该方法的语法 -

By using imsave() method, writing the image to an external file with the desired file format, file name, and desired location can be possible. Following is the syntax of this method −

skimage.io.imsave(fname, arr, plugin=None, check_contrast=True, **plugin_args)

这个函数采用以下参数 −

The function takes the following parameters −

  1. fname − A string representing the file name or path where the image will be saved. The format of the file is obtained from the extension of file name.

  2. arr − The NumPy array of shape (M,N) or (M,N,3) or (M,N,4) containing the image data to be saved.

  3. plugin (optional) − A string specifying the plugin to use for saving the image. If the plugin parameter is not provided, the function will automatically try different plugins, starting with imageio, until a suitable one is found. However, if the file name (fname) has a ".tiff" extension, the tifffile plugin will be used by default.

  4. check_contrast (optional) − A boolean indicating whether to check the contrast of the image and print warning. The default value is True.

  5. **plugin_args (optional) − Additional keyword arguments that are passed to the specified plugin.

Example 1

以下示例将一个随机数据的数组写到一个外部图像文件 (.jpg),使用 imsave() 方法。

The following example writes an array of random data to an external image file (.jpg) using the imsave() method.

import numpy as np
from skimage import io
# Generate an image with random numbers
image_data = np.random.randint(0, 256, size=(256, 256, 3), dtype=np.uint8)
# Save the random image as a JPG file
io.imsave('Output/Random_image.jpg', image_data)
print("The image was saved successfully.")
The image was saved successfully.

如果我们导航到该图像保存到的目录,我们就能观察到已保存的 "Random_image.jpg" 图像,如下所示 −

If we navigate to the directory where the image was saved, we will be able to observe the saved "Random_image.jpg" image as shown below −

writing images 1

Example 2

以下示例将一个图像数组写到一个 TIFF 文件 (.tiff),使用 imsave() 方法。

The following example writes an image array to a TIFF file (.tiff) using the imsave() method.

import numpy as np
from skimage import io
# Read an image
image_data = io.imread('Images/logo-w.png')
print("The input image properties:")
print('Type:', type(image_data))
print('Shape:', image_data.shape)
# Save the image as a tiff file
io.imsave('Output/logo.tiff', image_data)
print("The image was saved successfully...")
The input image properties:
Type: < class 'numpy.ndarray' >
Shape: (225, 225, 4)
The image was saved successfully...

以下图像表示了已保存的 "logo.tiff" 文件,在其保存到的目录中。

The following image represents the saved "logo.tiff" file in the directory where the image was saved.

writing images 2

Example 3

以下示例演示如何使用 imsave() 方法将一个图像保存到一个 JPEG 文件 (.jpeg),且为低质量。

The following example demonstrates how to save an image to a JPEG file (.jpeg) with low quality using the imsave() method.

import numpy as np
from skimage import io
# Read an image
image = io.imread('Images/Flower1.jpg')
# Save the image as a JPEG file
io.imsave('Output/flower.jpeg', image, plugin='pil', quality=10)
# Display the image array properties
print("The input image properties:")
print('Type:', type(image))
print('Shape:', image.shape)
print("The image was saved successfully...")
The input image properties:
Type: < class 'numpy.ndarray'>
Shape: (4000, 6000, 3)
The image was saved successfully...

以下图像代表了在各自目录中的输入文件 (Flower1.jpg) 和已保存文件 (flower.jpeg) 的详细信息。

The following images represent details of both input (Flower1.jpg) and saved (flower.jpeg) files in the respective directories.

writing images 3
writing images 4