Creating a python script that detects colors with high accuracy. /u/LongjumpingSet7810 Python Education

Hello all,

I’m having trouble with this script that I’m working on. I do not understand how I can write the code so it can identify any color and tell me without my defining it. BTW I’m relatively new to Python so I don’t understand some of the complex theories behind why some things work etc.

This code will detect colors in an image that I upload and split the image into 4 squares, from there it’ll identify the color that is most present in that square. Then it’ll create an output image.

Here is my current code:

from PIL import Image, ImageDraw, ImageFont from collections import Counter def get_dominant_color(image): small_image = image.resize((1, 1)) return small_image.getpixel((0, 0)) def rgb_to_name(rgb): colors = { "red": (255, 0, 0), "green": (0, 255, 0), "blue": (0, 0, 255), "yellow": (255, 255, 0), "cyan": (0, 255, 255), "magenta": (255, 0, 255), "black": (0, 0, 0), "white": (255, 255, 255), "gray": (128, 128, 128), "light purple": (200, 162, 200), "teal green": (0, 128, 128), "orange": (255, 165, 0), "pink": (255, 192, 203), "lime": (50, 205, 50), "navy blue": (0, 0, 128), "maroon": (128, 0, 0), "olive": (128, 128, 0), } closest_color = min( colors.items(), key=lambda item: sum((c1 - c2) ** 2 for c1, c2 in zip(rgb, item[1])) ) return closest_color[0] def split_image_into_squares(image, grid_size): width, height = image.size square_width = width // grid_size square_height = height // grid_size squares = [] for row in range(grid_size): for col in range(grid_size): left = col * square_width upper = row * square_height right = (col + 1) * square_width lower = (row + 1) * square_height box = (left, upper, right, lower) squares.append(image.crop(box)) return squares def draw_text_on_image(image, texts, grid_size): draw = ImageDraw.Draw(image) width, height = image.size square_width = width // grid_size square_height = height // grid_size try: font = ImageFont.truetype("arial.ttf", size=20) except IOError: font = ImageFont.load_default() for i, text in enumerate(texts): col = i % grid_size row = i // grid_size x = col * square_width + square_width // 2 y = row * square_height + square_height // 2 text_position = (x, y) draw.text(text_position, text, fill="black", font=font, anchor="mm") def main(image_path): try: image = Image.open(image_path) grid_size = 4 squares = split_image_into_squares(image, grid_size) dominant_colors = [rgb_to_name(get_dominant_color(square)) for square in squares] draw_text_on_image(image, dominant_colors, grid_size) output_path = "output.jpg" image.save(output_path) print(f"Output saved to {output_path}") except Exception as e: print(f"Error processing image: {e}") if __name__ == "__main__": image_path = "input.jpg" main(image_path) 

submitted by /u/LongjumpingSet7810
[link] [comments]

