Advertisement
utahman3431

Untitled

Apr 9th, 2025
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.50 KB | None | 0 0
  1. import numpy as np
  2. from PIL import Image, ImageDraw, ImageFont
  3. import os
  4. import math
  5.  
  6. # Define the list of hex codes
  7. colors = [
  8.     "#000000", "#808080", "#5C4033", "#A52A2A", "#D2B48C",
  9.     "#FFE5B4", "#FF0000", "#FF5349", "#FFA500", "#FFFF00",
  10.     "#9ACD32", "#008000", "#006400", "#00BCA1", "#ADD8E6",
  11.     "#0000FF", "#FFC0CB", "#7F00FF", "#341539", "#FD3DB5"
  12. ]
  13.  
  14. # Define a function to convert a color to its closest approximation
  15. def closest_color(color):
  16.     min_distance = float("inf")
  17.     closest_color_index = -1
  18.     for i, c in enumerate(colors):
  19.         c_rgb = tuple(int(c.lstrip("#")[i:i+2], 16) for i in (0, 2, 4))
  20.         distance = np.sqrt(np.sum((np.array(color) - np.array(c_rgb)) ** 2))
  21.         if distance < min_distance:
  22.             min_distance = distance
  23.             closest_color_index = i
  24.     return closest_color_index
  25.  
  26. # Define a function to convert an image to a grid of interlocking hexagons
  27. def image_to_hexagons(input_image_path, output_image_path, hexagon_size):
  28.     image = Image.open(input_image_path)
  29.     image = image.convert('RGB')  # Convert image to RGB mode
  30.     pixels = image.load()
  31.     width, height = image.size
  32.     hexagon_width = int(width / (hexagon_size * 1.5))
  33.     hexagon_height = int(height / (hexagon_size * math.sqrt(3) / 2))
  34.     grid = np.zeros((hexagon_height, hexagon_width), dtype=int)
  35.  
  36.     for i in range(hexagon_height):
  37.         for j in range(hexagon_width):
  38.             hexagon_pixels = []
  39.             for x in range(int(j * hexagon_size * 1.5), int((j + 1) * hexagon_size * 1.5)):
  40.                 for y in range(int(i * hexagon_size * math.sqrt(3) / 2), int((i + 1) * hexagon_size * math.sqrt(3) / 2)):
  41.                     if x < width and y < height:
  42.                         hexagon_pixels.append(pixels[x, y])
  43.             if hexagon_pixels:
  44.                 avg_color = tuple(int(np.mean([pixel[k] for pixel in hexagon_pixels])) for k in range(3))
  45.                 grid[i, j] = closest_color(avg_color)
  46.  
  47.     # Create a new image with the hexagon pattern
  48.     hexagon_image = Image.new("RGB", (int(hexagon_width * hexagon_size * 1.5), int(hexagon_height * hexagon_size * math.sqrt(3) / 2)), (255, 255, 255))
  49.     draw = ImageDraw.Draw(hexagon_image)
  50.     font = ImageFont.load_default()
  51.     for i in range(hexagon_height):
  52.         for j in range(hexagon_width):
  53.             points = []
  54.             for k in range(6):
  55.                 angle = k * math.pi / 3
  56.                 x = int(j * hexagon_size * 1.5 + hexagon_size * math.cos(angle))
  57.                 y = int(i * hexagon_size * math.sqrt(3) / 2 + hexagon_size * math.sin(angle))
  58.                 points.append((x, y))
  59.             draw.polygon(points, outline=(0, 0, 0), width=1)
  60.             text = str(grid[i, j])
  61.             text_width, text_height = font.getmask(text).size
  62.             x = int(j * hexagon_size * 1.5 + hexagon_size / 2 - text_width / 2)
  63.             y = int(i * hexagon_size * math.sqrt(3) / 2 + hexagon_size / 2 - text_height / 2)
  64.             draw.text((x, y), text, fill=(0, 0, 0), font=font)
  65.  
  66.     # Create a new folder for the output
  67.     output_folder = output_image_path
  68.     if not os.path.exists(output_folder):
  69.         os.makedirs(output_folder)
  70.  
  71.     # Save the hexagon image
  72.     hexagon_image.save(os.path.join(output_folder, "hexagons.png"))
  73.  
  74.     # Create a new image with the colored hexagon pattern
  75.     colored_image = Image.new("RGB", (int(hexagon_width * hexagon_size * 1.5), int(hexagon_height * hexagon_size * math.sqrt(3) / 2)), (255, 255, 255))
  76.     draw = ImageDraw.Draw(colored_image)
  77.     for i in range(hexagon_height):
  78.         for j in range(hexagon_width):
  79.             points = []
  80.             for k in range(6):
  81.                 angle = k * math.pi / 3
  82.                 x = int(j * hexagon_size * 1.5 + hexagon_size * math.cos(angle))
  83.                 y = int(i * hexagon_size * math.sqrt(3) / 2 + hexagon_size * math.sin(angle))
  84.                 points.append((x, y))
  85.             color_index = grid[i, j]
  86.             color = tuple(int(colors[color_index].lstrip("#")[i:i+2], 16) for i in (0, 2, 4))
  87.             draw.polygon(points, fill=color, outline=(0, 0, 0), width=1)
  88.  
  89.     # Save the colored image
  90.     colored_image.save(os.path.join(output_folder, "colored.png"))
  91.  
  92. # Get user input
  93. input_image_path = input("Enter the name of the input image: ")
  94. output_image_path = input("Enter the name for the output image: ")
  95. hexagon_size = int(input("Enter the size of the hexagons: "))
  96.  
  97. # Call the function
  98. image_to_hexagons(input_image_path, output_image_path, hexagon_size)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement