phenixrhyder commited on
Commit
def0e97
·
unverified ·
1 Parent(s): 8af9ebf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -16
app.py CHANGED
@@ -2,33 +2,33 @@
2
 
3
  import gradio as gr
4
  from PIL import Image, ImageDraw
 
5
 
6
- def create_checkerboard(board_size=8, square_size=50):
7
  """
8
- Generates a checkerboard image using the Pillow library.
9
 
10
  Args:
11
- board_size (int): The number of squares per side (e.g., 8 for an 8x8 board).
12
  square_size (int): The size of each square in pixels.
 
 
13
 
14
  Returns:
15
- PIL.Image.Image: The generated checkerboard image.
 
16
  """
17
  # Calculate the total size of the image
18
  image_size = board_size * square_size
19
 
20
- # Create a new blank image in RGB mode with a white background
21
  image = Image.new("RGB", (image_size, image_size), "white")
22
  draw = ImageDraw.Draw(image)
23
 
24
- # Define the colors for the checkerboard
25
- color1 = (255, 255, 255) # White
26
- color2 = (0, 0, 0) # Black
27
-
28
  # Loop through each square position
29
  for row in range(board_size):
30
  for col in range(board_size):
31
- # Calculate the top-left and bottom-right coordinates of the square
32
  x1 = col * square_size
33
  y1 = row * square_size
34
  x2 = x1 + square_size
@@ -40,32 +40,45 @@ def create_checkerboard(board_size=8, square_size=50):
40
  else:
41
  square_color = color2
42
 
43
- # Draw the rectangle on the image
44
  draw.rectangle([x1, y1, x2, y2], fill=square_color)
45
 
46
- return image
 
 
 
 
 
47
 
48
  # --- Create the Gradio Interface ---
49
- with gr.Blocks() as demo:
50
  gr.Markdown("# Checkerboard Pattern Generator")
51
- gr.Markdown("Use the sliders to change the size of the board and the squares, then click Generate.")
52
 
53
  with gr.Row():
54
  # Input sliders for customization
55
  board_size_slider = gr.Slider(minimum=2, maximum=20, value=8, step=1, label="Board Size (e.g., 8x8)")
56
  square_size_slider = gr.Slider(minimum=10, maximum=100, value=50, step=5, label="Square Size (pixels)")
57
 
 
 
 
 
 
58
  # The button to trigger the image generation
59
  generate_button = gr.Button("Generate Image")
60
 
61
  # The output component to display the generated image
62
  output_image = gr.Image(label="Generated Checkerboard")
 
 
 
63
 
64
  # Link the button to the function
65
  generate_button.click(
66
  fn=create_checkerboard,
67
- inputs=[board_size_slider, square_size_slider],
68
- outputs=output_image
69
  )
70
 
71
  # --- Launch the App ---
 
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 hex code for the first color.
15
+ color2 (str): The hex code for the second color.
16
 
17
  Returns:
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
25
  image = Image.new("RGB", (image_size, image_size), "white")
26
  draw = ImageDraw.Draw(image)
27
 
 
 
 
 
28
  # Loop through each square position
29
  for row in range(board_size):
30
  for col in range(board_size):
31
+ # Calculate the coordinates of the square
32
  x1 = col * square_size
33
  y1 = row * square_size
34
  x2 = x1 + square_size
 
40
  else:
41
  square_color = color2
42
 
43
+ # Draw the rectangle
44
  draw.rectangle([x1, y1, x2, y2], fill=square_color)
45
 
46
+ # Save the image to a temporary file to make it downloadable
47
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
48
+ image.save(temp_file.name)
49
+ download_path = temp_file.name
50
+
51
+ return image, download_path
52
 
53
  # --- Create the Gradio Interface ---
54
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
55
  gr.Markdown("# Checkerboard Pattern Generator")
56
+ gr.Markdown("Use the sliders and color pickers to customize your pattern, then click Generate.")
57
 
58
  with gr.Row():
59
  # Input sliders for customization
60
  board_size_slider = gr.Slider(minimum=2, maximum=20, value=8, step=1, label="Board Size (e.g., 8x8)")
61
  square_size_slider = gr.Slider(minimum=10, maximum=100, value=50, step=5, label="Square Size (pixels)")
62
 
63
+ with gr.Row():
64
+ # NEW: Color pickers for the two board colors
65
+ color_picker_1 = gr.ColorPicker(value="#FFFFFF", label="Color 1 (Light)")
66
+ color_picker_2 = gr.ColorPicker(value="#000000", label="Color 2 (Dark)")
67
+
68
  # The button to trigger the image generation
69
  generate_button = gr.Button("Generate Image")
70
 
71
  # The output component to display the generated image
72
  output_image = gr.Image(label="Generated Checkerboard")
73
+
74
+ # NEW: The file component for the download button
75
+ download_button = gr.File(label="Download Image as PNG")
76
 
77
  # Link the button to the function
78
  generate_button.click(
79
  fn=create_checkerboard,
80
+ inputs=[board_size_slider, square_size_slider, color_picker_1, color_picker_2],
81
+ outputs=[output_image, download_button] # We now have two outputs
82
  )
83
 
84
  # --- Launch the App ---