​r/learnpython Hello all, I’m having trouble with this script that I’m working on. I do not understand how I can write the code so it can identify any color and tell me without my defining it. BTW I’m relatively new to Python so I don’t understand some of the complex theories behind why some things work etc. This code will detect colors in an image that I upload and split the image into 4 squares, from there it’ll identify the color that is most present in that square. Then it’ll create an output image. Here is my current code: from PIL import Image, ImageDraw, ImageFont from collections import Counter def get_dominant_color(image): small_image = image.resize((1, 1)) return small_image.getpixel((0, 0)) def rgb_to_name(rgb): colors = { “red”: (255, 0, 0), “green”: (0, 255, 0), “blue”: (0, 0, 255), “yellow”: (255, 255, 0), “cyan”: (0, 255, 255), “magenta”: (255, 0, 255), “black”: (0, 0, 0), “white”: (255, 255, 255), “gray”: (128, 128, 128), “light purple”: (200, 162, 200), “teal green”: (0, 128, 128), “orange”: (255, 165, 0), “pink”: (255, 192, 203), “lime”: (50, 205, 50), “navy blue”: (0, 0, 128), “maroon”: (128, 0, 0), “olive”: (128, 128, 0), } closest_color = min( colors.items(), key=lambda item: sum((c1 – c2) ** 2 for c1, c2 in zip(rgb, item[1])) ) return closest_color[0] def split_image_into_squares(image, grid_size): width, height = image.size square_width = width // grid_size square_height = height // grid_size squares = [] for row in range(grid_size): for col in range(grid_size): left = col * square_width upper = row * square_height right = (col + 1) * square_width lower = (row + 1) * square_height box = (left, upper, right, lower) squares.append(image.crop(box)) return squares def draw_text_on_image(image, texts, grid_size): draw = ImageDraw.Draw(image) width, height = image.size square_width = width // grid_size square_height = height // grid_size try: font = ImageFont.truetype(“arial.ttf”, size=20) except IOError: font = ImageFont.load_default() for i, text in enumerate(texts): col = i % grid_size row = i // grid_size x = col * square_width + square_width // 2 y = row * square_height + square_height // 2 text_position = (x, y) draw.text(text_position, text, fill=”black”, font=font, anchor=”mm”) def main(image_path): try: image = Image.open(image_path) grid_size = 4 squares = split_image_into_squares(image, grid_size) dominant_colors = [rgb_to_name(get_dominant_color(square)) for square in squares] draw_text_on_image(image, dominant_colors, grid_size) output_path = “output.jpg” image.save(output_path) print(f”Output saved to {output_path}”) except Exception as e: print(f”Error processing image: {e}”) if __name__ == “__main__”: image_path = “input.jpg” main(image_path) submitted by /u/LongjumpingSet7810 [link] [comments] 

Hello all,

I’m having trouble with this script that I’m working on. I do not understand how I can write the code so it can identify any color and tell me without my defining it. BTW I’m relatively new to Python so I don’t understand some of the complex theories behind why some things work etc.

This code will detect colors in an image that I upload and split the image into 4 squares, from there it’ll identify the color that is most present in that square. Then it’ll create an output image.

Here is my current code:

from PIL import Image, ImageDraw, ImageFont from collections import Counter def get_dominant_color(image): small_image = image.resize((1, 1)) return small_image.getpixel((0, 0)) def rgb_to_name(rgb): colors = { "red": (255, 0, 0), "green": (0, 255, 0), "blue": (0, 0, 255), "yellow": (255, 255, 0), "cyan": (0, 255, 255), "magenta": (255, 0, 255), "black": (0, 0, 0), "white": (255, 255, 255), "gray": (128, 128, 128), "light purple": (200, 162, 200), "teal green": (0, 128, 128), "orange": (255, 165, 0), "pink": (255, 192, 203), "lime": (50, 205, 50), "navy blue": (0, 0, 128), "maroon": (128, 0, 0), "olive": (128, 128, 0), } closest_color = min( colors.items(), key=lambda item: sum((c1 - c2) ** 2 for c1, c2 in zip(rgb, item[1])) ) return closest_color[0] def split_image_into_squares(image, grid_size): width, height = image.size square_width = width // grid_size square_height = height // grid_size squares = [] for row in range(grid_size): for col in range(grid_size): left = col * square_width upper = row * square_height right = (col + 1) * square_width lower = (row + 1) * square_height box = (left, upper, right, lower) squares.append(image.crop(box)) return squares def draw_text_on_image(image, texts, grid_size): draw = ImageDraw.Draw(image) width, height = image.size square_width = width // grid_size square_height = height // grid_size try: font = ImageFont.truetype("arial.ttf", size=20) except IOError: font = ImageFont.load_default() for i, text in enumerate(texts): col = i % grid_size row = i // grid_size x = col * square_width + square_width // 2 y = row * square_height + square_height // 2 text_position = (x, y) draw.text(text_position, text, fill="black", font=font, anchor="mm") def main(image_path): try: image = Image.open(image_path) grid_size = 4 squares = split_image_into_squares(image, grid_size) dominant_colors = [rgb_to_name(get_dominant_color(square)) for square in squares] draw_text_on_image(image, dominant_colors, grid_size) output_path = "output.jpg" image.save(output_path) print(f"Output saved to {output_path}") except Exception as e: print(f"Error processing image: {e}") if __name__ == "__main__": image_path = "input.jpg" main(image_path) 

submitted by /u/LongjumpingSet7810
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *