Spaces:
Sleeping
Sleeping
import os | |
import shutil | |
from PIL import Image | |
def colors_within_tolerance(color1, color2, tolerance): | |
return all(abs(c1 - c2) <= tolerance for c1, c2 in zip(color1, color2)) | |
def check_border_colors(image_path, tolerance): | |
# Open the image | |
image = Image.open(image_path) | |
pixels = image.load() | |
width, height = image.size | |
# Get the color of the first pixel on the left and right borders | |
left_border_color = pixels[0, 0] | |
right_border_color = pixels[width - 1, 0] | |
# Check the left border | |
for y in range(height): | |
if not colors_within_tolerance(pixels[0, y], left_border_color, tolerance): | |
return False | |
# Check the right border | |
for y in range(height): | |
if not colors_within_tolerance(pixels[width - 1, y], right_border_color, tolerance): | |
return False | |
return True | |
def process_images(input_folder, output_folder_same, output_folder_different, tolerance): | |
# Ensure output directories exist | |
os.makedirs(output_folder_same, exist_ok=True) | |
os.makedirs(output_folder_different, exist_ok=True) | |
for filename in os.listdir(input_folder): | |
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')): | |
image_path = os.path.join(input_folder, filename) | |
if check_border_colors(image_path, tolerance): | |
shutil.copy(image_path, os.path.join(output_folder_same, filename)) | |
else: | |
shutil.copy(image_path, os.path.join(output_folder_different, filename)) | |
# Example usage | |
input_folder = 'D:\Ardha\Kerja\AISensum\ROX\Task 1' | |
output_folder_same = 'D:\Ardha\Kerja\AISensum\ROX\Task 1\warna_sama' | |
output_folder_different = 'D:\Ardha\Kerja\AISensum\ROX\Task 1\warna_berbeda' | |
tolerance = 50 # Adjust the tolerance value as needed | |
process_images(input_folder, output_folder_same, output_folder_different, tolerance) | |