Learn Scikit Image – Writing Images work project make money

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.

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.

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

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 −

  • 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.
  • arr − The NumPy array of shape (M,N) or (M,N,3) or (M,N,4) containing the image data to be saved.
  • 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.
  • check_contrast (optional) − A boolean indicating whether to check the contrast of the image and print warning. The default value is True.
  • **plugin_args (optional) − Additional keyword arguments that are passed to the specified plugin.

Example 1

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

Output

The image was saved successfully.

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

Example 2

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

Output

The input image properties:
Type: < class ''numpy.ndarray'' >
Shape: (225, 225, 4)
The image was saved successfully...

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

Writing Images

Example 3

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

Output

The input image properties:
Type: < class ''numpy.ndarray''>
Shape: (4000, 6000, 3)
The image was saved successfully...

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

Input file

Writing Images

Saved file

Writing Images

Leave a Reply

Your email address will not be published. Required fields are marked *