Spaces:
Running
on
Zero
Running
on
Zero
Enable variable number of sides on hexes
Browse filesfuture implementation of squares and hexes and other shapes
- app.py +2 -2
- utils/hex_grid.py +15 -13
app.py
CHANGED
@@ -141,7 +141,7 @@ def end_session(req: gr.Request):
|
|
141 |
# Register the cleanup function
|
142 |
atexit.register(end_session)
|
143 |
|
144 |
-
def hex_create(hex_size, border_size, input_image_path, start_x, start_y, end_x, end_y, rotation, background_color_hex, background_opacity, border_color_hex, border_opacity, fill_hex, excluded_colors_var, filter_color, x_spacing, y_spacing, add_hex_text_option=None, custom_text_list=None, custom_text_color_list=None):
|
145 |
global input_image_palette
|
146 |
|
147 |
try:
|
@@ -211,7 +211,7 @@ def hex_create(hex_size, border_size, input_image_path, start_x, start_y, end_x,
|
|
211 |
y_spacing,
|
212 |
add_hex_text_option,
|
213 |
custom_text_list,
|
214 |
-
custom_text_color_list
|
215 |
)
|
216 |
_,_, name, _, new_ext = get_file_parts(input_image_path)
|
217 |
grid_image_path = save_image_to_temp_png(grid_image, user_dir, "hexgrid_" + name)
|
|
|
141 |
# Register the cleanup function
|
142 |
atexit.register(end_session)
|
143 |
|
144 |
+
def hex_create(hex_size, border_size, input_image_path, start_x, start_y, end_x, end_y, rotation, background_color_hex, background_opacity, border_color_hex, border_opacity, fill_hex, excluded_colors_var, filter_color, x_spacing, y_spacing, add_hex_text_option=None, custom_text_list=None, custom_text_color_list=None, sides=6):
|
145 |
global input_image_palette
|
146 |
|
147 |
try:
|
|
|
211 |
y_spacing,
|
212 |
add_hex_text_option,
|
213 |
custom_text_list,
|
214 |
+
custom_text_color_list, sides
|
215 |
)
|
216 |
_,_, name, _, new_ext = get_file_parts(input_image_path)
|
217 |
grid_image_path = save_image_to_temp_png(grid_image, user_dir, "hexgrid_" + name)
|
utils/hex_grid.py
CHANGED
@@ -33,7 +33,7 @@ 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):
|
37 |
if input_image:
|
38 |
image_width, image_height = input_image.size
|
39 |
# Use half hex_size, thus do not double border size
|
@@ -72,7 +72,8 @@ def generate_hexagon_grid(hex_size, border_size, input_image=None, image_width=0
|
|
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 |
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))]
|
77 |
draw.polygon(points, fill=color, outline=outline_color, width=max(-5, outline_width))
|
78 |
# Function to range a floating number
|
@@ -120,12 +121,12 @@ def generate_hexagon_grid(hex_size, border_size, input_image=None, image_width=0
|
|
120 |
else:
|
121 |
print(f"color found: {avg_color}")
|
122 |
#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)
|
123 |
-
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)
|
124 |
else:
|
125 |
-
draw_hexagon(x + x_offset, y + y_offset, color="#000000", outline_color=border_color, outline_width=hex_border_size)
|
126 |
else:
|
127 |
color = "#%02x%02x%02x%02x" % (128, math.ceil(y) % 255, math.ceil(x) % 255, 255) if fill_hex else (0,0,0,0)
|
128 |
-
draw_hexagon(x + x_offset, y + y_offset, color=color, outline_color=border_color, outline_width=hex_border_size)
|
129 |
if rotation != 0:
|
130 |
#image.show()
|
131 |
# Rotate the final image
|
@@ -140,7 +141,7 @@ def generate_hexagon_grid(hex_size, border_size, input_image=None, image_width=0
|
|
140 |
final_image = image
|
141 |
return final_image
|
142 |
|
143 |
-
def generate_hexagon_grid_with_text(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,
|
144 |
add_hex_text_option=None, custom_text_list=None, custom_text_color_list=None):
|
145 |
|
146 |
if input_image:
|
@@ -212,7 +213,8 @@ def generate_hexagon_grid_with_text(hex_size, border_size, input_image=None, ima
|
|
212 |
pass
|
213 |
hex_index = -1 # Initialize hex index
|
214 |
def draw_hexagon(x, y, color="#FFFFFFFF", rotation=0, outline_color="#12165380", outline_width=0, sides=6):
|
215 |
-
side_length = (hex_size * 2) / math.sqrt(3)
|
|
|
216 |
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))]
|
217 |
draw.polygon(points, fill=color, outline=outline_color, width=max(-5, outline_width))
|
218 |
# Function to range a floating number
|
@@ -260,13 +262,13 @@ def generate_hexagon_grid_with_text(hex_size, border_size, input_image=None, ima
|
|
260 |
avg_color = (0,0,0,0)
|
261 |
else:
|
262 |
print(f"color found: {avg_color}")
|
263 |
-
#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)
|
264 |
-
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)
|
265 |
else:
|
266 |
-
draw_hexagon(x + x_offset, y + y_offset, color="#00000000", outline_color=border_color, outline_width=hex_border_size)
|
267 |
else:
|
268 |
color = "#%02x%02x%02x%02x" % (128, math.ceil(y) % 255, math.ceil(x) % 255, 255) if fill_hex else (0,0,0,0)
|
269 |
-
draw_hexagon(x + x_offset, y + y_offset, color=color, outline_color=border_color, outline_width=hex_border_size)
|
270 |
# Draw text in hexagon
|
271 |
if add_hex_text_option != None:
|
272 |
font_size = calculate_font_size(hex_size, 0.333, 20, 7)
|
@@ -393,7 +395,7 @@ def generate_hexagon_grid_with_text(hex_size, border_size, input_image=None, ima
|
|
393 |
final_image = image
|
394 |
return final_image
|
395 |
|
396 |
-
def generate_hexagon_grid_interface(hex_size, border_size, image, start_x, start_y, end_x, end_y, rotation, background_color, border_color, fill_hex, excluded_color_list, filter_color, x_spacing, y_spacing, add_hex_text_option=None, custom_text_list=None, custom_text_color_list=None):
|
397 |
print(f"Generating Hexagon Grid with Parameters: Hex Size: {hex_size}, Border Size: {border_size}, Start X: {start_x}, Start Y: {start_y}, End X: {end_x}, End Y: {end_y}, Rotation: {rotation}, Background Color: {background_color}, Border Color: {border_color}, Fill Hex: {fill_hex}, Excluded Color List: {excluded_color_list}, Filter Color: {filter_color}, X Spacing: {x_spacing}, Y Spacing: {y_spacing}, add Text Option {add_hex_text_option}\n")
|
398 |
hexagon_grid_image = generate_hexagon_grid_with_text(
|
399 |
hex_size=abs(hex_size),
|
@@ -413,7 +415,7 @@ def generate_hexagon_grid_interface(hex_size, border_size, image, start_x, start
|
|
413 |
y_spacing = y_spacing if abs(hex_size) > abs(y_spacing) else (hex_size if y_spacing >= 0 else -hex_size),
|
414 |
add_hex_text_option = add_hex_text_option,
|
415 |
custom_text_list = custom_text_list,
|
416 |
-
custom_text_color_list= custom_text_color_list
|
417 |
)
|
418 |
overlay_image = multiply_and_blend_images(image, hexagon_grid_image, 50)
|
419 |
return hexagon_grid_image, overlay_image
|
|
|
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
|
39 |
# Use half hex_size, thus do not double border size
|
|
|
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
|
|
|
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
|
|
|
141 |
final_image = image
|
142 |
return final_image
|
143 |
|
144 |
+
def generate_hexagon_grid_with_text(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,
|
145 |
add_hex_text_option=None, custom_text_list=None, custom_text_color_list=None):
|
146 |
|
147 |
if input_image:
|
|
|
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
|
|
|
262 |
avg_color = (0,0,0,0)
|
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)
|
|
|
395 |
final_image = image
|
396 |
return final_image
|
397 |
|
398 |
+
def generate_hexagon_grid_interface(hex_size, border_size, image, start_x, start_y, end_x, end_y, rotation, background_color, border_color, fill_hex, excluded_color_list, filter_color, x_spacing, y_spacing, add_hex_text_option=None, custom_text_list=None, custom_text_color_list=None, sides=6):
|
399 |
print(f"Generating Hexagon Grid with Parameters: Hex Size: {hex_size}, Border Size: {border_size}, Start X: {start_x}, Start Y: {start_y}, End X: {end_x}, End Y: {end_y}, Rotation: {rotation}, Background Color: {background_color}, Border Color: {border_color}, Fill Hex: {fill_hex}, Excluded Color List: {excluded_color_list}, Filter Color: {filter_color}, X Spacing: {x_spacing}, Y Spacing: {y_spacing}, add Text Option {add_hex_text_option}\n")
|
400 |
hexagon_grid_image = generate_hexagon_grid_with_text(
|
401 |
hex_size=abs(hex_size),
|
|
|
415 |
y_spacing = y_spacing if abs(hex_size) > abs(y_spacing) else (hex_size if y_spacing >= 0 else -hex_size),
|
416 |
add_hex_text_option = add_hex_text_option,
|
417 |
custom_text_list = custom_text_list,
|
418 |
+
custom_text_color_list= custom_text_color_list, sides=sides
|
419 |
)
|
420 |
overlay_image = multiply_and_blend_images(image, hexagon_grid_image, 50)
|
421 |
return hexagon_grid_image, overlay_image
|