phenixrhyder
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,79 @@
|
|
1 |
-
|
2 |
|
3 |
-
|
4 |
-
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
|
3 |
+
import gradio as gr
|
4 |
+
from PIL import Image, ImageDraw
|
5 |
+
import tempfile
|
6 |
|
7 |
+
def create_checkerboard_frame(width, height, frame_thickness, square_size, color1, color2):
|
8 |
+
"""
|
9 |
+
Generates a checkerboard frame with a transparent center.
|
10 |
+
"""
|
11 |
+
# Create a new blank image in RGBA mode to support transparency
|
12 |
+
image = Image.new("RGBA", (width, height), (0, 0, 0, 0))
|
13 |
+
draw = ImageDraw.Draw(image)
|
14 |
|
15 |
+
# Calculate how many squares fit on the canvas
|
16 |
+
board_size_w = int(width / square_size) + 1
|
17 |
+
board_size_h = int(height / square_size) + 1
|
18 |
+
|
19 |
+
# Loop through each square position
|
20 |
+
for row in range(board_size_h):
|
21 |
+
for col in range(board_size_w):
|
22 |
+
# Check if the current square is part of the frame
|
23 |
+
is_in_frame = (
|
24 |
+
row < frame_thickness or
|
25 |
+
col < frame_thickness or
|
26 |
+
row >= (board_size_h - frame_thickness) or
|
27 |
+
col >= (board_size_w - frame_thickness)
|
28 |
+
)
|
29 |
+
|
30 |
+
if is_in_frame:
|
31 |
+
# Determine which color to use
|
32 |
+
color_name = color1 if (row + col) % 2 == 0 else color2
|
33 |
+
|
34 |
+
# Use a transparent tuple if "Transparent" is selected
|
35 |
+
square_color = (0, 0, 0, 0) if color_name == "Transparent" else color_name
|
36 |
+
|
37 |
+
# Calculate coordinates and draw the square
|
38 |
+
x1, y1 = col * square_size, row * square_size
|
39 |
+
x2, y2 = x1 + square_size, y1 + square_size
|
40 |
+
draw.rectangle([x1, y1, x2, y2], fill=square_color)
|
41 |
+
|
42 |
+
# Save the image to a temporary file for download
|
43 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
|
44 |
+
image.save(temp_file.name)
|
45 |
+
download_path = temp_file.name
|
46 |
+
|
47 |
+
return image, download_path
|
48 |
+
|
49 |
+
# --- Create the Gradio Interface ---
|
50 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
51 |
+
gr.Markdown("# Checkerboard Frame Generator")
|
52 |
+
gr.Markdown("Create a checkerboard frame with a transparent center. Adjust the canvas, frame, and square sizes.")
|
53 |
+
|
54 |
+
with gr.Row():
|
55 |
+
width_slider = gr.Slider(minimum=200, maximum=4000, value=1080, step=10, label="Canvas Width (pixels)")
|
56 |
+
height_slider = gr.Slider(minimum=200, maximum=4000, value=1080, step=10, label="Canvas Height (pixels)")
|
57 |
+
|
58 |
+
with gr.Row():
|
59 |
+
frame_thickness_slider = gr.Slider(minimum=1, maximum=20, value=2, step=1, label="Frame Thickness (in squares)")
|
60 |
+
square_size_slider = gr.Slider(minimum=10, maximum=200, value=50, step=1, label="Square Size (pixels)")
|
61 |
+
|
62 |
+
color_choices = ["Transparent", "White", "Black", "Gray", "Red", "Green", "Blue", "Yellow", "Purple", "Orange"]
|
63 |
+
|
64 |
+
with gr.Row():
|
65 |
+
dropdown_1 = gr.Dropdown(choices=color_choices, value="White", label="Color 1")
|
66 |
+
dropdown_2 = gr.Dropdown(choices=color_choices, value="Black", label="Color 2")
|
67 |
+
|
68 |
+
generate_button = gr.Button("Generate Frame")
|
69 |
+
output_image = gr.Image(label="Generated Frame")
|
70 |
+
download_button = gr.File(label="Download Image as PNG")
|
71 |
+
|
72 |
+
generate_button.click(
|
73 |
+
fn=create_checkerboard_frame,
|
74 |
+
inputs=[width_slider, height_slider, frame_thickness_slider, square_size_slider, dropdown_1, dropdown_2],
|
75 |
+
outputs=[output_image, download_button]
|
76 |
+
)
|
77 |
+
|
78 |
+
if __name__ == "__main__":
|
79 |
+
demo.launch()
|