Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- from PIL import Image, ImageDraw, ImageFont
- import os
- import math
- # Define the list of hex codes
- colors = [
- "#000000", "#808080", "#5C4033", "#A52A2A", "#D2B48C",
- "#FFE5B4", "#FF0000", "#FF5349", "#FFA500", "#FFFF00",
- "#9ACD32", "#008000", "#006400", "#00BCA1", "#ADD8E6",
- "#0000FF", "#FFC0CB", "#7F00FF", "#341539", "#FD3DB5"
- ]
- # Define a function to convert a color to its closest approximation
- def closest_color(color):
- min_distance = float("inf")
- closest_color_index = -1
- for i, c in enumerate(colors):
- c_rgb = tuple(int(c.lstrip("#")[i:i+2], 16) for i in (0, 2, 4))
- distance = np.sqrt(np.sum((np.array(color) - np.array(c_rgb)) ** 2))
- if distance < min_distance:
- min_distance = distance
- closest_color_index = i
- return closest_color_index
- # Define a function to convert an image to a grid of interlocking hexagons
- def image_to_hexagons(input_image_path, output_image_path, hexagon_size):
- image = Image.open(input_image_path)
- image = image.convert('RGB') # Convert image to RGB mode
- pixels = image.load()
- width, height = image.size
- hexagon_width = int(width / (hexagon_size * 1.5))
- hexagon_height = int(height / (hexagon_size * math.sqrt(3) / 2))
- grid = np.zeros((hexagon_height, hexagon_width), dtype=int)
- for i in range(hexagon_height):
- for j in range(hexagon_width):
- hexagon_pixels = []
- for x in range(int(j * hexagon_size * 1.5), int((j + 1) * hexagon_size * 1.5)):
- for y in range(int(i * hexagon_size * math.sqrt(3) / 2), int((i + 1) * hexagon_size * math.sqrt(3) / 2)):
- if x < width and y < height:
- hexagon_pixels.append(pixels[x, y])
- if hexagon_pixels:
- avg_color = tuple(int(np.mean([pixel[k] for pixel in hexagon_pixels])) for k in range(3))
- grid[i, j] = closest_color(avg_color)
- # Create a new image with the hexagon pattern
- hexagon_image = Image.new("RGB", (int(hexagon_width * hexagon_size * 1.5), int(hexagon_height * hexagon_size * math.sqrt(3) / 2)), (255, 255, 255))
- draw = ImageDraw.Draw(hexagon_image)
- font = ImageFont.load_default()
- for i in range(hexagon_height):
- for j in range(hexagon_width):
- points = []
- for k in range(6):
- angle = k * math.pi / 3
- x = int(j * hexagon_size * 1.5 + hexagon_size * math.cos(angle))
- y = int(i * hexagon_size * math.sqrt(3) / 2 + hexagon_size * math.sin(angle))
- points.append((x, y))
- draw.polygon(points, outline=(0, 0, 0), width=1)
- text = str(grid[i, j])
- text_width, text_height = font.getmask(text).size
- x = int(j * hexagon_size * 1.5 + hexagon_size / 2 - text_width / 2)
- y = int(i * hexagon_size * math.sqrt(3) / 2 + hexagon_size / 2 - text_height / 2)
- draw.text((x, y), text, fill=(0, 0, 0), font=font)
- # Create a new folder for the output
- output_folder = output_image_path
- if not os.path.exists(output_folder):
- os.makedirs(output_folder)
- # Save the hexagon image
- hexagon_image.save(os.path.join(output_folder, "hexagons.png"))
- # Create a new image with the colored hexagon pattern
- colored_image = Image.new("RGB", (int(hexagon_width * hexagon_size * 1.5), int(hexagon_height * hexagon_size * math.sqrt(3) / 2)), (255, 255, 255))
- draw = ImageDraw.Draw(colored_image)
- for i in range(hexagon_height):
- for j in range(hexagon_width):
- points = []
- for k in range(6):
- angle = k * math.pi / 3
- x = int(j * hexagon_size * 1.5 + hexagon_size * math.cos(angle))
- y = int(i * hexagon_size * math.sqrt(3) / 2 + hexagon_size * math.sin(angle))
- points.append((x, y))
- color_index = grid[i, j]
- color = tuple(int(colors[color_index].lstrip("#")[i:i+2], 16) for i in (0, 2, 4))
- draw.polygon(points, fill=color, outline=(0, 0, 0), width=1)
- # Save the colored image
- colored_image.save(os.path.join(output_folder, "colored.png"))
- # Get user input
- input_image_path = input("Enter the name of the input image: ")
- output_image_path = input("Enter the name for the output image: ")
- hexagon_size = int(input("Enter the size of the hexagons: "))
- # Call the function
- image_to_hexagons(input_image_path, output_image_path, hexagon_size)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement