phenixrhyder commited on
Commit
d8be0c0
·
unverified ·
1 Parent(s): 8a14e66

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -15
app.py CHANGED
@@ -1,21 +1,106 @@
1
  import gradio as gr
2
- import os
 
3
 
4
- # Get the absolute path to the directory where this script is located
5
- script_dir = os.path.dirname(os.path.abspath(__file__))
6
- # Join that directory path with the filename to get the full path to index.html
7
- html_file_path = os.path.join(script_dir, 'index.html')
 
 
 
8
 
9
- # Read the entire content of the index.html file using the full path
10
- try:
11
- with open(html_file_path, 'r', encoding='utf-8') as f:
12
- html_content = f.read()
13
- except FileNotFoundError:
14
- html_content = "<h1>Error: index.html not found.</h1>"
15
 
16
- # Create and launch the Gradio interface
17
- with gr.Blocks(title="Frame Studio") as demo:
18
- gr.HTML(html_content)
19
 
20
- demo.launch()
 
 
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from PIL import Image, ImageDraw
3
+ import tempfile
4
 
5
+ # Define a dictionary of preset canvas sizes (width, height)
6
+ PRESET_SIZES = {
7
+ "Square (1080x1080)": (1080, 1080),
8
+ "Widescreen (1920x1080)": (1920, 1080),
9
+ "Portrait (1080x1920)": (1080, 1920),
10
+ "Classic TV (1200x900)": (1200, 900),
11
+ }
12
 
13
+ def create_checkerboard_frame(preset_name, frame_thickness, square_size, color1, color1_transparent, color2, color2_transparent):
14
+ """
15
+ Generates a checkerboard frame on a transparent canvas.
16
+ """
17
+ # Get the image dimensions from the selected preset
18
+ image_width, image_height = PRESET_SIZES[preset_name]
19
 
20
+ # Calculate how many squares will fit in the canvas
21
+ cols = int(image_width / square_size) + 1
22
+ rows = int(image_height / square_size) + 1
23
 
24
+ # Create a new blank image in RGBA mode to support transparency
25
+ image = Image.new("RGBA", (image_width, image_height), (0, 0, 0, 0))
26
+ draw = ImageDraw.Draw(image)
27
 
28
+ # Determine the actual colors to use
29
+ final_color1 = (0, 0, 0, 0) if color1_transparent else color1
30
+ final_color2 = (0, 0, 0, 0) if color2_transparent else color2
31
+
32
+ # Loop through each square position
33
+ for r in range(rows):
34
+ for c in range(cols):
35
+ # *** CORE LOGIC: Check if the current square is part of the frame ***
36
+ is_frame = r < frame_thickness or r >= rows - frame_thickness or \
37
+ c < frame_thickness or c >= cols - frame_thickness
38
+
39
+ if is_frame:
40
+ # Determine which color to use for the current square
41
+ square_color = final_color1 if (r + c) % 2 == 0 else final_color2
42
+
43
+ # Calculate the coordinates of the square
44
+ x1 = c * square_size
45
+ y1 = r * square_size
46
+ x2 = x1 + square_size
47
+ y2 = y1 + square_size
48
+
49
+ # Draw the rectangle, but only if the color isn't fully transparent
50
+ if square_color[3] != 0 if isinstance(square_color, tuple) else True:
51
+ draw.rectangle([x1, y1, x2, y2], fill=square_color)
52
+
53
+ # Save the image to a temporary file to make it downloadable
54
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
55
+ image.save(temp_file.name)
56
+ download_path = temp_file.name
57
+
58
+ return image, download_path
59
+
60
+ # --- Create the Gradio Interface ---
61
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="pink", secondary_hue="orange")) as demo:
62
+ gr.Markdown("# Checkerboard Frame Generator")
63
+ gr.Markdown("Create a custom checkerboard frame. The center will remain transparent. The image updates automatically as you change the settings.")
64
+
65
+ with gr.Row():
66
+ with gr.Column(scale=2):
67
+ # Input controls
68
+ preset_dropdown = gr.Dropdown(choices=list(PRESET_SIZES.keys()), value="Square (1080x1080)", label="Canvas Size Preset")
69
+ frame_thickness_slider = gr.Slider(minimum=1, maximum=20, value=3, step=1, label="Frame Thickness (in squares)")
70
+ square_size_slider = gr.Slider(minimum=10, maximum=200, value=50, step=1, label="Square Size (pixels)")
71
+
72
+ with gr.Row():
73
+ color_picker_1 = gr.ColorPicker(value="#6b7280", label="Color 1")
74
+ transparent_cb_1 = gr.Checkbox(label="Transparent", value=False)
75
+ with gr.Row():
76
+ color_picker_2 = gr.ColorPicker(value="#d1d5db", label="Color 2")
77
+ transparent_cb_2 = gr.Checkbox(label="Transparent", value=False)
78
+
79
+ download_button = gr.File(label="Download Image as PNG", interactive=False)
80
+
81
+ with gr.Column(scale=3):
82
+ # Output display
83
+ output_image = gr.Image(label="Generated Frame", interactive=False, height=500)
84
+
85
+ # List of all input components
86
+ inputs = [
87
+ preset_dropdown,
88
+ frame_thickness_slider,
89
+ square_size_slider,
90
+ color_picker_1,
91
+ transparent_cb_1,
92
+ color_picker_2,
93
+ transparent_cb_2
94
+ ]
95
+
96
+ # Link the controls to the function. The 'live=True' makes it update automatically.
97
+ for component in inputs:
98
+ component.change(
99
+ fn=create_checkerboard_frame,
100
+ inputs=inputs,
101
+ outputs=[output_image, download_button],
102
+ # api_name="generate" # This line is not needed for the UI to work
103
+ )
104
+
105
+ if __name__ == "__main__":
106
+ demo.launch()