phenixrhyder commited on
Commit
28a81f5
·
unverified ·
1 Parent(s): c02a94a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -34
app.py CHANGED
@@ -2,15 +2,23 @@
2
 
3
  import gradio as gr
4
  from PIL import Image, ImageDraw
5
- import tempfile # Needed to create a temporary file for downloading
6
 
7
- def create_checkerboard(board_size, square_size, color1, color2):
 
 
 
 
 
 
 
 
8
  """
9
- Generates a checkerboard image using the Pillow library with custom colors.
10
 
11
  Args:
12
- board_size (int): The number of squares per side.
13
- square_size (int): The size of each square in pixels.
14
  color1 (str): The name of the first color.
15
  color2 (str): The name of the second color.
16
 
@@ -18,28 +26,28 @@ def create_checkerboard(board_size, square_size, color1, color2):
18
  (PIL.Image.Image, str): A tuple containing the generated image
19
  and the path to a temporary file for download.
20
  """
21
- # Calculate the total size of the image
22
- image_size = board_size * square_size
23
-
 
 
 
 
24
  # Create a new blank image in RGBA mode to support transparency
25
- # The background is now transparent (0,0,0,0)
26
- image = Image.new("RGBA", (image_size, image_size), (0, 0, 0, 0))
27
  draw = ImageDraw.Draw(image)
28
 
29
  # Loop through each square position
30
- for row in range(board_size):
31
- for col in range(board_size):
32
  # Determine which color string to use for the current square
33
  if (row + col) % 2 == 0:
34
  color_name = color1
35
  else:
36
  color_name = color2
37
 
38
- # Use a transparent tuple if "Transparent" is selected, otherwise use the color name
39
- if color_name == "Transparent":
40
- square_color = (0, 0, 0, 0)
41
- else:
42
- square_color = color_name
43
 
44
  # Calculate the coordinates of the square
45
  x1 = col * square_size
@@ -47,7 +55,7 @@ def create_checkerboard(board_size, square_size, color1, color2):
47
  x2 = x1 + square_size
48
  y2 = y1 + square_size
49
 
50
- # Draw the rectangle
51
  draw.rectangle([x1, y1, x2, y2], fill=square_color)
52
 
53
  # Save the image to a temporary file to make it downloadable
@@ -60,37 +68,42 @@ def create_checkerboard(board_size, square_size, color1, color2):
60
  # --- Create the Gradio Interface ---
61
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
62
  gr.Markdown("# Checkerboard Pattern Generator")
63
- gr.Markdown("Use the sliders and dropdowns to customize your pattern, then click Generate.")
64
 
65
  with gr.Row():
66
- # Input sliders for customization
67
- board_size_slider = gr.Slider(minimum=2, maximum=20, value=8, step=1, label="Board Size (e.g., 8x8)")
68
- square_size_slider = gr.Slider(minimum=10, maximum=100, value=50, step=5, label="Square Size (pixels)")
69
-
70
- # Define a list of standard colors, now including Transparent
71
- color_choices = ["Transparent", "White", "Black", "Gray", "Red", "Green", "Blue", "Yellow", "Purple", "Orange", "Cyan", "Magenta"]
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  with gr.Row():
74
- # Dropdowns now include and default to Transparent
75
  dropdown_1 = gr.Dropdown(choices=color_choices, value="Transparent", label="Color 1")
76
  dropdown_2 = gr.Dropdown(choices=color_choices, value="Black", label="Color 2")
77
 
78
- # The button to trigger the image generation
79
  generate_button = gr.Button("Generate Image")
80
-
81
- # The output component to display the generated image
82
  output_image = gr.Image(label="Generated Checkerboard")
83
-
84
- # The file component for the download button
85
  download_button = gr.File(label="Download Image as PNG")
86
 
87
- # Link the button to the function
88
  generate_button.click(
89
  fn=create_checkerboard,
90
- inputs=[board_size_slider, square_size_slider, dropdown_1, dropdown_2],
91
  outputs=[output_image, download_button]
92
  )
93
 
94
- # --- Launch the App ---
95
  if __name__ == "__main__":
96
  demo.launch()
 
2
 
3
  import gradio as gr
4
  from PIL import Image, ImageDraw
