Spaces:
Running
on
Zero
Running
on
Zero
Update Hex generation to allow Triangles, Squares and Hexagons, the only 3 shapes that allow tiling
Browse files- app.py +6 -4
- utils/color_utils.py +73 -1
- utils/hex_grid.py +76 -28
app.py
CHANGED
|
@@ -67,6 +67,7 @@ from utils.image_utils import (
|
|
| 67 |
from utils.hex_grid import (
|
| 68 |
generate_hexagon_grid,
|
| 69 |
generate_hexagon_grid_interface,
|
|
|
|
| 70 |
)
|
| 71 |
|
| 72 |
from utils.excluded_colors import (
|
|
@@ -1360,10 +1361,11 @@ with gr.Blocks(css_paths="style_20250314.css", title=title, theme='Surn/beeuty',
|
|
| 1360 |
end_x = gr.Number(label="End X", value=-20, minimum=-512, maximum= 512, precision=0)
|
| 1361 |
end_y = gr.Number(label="End Y", value=-20, minimum=-512, maximum= 512, precision=0)
|
| 1362 |
with gr.Row():
|
| 1363 |
-
x_spacing = gr.Number(label="Adjust Horizontal spacing", value=-
|
| 1364 |
y_spacing = gr.Number(label="Adjust Vertical spacing", value=3, minimum=-200, maximum=200, precision=1)
|
| 1365 |
with gr.Row():
|
| 1366 |
rotation = gr.Slider(-90, 180, 0.0, 0.1, label="Hexagon Rotation (degree)")
|
|
|
|
| 1367 |
add_hex_text = gr.Dropdown(label="Add Text to Hexagons", choices=[None, "Column-Row Coordinates", "Column(Letter)-Row Coordinates", "Column-Row(Letter) Coordinates", "Sequential Numbers", "Playing Cards Sequential", "Playing Cards Alternate Red and Black", "Custom List"], value=None)
|
| 1368 |
with gr.Row():
|
| 1369 |
custom_text_list = gr.TextArea(label="Custom Text List", value=constants.cards_alternating, visible=False,)
|
|
@@ -1486,9 +1488,9 @@ with gr.Blocks(css_paths="style_20250314.css", title=title, theme='Surn/beeuty',
|
|
| 1486 |
delete_button.click(fn=delete_color, inputs=[selected_row, color_display], outputs=[color_display])
|
| 1487 |
exclude_color_button.click(fn=add_color, inputs=[color_picker, gr.State(excluded_color_list)], outputs=[color_display, gr.State(excluded_color_list)])
|
| 1488 |
hex_button.click(
|
| 1489 |
-
fn=lambda hex_size, border_size, input_image, start_x, start_y, end_x, end_y, rotation, background_color, background_opacity, border_color, border_opacity, fill_hex, color_display, filter_color, x_spacing, y_spacing, add_hex_text, custom_text_list, custom_text_color_list:
|
| 1490 |
-
gr.Warning("Please upload an Input Image to get started.") if input_image is None else hex_create(hex_size, border_size, input_image, start_x, start_y, end_x, end_y, rotation, background_color, background_opacity, border_color, border_opacity, fill_hex, color_display, filter_color, x_spacing, y_spacing, add_hex_text, custom_text_list, custom_text_color_list),
|
| 1491 |
-
inputs=[hex_size, border_size, input_image, start_x, start_y, end_x, end_y, rotation, background_color, background_opacity, border_color, border_opacity, fill_hex, color_display, filter_color, x_spacing, y_spacing, add_hex_text, custom_text_list, custom_text_color_list],
|
| 1492 |
outputs=[output_image, overlay_image],
|
| 1493 |
scroll_to_output=True
|
| 1494 |
)
|
|
|
|
| 67 |
from utils.hex_grid import (
|
| 68 |
generate_hexagon_grid,
|
| 69 |
generate_hexagon_grid_interface,
|
| 70 |
+
map_sides
|
| 71 |
)
|
| 72 |
|
| 73 |
from utils.excluded_colors import (
|
|
|
|
| 1361 |
end_x = gr.Number(label="End X", value=-20, minimum=-512, maximum= 512, precision=0)
|
| 1362 |
end_y = gr.Number(label="End Y", value=-20, minimum=-512, maximum= 512, precision=0)
|
| 1363 |
with gr.Row():
|
| 1364 |
+
x_spacing = gr.Number(label="Adjust Horizontal spacing", value=-10, minimum=-200, maximum=200, precision=1)
|
| 1365 |
y_spacing = gr.Number(label="Adjust Vertical spacing", value=3, minimum=-200, maximum=200, precision=1)
|
| 1366 |
with gr.Row():
|
| 1367 |
rotation = gr.Slider(-90, 180, 0.0, 0.1, label="Hexagon Rotation (degree)")
|
| 1368 |
+
sides = gr.Dropdown(label="Grid Shapes", info="other choices do not form grids",choices=["triangle", "square", "hexagon"], value="hexagon")
|
| 1369 |
add_hex_text = gr.Dropdown(label="Add Text to Hexagons", choices=[None, "Column-Row Coordinates", "Column(Letter)-Row Coordinates", "Column-Row(Letter) Coordinates", "Sequential Numbers", "Playing Cards Sequential", "Playing Cards Alternate Red and Black", "Custom List"], value=None)
|
| 1370 |
with gr.Row():
|
| 1371 |
custom_text_list = gr.TextArea(label="Custom Text List", value=constants.cards_alternating, visible=False,)
|
|
|
|
| 1488 |
delete_button.click(fn=delete_color, inputs=[selected_row, color_display], outputs=[color_display])
|
| 1489 |
exclude_color_button.click(fn=add_color, inputs=[color_picker, gr.State(excluded_color_list)], outputs=[color_display, gr.State(excluded_color_list)])
|
| 1490 |
hex_button.click(
|
| 1491 |
+
fn=lambda hex_size, border_size, input_image, start_x, start_y, end_x, end_y, rotation, sides, background_color, background_opacity, border_color, border_opacity, fill_hex, color_display, filter_color, x_spacing, y_spacing, add_hex_text, custom_text_list, custom_text_color_list:
|
| 1492 |
+
gr.Warning("Please upload an Input Image to get started.") if input_image is None else hex_create(hex_size, border_size, input_image, start_x, start_y, end_x, end_y, rotation, background_color, background_opacity, border_color, border_opacity, fill_hex, color_display, filter_color, x_spacing, y_spacing, add_hex_text, custom_text_list, custom_text_color_list, map_sides(sides)),
|
| 1493 |
+
inputs=[hex_size, border_size, input_image, start_x, start_y, end_x, end_y, rotation, sides, background_color, background_opacity, border_color, border_opacity, fill_hex, color_display, filter_color, x_spacing, y_spacing, add_hex_text, custom_text_list, custom_text_color_list],
|
| 1494 |
outputs=[output_image, overlay_image],
|
| 1495 |
scroll_to_output=True
|
| 1496 |
)
|
utils/color_utils.py
CHANGED
|
@@ -5,6 +5,7 @@ import re
|
|
| 5 |
import cairocffi as cairo
|
| 6 |
import pangocffi
|
| 7 |
import pangocairocffi
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
def multiply_and_clamp(value, scale, min_value=0, max_value=255):
|
|
@@ -196,7 +197,7 @@ def draw_text_with_emojis(image, text, font_color, offset_x, offset_y, font_name
|
|
| 196 |
# Set text color
|
| 197 |
r, g, b, a = parse_hex_color(font_color)
|
| 198 |
context.set_source_rgba(r , g , b , a )
|
| 199 |
-
# Move to the position (top-left corner adjusted to
|
| 200 |
context.move_to(offset_x, offset_y)
|
| 201 |
# Render the text
|
| 202 |
pangocairocffi.show_layout(context, layout)
|
|
@@ -211,4 +212,75 @@ def draw_text_with_emojis(image, text, font_color, offset_x, offset_y, font_name
|
|
| 211 |
"BGRA", # Cairo stores data in BGRA order
|
| 212 |
surface.get_stride(),
|
| 213 |
).convert("RGBA")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
return modified_image
|
|
|
|
| 5 |
import cairocffi as cairo
|
| 6 |
import pangocffi
|
| 7 |
import pangocairocffi
|
| 8 |
+
import math
|
| 9 |
|
| 10 |
|
| 11 |
def multiply_and_clamp(value, scale, min_value=0, max_value=255):
|
|
|
|
| 197 |
# Set text color
|
| 198 |
r, g, b, a = parse_hex_color(font_color)
|
| 199 |
context.set_source_rgba(r , g , b , a )
|
| 200 |
+
# Move to the position (top-left corner adjusted to position the text)
|
| 201 |
context.move_to(offset_x, offset_y)
|
| 202 |
# Render the text
|
| 203 |
pangocairocffi.show_layout(context, layout)
|
|
|
|
| 212 |
"BGRA", # Cairo stores data in BGRA order
|
| 213 |
surface.get_stride(),
|
| 214 |
).convert("RGBA")
|
| 215 |
+
return modified_image
|
| 216 |
+
|
| 217 |
+
def draw_rotated_text_with_emojis(image, text, font_color, offset_x, offset_y, font_name, font_size, angle=0):
|
| 218 |
+
"""
|
| 219 |
+
Draws text with emojis directly onto the given PIL image at specified coordinates with the specified color and rotation.
|
| 220 |
+
Parameters:
|
| 221 |
+
image (PIL.Image.Image): The RGBA image to draw on.
|
| 222 |
+
text (str): The text to draw, including emojis.
|
| 223 |
+
font_color (tuple): RGBA color tuple for the text (e.g., (255, 0, 0, 255)).
|
| 224 |
+
offset_x (int): The x-coordinate for the text center position.
|
| 225 |
+
offset_y (int): The y-coordinate for the text center position.
|
| 226 |
+
font_name (str): The name of the font family.
|
| 227 |
+
font_size (int): Size of the font.
|
| 228 |
+
angle (float): Rotation angle in degrees (default is 0, positive values rotate counter-clockwise).
|
| 229 |
+
Returns:
|
| 230 |
+
PIL.Image.Image: The modified image with the text drawn.
|
| 231 |
+
"""
|
| 232 |
+
if image.mode != 'RGBA':
|
| 233 |
+
raise ValueError("Image must be in RGBA mode.")
|
| 234 |
+
# Convert PIL image to a mutable bytearray
|
| 235 |
+
img_data = bytearray(image.tobytes("raw", "BGRA"))
|
| 236 |
+
|
| 237 |
+
# Create a Cairo ImageSurface that wraps the image's data
|
| 238 |
+
surface = cairo.ImageSurface.create_for_data(
|
| 239 |
+
img_data,
|
| 240 |
+
cairo.FORMAT_ARGB32,
|
| 241 |
+
image.width,
|
| 242 |
+
image.height,
|
| 243 |
+
image.width * 4
|
| 244 |
+
)
|
| 245 |
+
context = cairo.Context(surface)
|
| 246 |
+
# Create Pango layout
|
| 247 |
+
layout = pangocairocffi.create_layout(context)
|
| 248 |
+
layout._set_text(text)
|
| 249 |
+
# Set font description
|
| 250 |
+
desc = pangocffi.FontDescription()
|
| 251 |
+
desc._set_family(font_name)
|
| 252 |
+
desc._set_size(pangocffi.units_from_double(font_size))
|
| 253 |
+
layout._set_font_description(desc)
|
| 254 |
+
# Get the ink and logical rectangles of the layout
|
| 255 |
+
#ink_rect, logical_rect = layout.get_extents()
|
| 256 |
+
# Extract width and height from the logical rectangle, convert from pango units to pixels
|
| 257 |
+
#width = logical_rect.width / 1024.0
|
| 258 |
+
#height = logical_rect.height /1024.0
|
| 259 |
+
# Set text color
|
| 260 |
+
r, g, b, a = parse_hex_color(font_color)
|
| 261 |
+
context.set_source_rgba(r, g, b, a)
|
| 262 |
+
# Save the current context state
|
| 263 |
+
context.save()
|
| 264 |
+
# Move to the position (top-left corner adjusted to position the text)
|
| 265 |
+
context.translate(offset_x, offset_y)
|
| 266 |
+
# Rotate by the specified angle (convert degrees to radians)
|
| 267 |
+
context.rotate(math.radians(angle))
|
| 268 |
+
# Move to the position to center the text at (0, 0) in the transformed context
|
| 269 |
+
#context.move_to(-width / 2,-height / 2)
|
| 270 |
+
# Render the text
|
| 271 |
+
pangocairocffi.show_layout(context, layout)
|
| 272 |
+
# Restore the original context state
|
| 273 |
+
context.restore()
|
| 274 |
+
# Flush the surface to ensure all drawing operations are complete
|
| 275 |
+
surface.flush()
|
| 276 |
+
# Convert the modified bytearray back to a PIL Image
|
| 277 |
+
modified_image = Image.frombuffer(
|
| 278 |
+
"RGBA",
|
| 279 |
+
(image.width, image.height),
|
| 280 |
+
bytes(img_data),
|
| 281 |
+
"raw",
|
| 282 |
+
"BGRA",
|
| 283 |
+
surface.get_stride(),
|
| 284 |
+
).convert("RGBA")
|
| 285 |
+
|
| 286 |
return modified_image
|
utils/hex_grid.py
CHANGED
|
@@ -11,7 +11,7 @@ from utils.excluded_colors import (
|
|
| 11 |
excluded_color_list,
|
| 12 |
)
|
| 13 |
from utils.image_utils import multiply_and_blend_images
|
| 14 |
-
from utils.color_utils import update_color_opacity, parse_hex_color,
|
| 15 |
import random # For random text options
|
| 16 |
import utils.constants as constants # Import constants
|
| 17 |
import ast
|
|
@@ -33,6 +33,10 @@ def calculate_font_size(hex_size, padding=0.6, size_ceil=20, min_font_size=8):
|
|
| 33 |
return None # Hex is too small for text
|
| 34 |
return min(font_size, size_ceil)
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
def generate_hexagon_grid(hex_size, border_size, input_image=None, image_width=0, image_height=0, start_x=0, start_y=0, end_x=0, end_y=0, rotation=0, background_color="#ede9ac44", border_color="#12165380", fill_hex=True, excluded_color_list=excluded_color_list, filter_color=False, x_spacing=0, y_spacing=0, sides=6):
|
| 37 |
if input_image:
|
| 38 |
image_width, image_height = input_image.size
|
|
@@ -67,13 +71,13 @@ def generate_hexagon_grid(hex_size, border_size, input_image=None, image_width=0
|
|
| 67 |
draw = ImageDraw.Draw(image, mode="RGBA")
|
| 68 |
hex_width = hex_size * 2
|
| 69 |
hex_height = hex_size * 2
|
| 70 |
-
hex_horizontal_spacing =
|
| 71 |
hex_vertical_spacing = hex_height + (hex_border_size if hex_border_size < 0 else 0) + y_spacing
|
| 72 |
col = 0
|
| 73 |
row = 0
|
| 74 |
def draw_hexagon(x, y, color="#FFFFFFFF", rotation=0, outline_color="#12165380", outline_width=0, sides=6):
|
| 75 |
-
#side_length = (hex_size * 2) / math.sqrt(3)
|
| 76 |
-
side_length = 2 * hex_size * math.tan(math.pi / sides)
|
| 77 |
points = [(x + side_length * math.cos(math.radians(angle + rotation)), y + side_length * math.sin(math.radians(angle + rotation))) for angle in range(0, 360, (360 // sides))]
|
| 78 |
draw.polygon(points, fill=color, outline=outline_color, width=max(-5, outline_width))
|
| 79 |
# Function to range a floating number
|
|
@@ -88,11 +92,32 @@ def generate_hexagon_grid(hex_size, border_size, input_image=None, image_width=0
|
|
| 88 |
col = 0
|
| 89 |
for x in frange(start_x, max(image_width + additional_width, image_width, rotated_image_width) + (end_x - start_x), hex_horizontal_spacing):
|
| 90 |
col += 1
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
if rotated_input_image:
|
| 97 |
# Sample the colors of the pixels in the hexagon, if fill_hex is True
|
| 98 |
if fill_hex:
|
|
@@ -121,12 +146,12 @@ def generate_hexagon_grid(hex_size, border_size, input_image=None, image_width=0
|
|
| 121 |
else:
|
| 122 |
print(f"color found: {avg_color}")
|
| 123 |
#draw_hexagon(x + x_offset, y + y_offset, color="#{:02x}{:02x}{:02x}{:02x}".format(*avg_color if fill_hex else (0,0,0,0)), outline_color=border_color, outline_width=hex_border_size)
|
| 124 |
-
draw_hexagon(x + x_offset, y + y_offset, color="#{:02x}{:02x}{:02x}{:02x}".format(*avg_color), outline_color=border_color, outline_width=hex_border_size, sides=sides)
|
| 125 |
else:
|
| 126 |
-
draw_hexagon(x + x_offset, y + y_offset, color="#000000", outline_color=border_color, outline_width=hex_border_size, sides=sides)
|
| 127 |
else:
|
| 128 |
color = "#%02x%02x%02x%02x" % (128, math.ceil(y) % 255, math.ceil(x) % 255, 255) if fill_hex else (0,0,0,0)
|
| 129 |
-
draw_hexagon(x + x_offset, y + y_offset, color=color, outline_color=border_color, outline_width=hex_border_size, sides=sides)
|
| 130 |
if rotation != 0:
|
| 131 |
#image.show()
|
| 132 |
# Rotate the final image
|
|
@@ -178,8 +203,8 @@ def generate_hexagon_grid_with_text(hex_size, border_size, input_image=None, ima
|
|
| 178 |
draw = ImageDraw.Draw(image, mode="RGBA")
|
| 179 |
hex_width = hex_size * 2
|
| 180 |
hex_height = hex_size * 2
|
| 181 |
-
hex_horizontal_spacing = (hex_width
|
| 182 |
-
hex_vertical_spacing = hex_height + (hex_border_size if hex_border_size < 0 else 0) + y_spacing
|
| 183 |
col = 0
|
| 184 |
row = 0
|
| 185 |
## Function to draw optional text
|
|
@@ -213,8 +238,8 @@ def generate_hexagon_grid_with_text(hex_size, border_size, input_image=None, ima
|
|
| 213 |
pass
|
| 214 |
hex_index = -1 # Initialize hex index
|
| 215 |
def draw_hexagon(x, y, color="#FFFFFFFF", rotation=0, outline_color="#12165380", outline_width=0, sides=6):
|
| 216 |
-
#side_length = (hex_size * 2) / math.sqrt(3)
|
| 217 |
-
side_length = 2 * hex_size * math.tan(math.pi / sides)
|
| 218 |
points = [(x + side_length * math.cos(math.radians(angle + rotation)), y + side_length * math.sin(math.radians(angle + rotation))) for angle in range(0, 360, (360 // sides))]
|
| 219 |
draw.polygon(points, fill=color, outline=outline_color, width=max(-5, outline_width))
|
| 220 |
# Function to range a floating number
|
|
@@ -230,11 +255,33 @@ def generate_hexagon_grid_with_text(hex_size, border_size, input_image=None, ima
|
|
| 230 |
for x in frange(start_x, max(image_width + additional_width, image_width, rotated_image_width) + (end_x - start_x), hex_horizontal_spacing):
|
| 231 |
col += 1
|
| 232 |
hex_index += 1 # Increment hex index
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
if rotated_input_image:
|
| 239 |
# Sample the colors of the pixels in the hexagon, if fill_hex is True
|
| 240 |
if fill_hex:
|
|
@@ -263,12 +310,12 @@ def generate_hexagon_grid_with_text(hex_size, border_size, input_image=None, ima
|
|
| 263 |
else:
|
| 264 |
print(f"color found: {avg_color}")
|
| 265 |
#draw_hexagon(x + x_offset, y + y_offset, color="#{:02x}{:02x}{:02x}{:02x}".format(*avg_color if fill_hex else (0,0,0,0)), outline_color=border_color, outline_width=hex_border_size, sides=sides)
|
| 266 |
-
draw_hexagon(x + x_offset, y + y_offset, color="#{:02x}{:02x}{:02x}{:02x}".format(*avg_color), outline_color=border_color, outline_width=hex_border_size, sides=sides)
|
| 267 |
else:
|
| 268 |
-
draw_hexagon(x + x_offset, y + y_offset, color="#00000000", outline_color=border_color, outline_width=hex_border_size, sides=sides)
|
| 269 |
else:
|
| 270 |
color = "#%02x%02x%02x%02x" % (128, math.ceil(y) % 255, math.ceil(x) % 255, 255) if fill_hex else (0,0,0,0)
|
| 271 |
-
draw_hexagon(x + x_offset, y + y_offset, color=color, outline_color=border_color, outline_width=hex_border_size, sides=sides)
|
| 272 |
# Draw text in hexagon
|
| 273 |
if add_hex_text_option != None:
|
| 274 |
font_size = calculate_font_size(hex_size, 0.333, 20, 7)
|
|
@@ -322,18 +369,19 @@ def generate_hexagon_grid_with_text(hex_size, border_size, input_image=None, ima
|
|
| 322 |
# Calculate position to center text in hexagon
|
| 323 |
# text_x = x + x_offset - (text_width / 2)
|
| 324 |
# text_y = y + y_offset - (text_height / 2)
|
| 325 |
-
# Calculate position to
|
| 326 |
text_x = x + x_offset - (hex_size / 1.75)
|
| 327 |
-
text_y = y + y_offset - (hex_size / 1.
|
| 328 |
# Draw the text directly onto the image
|
| 329 |
-
font_image =
|
| 330 |
image=font_image,
|
| 331 |
text=text,
|
| 332 |
font_color=update_color_opacity(text_color,255),
|
| 333 |
offset_x=text_x,
|
| 334 |
offset_y=text_y,
|
| 335 |
font_name=font_name,
|
| 336 |
-
font_size=font_size
|
|
|
|
| 337 |
)
|
| 338 |
# # Use Pilmoji to draw text with emojis
|
| 339 |
# with Pilmoji(image) as pilmoji:
|
|
|
|
| 11 |
excluded_color_list,
|
| 12 |
)
|
| 13 |
from utils.image_utils import multiply_and_blend_images
|
| 14 |
+
from utils.color_utils import update_color_opacity, parse_hex_color, draw_rotated_text_with_emojis, hex_to_rgb
|
| 15 |
import random # For random text options
|
| 16 |
import utils.constants as constants # Import constants
|
| 17 |
import ast
|
|
|
|
| 33 |
return None # Hex is too small for text
|
| 34 |
return min(font_size, size_ceil)
|
| 35 |
|
| 36 |
+
def map_sides(selected_side):
|
| 37 |
+
mapping = {"triangle": 3, "square": 4, "hexagon": 6}
|
| 38 |
+
return mapping[selected_side]
|
| 39 |
+
|
| 40 |
def generate_hexagon_grid(hex_size, border_size, input_image=None, image_width=0, image_height=0, start_x=0, start_y=0, end_x=0, end_y=0, rotation=0, background_color="#ede9ac44", border_color="#12165380", fill_hex=True, excluded_color_list=excluded_color_list, filter_color=False, x_spacing=0, y_spacing=0, sides=6):
|
| 41 |
if input_image:
|
| 42 |
image_width, image_height = input_image.size
|
|
|
|
| 71 |
draw = ImageDraw.Draw(image, mode="RGBA")
|
| 72 |
hex_width = hex_size * 2
|
| 73 |
hex_height = hex_size * 2
|
| 74 |
+
hex_horizontal_spacing = hex_width + (hex_border_size if hex_border_size < 0 else 0) + x_spacing #* 0.8660254
|
| 75 |
hex_vertical_spacing = hex_height + (hex_border_size if hex_border_size < 0 else 0) + y_spacing
|
| 76 |
col = 0
|
| 77 |
row = 0
|
| 78 |
def draw_hexagon(x, y, color="#FFFFFFFF", rotation=0, outline_color="#12165380", outline_width=0, sides=6):
|
| 79 |
+
#side_length = (hex_size * 2) / math.sqrt(3) #hexagons only
|
| 80 |
+
side_length = 2 * hex_size * math.tan(math.pi / sides) #hexagons, squares, triangle can tile
|
| 81 |
points = [(x + side_length * math.cos(math.radians(angle + rotation)), y + side_length * math.sin(math.radians(angle + rotation))) for angle in range(0, 360, (360 // sides))]
|
| 82 |
draw.polygon(points, fill=color, outline=outline_color, width=max(-5, outline_width))
|
| 83 |
# Function to range a floating number
|
|
|
|
| 92 |
col = 0
|
| 93 |
for x in frange(start_x, max(image_width + additional_width, image_width, rotated_image_width) + (end_x - start_x), hex_horizontal_spacing):
|
| 94 |
col += 1
|
| 95 |
+
|
| 96 |
+
# Calculate offsets based on the number of sides
|
| 97 |
+
if sides == 4:
|
| 98 |
+
# Squares line up perfectly; no vertical offset is needed.
|
| 99 |
+
x_offset = int(hex_width + hex_horizontal_spacing * 2)
|
| 100 |
+
y_offset = 0
|
| 101 |
+
rotation_offset = -45
|
| 102 |
+
elif sides == 3:
|
| 103 |
+
# For equilateral triangles, you might offset rows by about one-third
|
| 104 |
+
# of the triangle�s height. Adjust as needed.
|
| 105 |
+
x_offset = hex_width + hex_horizontal_spacing
|
| 106 |
+
y_offset = int((hex_height * 2) + hex_vertical_spacing)
|
| 107 |
+
rotation_offset = -60
|
| 108 |
+
# Adjust y_offset for columns 1 and 3 to overlap
|
| 109 |
+
if col % 2 == 1:
|
| 110 |
+
y_offset -= (hex_height // 2) #* 0.8660254
|
| 111 |
+
rotation_offset = 0
|
| 112 |
+
else:
|
| 113 |
+
# Default behavior (6 sides)
|
| 114 |
+
x_offset = hex_width // 2
|
| 115 |
+
y_offset = (hex_height // 2) #* 1.15470054342517
|
| 116 |
+
rotation_offset = 0
|
| 117 |
+
# Adjust y_offset for columns 1 and 3 to overlap
|
| 118 |
+
if col % 2 == 1:
|
| 119 |
+
y_offset -= (hex_height // 2) #* 0.8660254
|
| 120 |
+
|
| 121 |
if rotated_input_image:
|
| 122 |
# Sample the colors of the pixels in the hexagon, if fill_hex is True
|
| 123 |
if fill_hex:
|
|
|
|
| 146 |
else:
|
| 147 |
print(f"color found: {avg_color}")
|
| 148 |
#draw_hexagon(x + x_offset, y + y_offset, color="#{:02x}{:02x}{:02x}{:02x}".format(*avg_color if fill_hex else (0,0,0,0)), outline_color=border_color, outline_width=hex_border_size)
|
| 149 |
+
draw_hexagon(x + x_offset, y + y_offset, color="#{:02x}{:02x}{:02x}{:02x}".format(*avg_color), rotation=rotation_offset, outline_color=border_color, outline_width=hex_border_size, sides=sides)
|
| 150 |
else:
|
| 151 |
+
draw_hexagon(x + x_offset, y + y_offset, color="#000000", rotation=rotation_offset, outline_color=border_color, outline_width=hex_border_size, sides=sides)
|
| 152 |
else:
|
| 153 |
color = "#%02x%02x%02x%02x" % (128, math.ceil(y) % 255, math.ceil(x) % 255, 255) if fill_hex else (0,0,0,0)
|
| 154 |
+
draw_hexagon(x + x_offset, y + y_offset, color=color, rotation=rotation_offset, outline_color=border_color, outline_width=hex_border_size, sides=sides)
|
| 155 |
if rotation != 0:
|
| 156 |
#image.show()
|
| 157 |
# Rotate the final image
|
|
|
|
| 203 |
draw = ImageDraw.Draw(image, mode="RGBA")
|
| 204 |
hex_width = hex_size * 2
|
| 205 |
hex_height = hex_size * 2
|
| 206 |
+
hex_horizontal_spacing = (hex_width + (hex_border_size if hex_border_size < 0 else 0) + x_spacing) * ((6 / sides) if sides > 3 else 1.3333) #* 0.8660254
|
| 207 |
+
hex_vertical_spacing = (hex_height + (hex_border_size if hex_border_size < 0 else 0) + y_spacing) * ((6 / sides) if sides > 3 else 3.0)
|
| 208 |
col = 0
|
| 209 |
row = 0
|
| 210 |
## Function to draw optional text
|
|
|
|
| 238 |
pass
|
| 239 |
hex_index = -1 # Initialize hex index
|
| 240 |
def draw_hexagon(x, y, color="#FFFFFFFF", rotation=0, outline_color="#12165380", outline_width=0, sides=6):
|
| 241 |
+
#side_length = (hex_size * 2) / math.sqrt(3) #hexagons only
|
| 242 |
+
side_length = 2 * hex_size * math.tan(math.pi / sides) #hexagons, squares, triangle can tile
|
| 243 |
points = [(x + side_length * math.cos(math.radians(angle + rotation)), y + side_length * math.sin(math.radians(angle + rotation))) for angle in range(0, 360, (360 // sides))]
|
| 244 |
draw.polygon(points, fill=color, outline=outline_color, width=max(-5, outline_width))
|
| 245 |
# Function to range a floating number
|
|
|
|
| 255 |
for x in frange(start_x, max(image_width + additional_width, image_width, rotated_image_width) + (end_x - start_x), hex_horizontal_spacing):
|
| 256 |
col += 1
|
| 257 |
hex_index += 1 # Increment hex index
|
| 258 |
+
|
| 259 |
+
# Calculate offsets based on the number of sides
|
| 260 |
+
if sides == 4:
|
| 261 |
+
# Squares line up perfectly; no vertical offset is needed.
|
| 262 |
+
x_offset = hex_size
|
| 263 |
+
y_offset = 0
|
| 264 |
+
rotation_offset = -45
|
| 265 |
+
elif sides == 3:
|
| 266 |
+
# For equilateral triangles, you might offset rows by about one-third
|
| 267 |
+
# of the triangle�s height. Adjust as needed.
|
| 268 |
+
x_offset = -hex_border_size * 2 #hex_width * math.tan(math.pi / sides) - hex_border_size * 2
|
| 269 |
+
y_offset = -hex_border_size * 2
|
| 270 |
+
rotation_offset = -60
|
| 271 |
+
# Adjust y_offset for columns 1 and 2 to overlap
|
| 272 |
+
if col % 2 == 1:
|
| 273 |
+
x_offset += int(hex_size * 0.8660254)
|
| 274 |
+
y_offset -= int(hex_height * 1.5)
|
| 275 |
+
rotation_offset = 0
|
| 276 |
+
else:
|
| 277 |
+
# Default behavior (6 sides)
|
| 278 |
+
x_offset = hex_width // 2
|
| 279 |
+
y_offset = (hex_height // 2) #* 1.15470054342517
|
| 280 |
+
rotation_offset = 0
|
| 281 |
+
# Adjust y_offset for columns 1 and 3 to overlap
|
| 282 |
+
if col % 2 == 1:
|
| 283 |
+
y_offset -= (hex_height // 2) #* 0.8660254
|
| 284 |
+
|
| 285 |
if rotated_input_image:
|
| 286 |
# Sample the colors of the pixels in the hexagon, if fill_hex is True
|
| 287 |
if fill_hex:
|
|
|
|
| 310 |
else:
|
| 311 |
print(f"color found: {avg_color}")
|
| 312 |
#draw_hexagon(x + x_offset, y + y_offset, color="#{:02x}{:02x}{:02x}{:02x}".format(*avg_color if fill_hex else (0,0,0,0)), outline_color=border_color, outline_width=hex_border_size, sides=sides)
|
| 313 |
+
draw_hexagon(x + x_offset, y + y_offset, color="#{:02x}{:02x}{:02x}{:02x}".format(*avg_color), rotation=rotation_offset, outline_color=border_color, outline_width=hex_border_size, sides=sides)
|
| 314 |
else:
|
| 315 |
+
draw_hexagon(x + x_offset, y + y_offset, color="#00000000", rotation=rotation_offset, outline_color=border_color, outline_width=hex_border_size, sides=sides)
|
| 316 |
else:
|
| 317 |
color = "#%02x%02x%02x%02x" % (128, math.ceil(y) % 255, math.ceil(x) % 255, 255) if fill_hex else (0,0,0,0)
|
| 318 |
+
draw_hexagon(x + x_offset, y + y_offset, color=color, rotation=rotation_offset, outline_color=border_color, outline_width=hex_border_size, sides=sides)
|
| 319 |
# Draw text in hexagon
|
| 320 |
if add_hex_text_option != None:
|
| 321 |
font_size = calculate_font_size(hex_size, 0.333, 20, 7)
|
|
|
|
| 369 |
# Calculate position to center text in hexagon
|
| 370 |
# text_x = x + x_offset - (text_width / 2)
|
| 371 |
# text_y = y + y_offset - (text_height / 2)
|
| 372 |
+
# Calculate position to top left text in hexagon
|
| 373 |
text_x = x + x_offset - (hex_size / 1.75)
|
| 374 |
+
text_y = y + y_offset - (hex_size / 1.75)
|
| 375 |
# Draw the text directly onto the image
|
| 376 |
+
font_image = draw_rotated_text_with_emojis(
|
| 377 |
image=font_image,
|
| 378 |
text=text,
|
| 379 |
font_color=update_color_opacity(text_color,255),
|
| 380 |
offset_x=text_x,
|
| 381 |
offset_y=text_y,
|
| 382 |
font_name=font_name,
|
| 383 |
+
font_size=font_size,
|
| 384 |
+
angle = -1.0 * rotation
|
| 385 |
)
|
| 386 |
# # Use Pilmoji to draw text with emojis
|
| 387 |
# with Pilmoji(image) as pilmoji:
|