Scikit Image - Alpha Channel



In digital images, the alpha channel represents the degree of transparency or opacity of a color. It is an additional channel that works along with the red, green, and blue channels (RGBA) in an image. It is used to determine how a pixel is rendered when blended with another pixel or when overlaying an image on top of a background. Each pixel in an image can have an associated alpha value, which indicates the pixel opacity.

Formats like TIFF, PNG, and WebP support transparency and can store an alpha channel along with the color channels. In these formats, each pixel can have an associated alpha value ranging from 0 (completely transparent) to 1 (completely opaque). On the other hand, formats like jpg do not support transparency and do not include an alpha channel.

In the scikit-image library, the color module offers functions like gray2rgba and rgba2rgb() to work with alpha channels in images.

Removing alpha channel

The skimage.color.rgba2rgb() method is used to convert an image from the RGBA format to the RGB format using alpha blending (Removing alpha channel). Following is the syntax of this method:

skimage.color.rgba2rgb(rgba, background=(1, 1, 1), *, channel_axis=-1)

Here are the parameters of the method −

  • rgba: It is an array-like object representing the image in RGBA format. The last dimension has a size of 4, which denotes the RGBA channels.
  • background: It is an array-like object representing the color of the background to blend the image with. The background color is specified as a tuple of three floats ranging from 0 to 1, representing the RGB value of the background.
  • channel_axis: An optional parameter indicating which axis of the array corresponds to channels. This parameter was introduced in scikit-image new version 0.19.

It returns a ndarray representing the image in RGB format. The shape of the output array is the same as the input, except that the last dimension is reduced to 3, representing the RGB channels.

Example

The following example demonstrates how to convert an image from the RGBA format to the RGB format using color.rgba2rgb() method.

from skimage import color, io
import matplotlib.pyplot as plt

# Read an RGBA image
rgba_image = io.imread('Images/logo-w.png')
print('Shape of the input RGBA image array:',rgba_image.shape)

# Convert RGBA to RGB
rgb_image = color.rgba2rgb(rgba_image)

# Print the result
print('Shape of the output RGB image array:',rgb_image.shape)

# Display the RGBA and RGB images 
fig, axes = plt.subplots(nrows=1, ncols=2) # figsize=(10, 5)

axes[0].imshow(rgba_image)
axes[0].set_title('Input RGBA Image')
axes[0].axis('off')

axes[1].imshow(rgb_image)
axes[1].set_title('Output RGB Image')
axes[1].axis('off')

plt.tight_layout()
plt.show()

Output

On executing the above program, you will get the following output −

Shape of the input RGBA image array: (225, 225, 4)
Shape of the output RGB image array: (225, 225, 3)
RGBA

Adding alpha channel

The skimages color.gray2rgba() method is used to create an RGBA representation of a gray-level image. It generates an ndarray representing the RGBA image from the input gray image. A new dimension of length 4 is added to the shape of the input image, representing the RGBA channels. Following is the syntax of this method.

skimage.color.gray2rgba(image, alpha=None, *, channel_axis=-1)

The method accepts the following parameters −

  • image: It is an array-like object representing the input gray-level image.
  • alpha: An optional parameter representing the alpha channel of the output image. It can be a scalar or an array that can be broadcast to the shape of the input image. If not specified, it is set to the maximum limit corresponding to the dtype of the input image.
  • channel_axis: An optional parameter indicating which axis of the output array will correspond to channels. This parameter was introduced in scikit-image version 0.19.

Example

The following code demonstrates the conversion of the gray-level image to an RGBA image using the skimage.color.gray2rgba() method.

from skimage import color, io
import matplotlib.pyplot as plt

# Read an RGB image as grayscale image
gray_image = io.imread('Images/Fruits.jpg', as_gray=True)

print('Shape of the input Gray image array:', gray_image.shape)

# Convert Gray to RGBA
rgba_image = color.gray2rgba(gray_image)

# Print the result
print('Shape of the output RGBA image array:', rgba_image.shape)

# Display the RGB and RGBA images
fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(10, 10))

axes[0].imshow(gray_image)
axes[0].set_title('Input Gray Image')
axes[0].axis('off')

axes[1].imshow(rgba_image)
axes[1].set_title('Output RGBA Image')
axes[1].axis('off')

plt.tight_layout()
plt.show()

Output

Shape of the input Gray image array: (257, 698)
Shape of the output RGBA image array: (257, 698, 4)
RGB
Advertisements