5
+ import tempfile
6
 
7
+ # Define a dictionary of preset canvas sizes (width, height)
8
+ PRESET_SIZES = {
9
+ "Square (1:1)": (1080, 1080),
10
+ "Classic TV (4:3)": (1200, 900),
11
+ "Widescreen (16:9)": (1920, 1080),
12
+ "Tumblr Header": (3000, 1055),
13
+ }
14
+
15
+ def create_checkerboard(preset_name, square_size, color1, color2):
16
  """
17
+ Generates a checkerboard image by filling a preset canvas with perfect squares.
18
 
19
  Args:
20
+ preset_name (str): The key for the PRESET_SIZES dictionary.
21
+ square_size (int): The width and height of each perfect square in pixels.
22
  color1 (str): The name of the first color.
23
  color2 (str): The name of the second color.
24
 
 
26
  (PIL.Image.Image, str): A tuple containing the generated image
27
  and the path to a temporary file for download.
28
  """
29
+ # Get the image dimensions from the selected preset
30
+ image_width, image_height = PRESET_SIZES[preset_name]
31
+
32
+ # Calculate how many squares will fit in the canvas
33
+ board_size_w = int(image_width / square_size) + 1
34
+ board_size_h = int(image_height / square_size) + 1
35
+
36
  # Create a new blank image in RGBA mode to support transparency
37
+ image = Image.new("RGBA", (image_width, image_height), (0, 0, 0, 0))
 
38
  draw = ImageDraw.Draw(image)
39
 
40
  # Loop through each square position
41
+ for row in range(board_size_h):
42
+ for col in range(board_size_w):
43
  # Determine which color string to use for the current square
44
  if (row + col) % 2 == 0:
45
  color_name = color1
46
  else:
47
  color_name = color2
48
 
49
+ # Use a transparent tuple if "Transparent" is selected
50
+ square_color = (0, 0, 0, 0) if color_name == "Transparent" else color_name
 
 
 
51
 
52
  # Calculate the coordinates of the square
53
  x1 = col * square_size
 
55
  x2 = x1 + square_size
56
  y2 = y1 + square_size
57
 
58
+ # Draw the rectangle (which is now always a square)
59
  draw.rectangle([x1, y1, x2, y2], fill=square_color)
60
 
61
  # Save the image to a temporary file to make it downloadable
 
68
  # --- Create the Gradio Interface ---
69
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
70
  gr.Markdown("# Checkerboard Pattern Generator")
71
+ gr.Markdown("Select a preset canvas size, the size of the squares, and colors, then click Generate.")
72
 
73
  with gr.Row():
74
+ # Dropdown for preset sizes
75
+ preset_dropdown = gr.Dropdown(
76
+ choices=list(PRESET_SIZES.keys()),
77
+ value="Square (1:1)",
78
+ label="Canvas Size Preset"
79
+ )
80
+ # CHANGED: Slider now controls the size of each square
81
+ square_size_slider = gr.Slider(
82
+ minimum=10,
83
+ maximum=200,
84
+ value=50,
85
+ step=1,
86
+ label="Square Size (pixels)"
87
+ )
88
+
89
+ # Define a list of standard colors, including Transparent
90
+ color_choices = ["Transparent", "White", "Black", "Gray", "Red", "Green", "Blue", "Yellow", "Purple", "Orange"]
91
 
92
  with gr.Row():
93
+ # Color dropdowns remain the same
94
  dropdown_1 = gr.Dropdown(choices=color_choices, value="Transparent", label="Color 1")
95
  dropdown_2 = gr.Dropdown(choices=color_choices, value="Black", label="Color 2")
96
 
 
97
  generate_button = gr.Button("Generate Image")
 
 
98
  output_image = gr.Image(label="Generated Checkerboard")
 
 
99
  download_button = gr.File(label="Download Image as PNG")
100
 
101
+ # Link the button to the function with the new inputs
102
  generate_button.click(
103
  fn=create_checkerboard,
104
+ inputs=[preset_dropdown, square_size_slider, dropdown_1, dropdown_2],
105
  outputs=[output_image, download_button]
106
  )
107
 
 
108
  if __name__ == "__main__":
109
  demo.launch()