5 Best Ways to Add a Specific Tint to Grayscale Images in Scikit-Learn in Python

πŸ’‘ Problem Formulation: Grayscale images do not contain color information, presenting tones of black and white only. However, you may want to add a specific tint to these images, for artistic effects, visualization enhancement, or to code information into them. We will explore how to add a specific tint, such as a sepia effect, to a grayscale image using Python’s Scikit-learn library, turning a pure grayscale image into a tinted one.

Method 1: Using NumPy Array Operations

This method involves manipulating the grayscale image, represented as a NumPy array, by broadcasting a color tint across all pixels. The grayscale image already contains luminance data, and by multiplying each pixel by the desired RGB tint, we introduce color uniformly across the image.

Here’s an example:

import numpy as np
from skimage import color
from skimage import io
import matplotlib.pyplot as plt

# Load grayscale image
image = io.imread('path_to_grayscale_image.jpg', as_gray=True)

# Convert to RGB
image_rgb = color.gray2rgb(image)

# Define tint (e.g., Gold tint)
tint = np.array([212, 175, 55]) / 255

# Apply tint to grayscale image
tinted_image = image_rgb * tint

# Display the tinted image
plt.imshow(tinted_image)
plt.axis('off')
plt.show()

The output will be the grayscale image displayed with a uniform gold tint.

In this code snippet, we first load a grayscale image and convert it to a three-channel RGB image. We then define our desired tint as an RGB triplet (here, a gold color) and normalize it by dividing by 255 to map it to [0,1]. Finally, we multiply our grayscale image by this tint to achieve our tinted image.

Method 2: Using Matplotlib Colormaps

Matplotlib provides a variety of colormaps that can be used to add tints to images. By applying a colormap to a grayscale image, each grayscale level is mapped to a color in the colormap. This is a quick and flexible way to tint an image.

Here’s an example:

import matplotlib.pyplot as plt
from skimage import io
from matplotlib.cm import get_cmap

# Load the grayscale image
image = io.imread('path_to_grayscale_image.jpg', as_gray=True)

# Get the colormap
cmap = get_cmap('copper')

# Apply colormap to grayscale image
tinted_image = cmap(image)

# Display the tinted image
plt.imshow(tinted_image)
plt.axis('off')
plt.show()

The output is the grayscale image tinted with the ‘copper’ colormap provided by Matplotlib.

The code loads the grayscale image, retrieves the ‘copper’ colormap using Matplotlib, and applies it to the image. The resulting image is then displayed without any axis for a clean look.

Method 3: Gradient Tinting

Gradient tinting allows for the application of a gradient of colors to an image. Instead of a uniform tinting, the tint varies across the image, which can be used to create more dynamic and visually appealing results.

Here’s an example:

import numpy as np
from skimage import color
from skimage import io
from matplotlib.colors import LinearSegmentedColormap
import matplotlib.pyplot as plt

# Load the grayscale image
image = io.imread('path_to_grayscale_image.jpg', as_gray=True)

# Convert image to RGB
image_rgb = color.gray2rgb(image)

# Define custom gradient colors (e.g., from blue to red)
colors = ["blue", "red"]
cmap = LinearSegmentedColormap.from_list("gradient", colors, N=256)

# Apply gradient tint
tinted_image = cmap(image)

# Display the tinted image
plt.imshow(tinted_image, interpolation='nearest')
plt.axis('off')
plt.show()

The output will be a grayscale image with a gradient tint ranging from blue at the bottom to red at the top.

We load the grayscale image, convert it to RGB for color manipulation, then create a custom LinearSegmentedColormap with a blue to red gradient. After applying this to our image, we display the resulting tinted image.

Method 4: Image Overlaying

Image overlaying involves creating a tint layer with the desired color and alpha blending it with the grayscale image. This mimics the effect of applying a translucent colored filter over a photograph.

Here’s an example:

import numpy as np
from skimage import io
import matplotlib.pyplot as plt

# Load the grayscale image
image = io.imread('path_to_grayscale_image.jpg', as_gray=True)

# Create a tint layer
tint_layer = np.zeros(image.shape + (3,))  # Initialize RGB channels
tint_layer[:, :] = [212, 175, 55]  # Set the tint color

# Alpha blend the tint layer with the grayscale image
alpha = 0.5  # Set blending factor
tinted_image = (1 - alpha) * image[:, :, np.newaxis] + alpha * tint_layer

# Display the tinted image
plt.imshow(tinted_image)
plt.axis('off')
plt.show()

The output is a grayscale image overlaid with a semi-transparent gold tint layer, resulting in a blended tinted image.

We create a transparent layer in the same dimension as the grayscale image, fill it with the tint color, and then blend it with the original image using an alpha factor. Adjusting the alpha factor allows control over the intensity of the tint.

Bonus One-Liner Method 5: Direct Application with skimage.color Module

For a fast and efficient one-liner solution, you can directly apply a tint to a grayscale image using skimage.color’s gray2rgb function combined with a tinting operation.

Here’s an example:

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

# Load grayscale image, convert to RGB, apply sepia tint in one line
tinted_image = color.gray2rgb(io.imread('path_to_grayscale_image.jpg', as_gray=True)) * [212, 175, 55]

# Display the tinted image
plt.imshow(tinted_image / 255)
plt.axis('off')
plt.show()

The output is the original grayscale image with a sepia tint applied.

This one-liner reads and converts the image, then applies the sepia tint, all at once. The multiplication is broadcasting the tint color across all pixels.

Summary/Discussion

  • Method 1: NumPy Array Operations. Provides full control over color manipulation. Requires manual normalization of colors.
  • Method 2: Matplotlib Colormaps. Quick and convenient for standard colormaps. Limited by available predefined colormaps.
  • Method 3: Gradient Tinting. Suitable for dynamic visual effects. Somewhat more complex due to the necessity of defining gradient intervals.
  • Method 4: Image Overlaying. Simulates physical tinting process. Adjusting alpha blending is required for desired effect.
  • Method 5: Direct Application. Efficient one-liner using skimage. Limited customizability compared to other methods.