To distinguish images that are supposed to be grayscale but have unintended color tones from real color images, you can write a Python program that analyzes the color content of each image. Here's a program that processes images in the input folder D:\Bilder\Python\1. Input, classifies them based on their color content, and saves them into subfolders within the output folder D:\Bilder\Python\2. Output.
This program uses the Python Imaging Library (Pillow) to handle image files and NumPy for numerical operations.
import os
from PIL import Image
import numpy as np
input_folder = r'D:\Bilder\Python\1. Input'
output_folder = r'D:\Bilder\Python\2. Output'
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Threshold for color difference between RGB channels to consider a pixel as colored
diff_threshold = 5 # Adjust this value as needed (range: 0-255)
# Iterate over all image files in the input folder
for filename in os.listdir(input_folder):
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')):
# Full path of the image file
image_path = os.path.join(input_folder, filename)
# Open the image and ensure it's in RGB format
image = Image.open(image_path).convert('RGB')
# Convert the image to a NumPy array
image_np = np.array(image)
total_pixels = image_np.shape[0] * image_np.shape[1]
# Separate the RGB channels and convert to int16 to prevent overflow
R = image_np[:, :, 0].astype(np.int16)
G = image_np[:, :, 1].astype(np.int16)
B = image_np[:, :, 2].astype(np.int16)
# Compute the absolute differences between each pair of RGB channels
diff_RG = np.abs(R - G)
diff_RB = np.abs(R - B)
diff_GB = np.abs(G - B)
# Find the maximum difference among the three pairs for each pixel
max_diff = np.maximum(np.maximum(diff_RG, diff_RB), diff_GB)
# Count the number of pixels where the maximum difference exceeds the threshold
num_colored_pixels = np.sum(max_diff > diff_threshold)
fraction_colored_pixels = num_colored_pixels / total_pixels
# Classify the image based on the fraction of colored pixels
if fraction_colored_pixels < 0.001:
classification = 'Pure Grayscale Image'
elif fraction_colored_pixels < 0.05:
classification = 'Grayscale Image with Color Tones'
else:
classification = 'Color Image'
print(f"{filename}: {classification} (Fraction colored pixels: {fraction_colored_pixels:.4f})")
# Create a subfolder in the output directory for the classification
output_subfolder = os.path.join(output_folder, classification)
if not os.path.exists(output_subfolder):
os.makedirs(output_subfolder)
# Save the image to the appropriate subfolder
output_image_path = os.path.join(output_subfolder, filename)
image.save(output_image_path)
Explanation of the Program:
Adjustments Based on Your Data:
-
Thresholds:
- The
diff_threshold determines how sensitive the program is to small color differences.
- The classification thresholds (0.001 and 0.05) determine how strict the classifications are.
- You may need to adjust these values depending on the specific characteristics of your images.
-
Color Sensitivity:
- If your images have minor color artifacts, reducing the
diff_threshold can help detect them.
- Increasing the threshold makes the program less sensitive to small color variations.
Prerequisites:
- Install Pillow (PIL) if you haven't already:
pip install Pillow
- Ensure you have NumPy installed:
pip install numpy
Running the Program:
Note:
- Always back up your images before processing them.
- Test the program with a few images to ensure it works as expected before running it on your entire dataset.
This program should help you effectively distinguish between pure grayscale images, grayscale images with unintended color tones, and real color images, organizing them neatly for further processing or inspection.