import os import shutil import cv2 import numpy as np import tkinter as tk from tkinter…
- User
import os import shutil import cv2 import numpy as np import tkinter as tk from tkinter import filedialog, messagebox
def get_images(input_path): """Return all image files from the input path.""" supported_formats = ('.jpg', '.jpeg', '.png', '.bmp', '.tiff') return [f for f in os.listdir(input_path) if f.lower().endswith(supported_formats)]
def find_dominant_color(image_path, k=1): """Find the dominant color of an image.""" img = cv2.imread(image_path) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convert to RGB
code# Reshape the image to a 2D array of pixels and apply k-means pixels = img.reshape((-1, 3)) pixels = np.float32(pixels) _, labels, palette = cv2.kmeans(pixels, k, None, (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0), 10, cv2.KMEANS_RANDOM_CENTERS) dominant_color = palette[np.argmax(np.unique(labels, return_counts=True)[1])] return tuple(dominant_color.astype(int))def sort_images_by_dominant_color(input_path): images = get_images(input_path) if not images: messagebox.showinfo("No Images", "No images found in the selected directory.") return
codeoutput_base_path = os.path.join(input_path, "Sorted_By_Color") if not os.path.exists(output_base_path): os.makedirs(output_base_path) for image_file in images: image_path = os.path.join(input_path, image_file) # Determine the dominant color color = find_dominant_color(image_path) color_folder_name = f"Color_{color[0]:02X}_{color[1]:02X}_{color[2]:02X}" color_folder_path = os.path.join(output_base_path, color_folder_name) if not os.path.exists(color_folder_path): os.makedirs(color_folder_path) shutil.move(image_path, os.path.join(color_folder_path, image_file)) messagebox.showinfo("Sorting Complete", "Images have been sorted by dominant color!") - Assistant
- User
can you print out the entire script
- Assistant
- User
this script has problem recognizing asian character correctly
- Assistant
- User
sorting method is very poor...it just place every image in a folder.
- Assistant
- User
same thing...perhaps you should separate images have the same 3 dominant colors
- Assistant