color-theory-proposal / color_classification.py
color_classification.py
Raw
'''Script to automatically determine if presented photo is warm or cool or neutral depedning on mean pixel color values compared to threshold in HSV. '''

from PIL import Image
import numpy as np

def load_image(image_path):
    try:
        return Image.open(image_path)
    except IOError:
        print(f"Error: Unable to load image from {image_path}")
        return None

def rgb_to_hsv(rgb):
    if rgb is None:
        return None
    return rgb.convert('HSV')

def categorize_image_based_on_hue(hsv):
    if hsv is None:
        return None

    np_hsv = np.array(hsv)
    average_hue = np.mean(np_hsv[:, :, 0])

    warm_hue_range = (0, 50)
    cool_hue_range = (180, 270)

    if warm_hue_range[0] <= average_hue <= warm_hue_range[1]:
        return "Warm"
    elif cool_hue_range[0] <= average_hue <= cool_hue_range[1]:
        return "Cool"
    else:
        return "Neutral"

def main(image_path):
    rgb_image = load_image(image_path)
    hsv_image = rgb_to_hsv(rgb_image)
    category = categorize_image_based_on_hue(hsv_image)

    if category is not None:
        print(f"The image is categorized as: {category}")
    else:
        print("Unable to categorize the image.")

if __name__ == "__main__":
    image_path = 'path_to_your_image.jpg'
    main(image_path)