order the images by black value from highest to lowest: from pathlib import Path import…
- User
order the images by black value from highest to lowest: from pathlib import Path import numpy as np from sklearn.cluster import KMeans from PIL import Image
def resize_image(image, target_height): aspect_ratio = image.width / image.height new_height = target_height new_width = int(aspect_ratio * new_height) return image.resize((new_width, new_height))
def find_black_value(image_path): # Open the image and convert it to grayscale with Image.open(image_path) as img: gray_image = img.convert('L')
code# Resize image to height of 512 resized_image = resize_image(gray_image, 512) # Convert image to numpy array of pixel intensities pixel_values = np.array(resized_image).flatten().reshape(-1, 1) # Perform K-Means clustering to find the dominant ('black') cluster kmeans = KMeans(n_clusters=10, random_state=0) kmeans.fit(pixel_values) # The cluster with the lowest center is considered the 'black' cluster value black_value = int(kmeans.cluster_centers_.min()) return black_valuedef process_images_in_directory(directory): directory_path = Path(directory)
code# Collect all image files in the directory and its subdirectories image_files = directory_path.rglob('*.*') results = {} for image_path in image_files: if image_path.suffix.lower() in ['.png', '.jpg', '.jpeg', '.bmp', '.tiff']: black_value = find_black_value(image_path) results[image_path] = black_value print(f"Image: {image_path.name}, Black Value: {black_value}") return resultsif name == "main": image_directory = "D:/Bilder/Python/1. Input" black_values = process_images_in_directory(image_directory)
- Assistant
- User
only look at pixel in the histogramm between 0 - 55
- Assistant
- User
do not resize the image
- Assistant