Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can a specific tint be added to grayscale images in scikit-learn in Python?
Adding tints to grayscale images involves manipulating the RGB channel values to create color effects. In scikit-image (part of the scikit-learn ecosystem), we convert grayscale images to RGB format and apply color multipliers to achieve different tints.
Required Libraries
First, let's import the necessary modules ?
import matplotlib.pyplot as plt from skimage import data, color from skimage import io import numpy as np
Loading and Converting Image
We'll use a sample image from scikit-image's dataset and convert it to grayscale ?
# Load sample image (you can replace with your own image path)
orig_img = data.chelsea() # Sample cat image from scikit-image
# Convert to grayscale
grayscale_img = color.rgb2gray(orig_img)
# Convert grayscale back to RGB (3-channel) for tinting
rgb_img = color.gray2rgb(grayscale_img)
print(f"Original shape: {orig_img.shape}")
print(f"Grayscale shape: {grayscale_img.shape}")
print(f"RGB converted shape: {rgb_img.shape}")
Original shape: (300, 451, 3) Grayscale shape: (300, 451) RGB converted shape: (300, 451, 3)
Applying Color Tints
We define color multipliers for different tints and apply them to create colored versions ?
# Define color multipliers for different tints red_tint = [1.2, 0.3, 0.3] # Red tint blue_tint = [0.3, 0.3, 1.2] # Blue tint yellow_tint = [1.0, 1.0, 0.2] # Yellow tint green_tint = [0.3, 1.2, 0.3] # Green tint # Apply tints by multiplying with color multipliers red_tinted = np.clip(rgb_img * red_tint, 0, 1) blue_tinted = np.clip(rgb_img * blue_tint, 0, 1) yellow_tinted = np.clip(rgb_img * yellow_tint, 0, 1) green_tinted = np.clip(rgb_img * green_tint, 0, 1)
Displaying Results
Let's visualize the original grayscale image alongside the tinted versions ?
# Create subplot to display images
fig, axes = plt.subplots(2, 3, figsize=(15, 10))
# Display original and tinted images
axes[0, 0].imshow(grayscale_img, cmap='gray')
axes[0, 0].set_title('Original Grayscale')
axes[0, 0].axis('off')
axes[0, 1].imshow(red_tinted)
axes[0, 1].set_title('Red Tint')
axes[0, 1].axis('off')
axes[0, 2].imshow(blue_tinted)
axes[0, 2].set_title('Blue Tint')
axes[0, 2].axis('off')
axes[1, 0].imshow(yellow_tinted)
axes[1, 0].set_title('Yellow Tint')
axes[1, 0].axis('off')
axes[1, 1].imshow(green_tinted)
axes[1, 1].set_title('Green Tint')
axes[1, 1].axis('off')
axes[1, 2].axis('off') # Hide empty subplot
plt.tight_layout()
plt.show()
Custom Tint Function
Here's a reusable function to apply any custom tint to grayscale images ?
def apply_tint(grayscale_img, tint_color):
"""
Apply a color tint to a grayscale image.
Parameters:
grayscale_img: 2D numpy array (grayscale image)
tint_color: list of 3 values [R, G, B] for tint multipliers
Returns:
Tinted RGB image
"""
# Convert grayscale to RGB
rgb_img = color.gray2rgb(grayscale_img)
# Apply tint and clip values to valid range
tinted_img = np.clip(rgb_img * tint_color, 0, 1)
return tinted_img
# Example usage
sepia_tint = [1.0, 0.8, 0.6] # Sepia effect
purple_tint = [0.8, 0.4, 1.0] # Purple tint
sepia_img = apply_tint(grayscale_img, sepia_tint)
purple_img = apply_tint(grayscale_img, purple_tint)
# Display results
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(12, 4))
ax1.imshow(grayscale_img, cmap='gray')
ax1.set_title('Original Grayscale')
ax1.axis('off')
ax2.imshow(sepia_img)
ax2.set_title('Sepia Tint')
ax2.axis('off')
ax3.imshow(purple_img)
ax3.set_title('Purple Tint')
ax3.axis('off')
plt.tight_layout()
plt.show()
Key Points
Color Multipliers: Values greater than 1 enhance a channel, while values less than 1 reduce it
Clipping: Use
np.clip()to ensure pixel values stay within [0, 1] rangeRGB Conversion:
color.gray2rgb()converts single-channel grayscale to 3-channel RGBTint Effects: Warm tints use higher red/yellow values, cool tints use higher blue values
Conclusion
Adding tints to grayscale images in scikit-image involves converting to RGB format and applying color multipliers. The np.clip() function ensures valid pixel ranges, while different multiplier combinations create various artistic effects.
