phenixrhyder
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,8 +11,8 @@ def create_checkerboard(board_size, square_size, color1, color2):
|
|
| 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
|
| 15 |
-
color2 (str): The name
|
| 16 |
|
| 17 |
Returns:
|
| 18 |
(PIL.Image.Image, str): A tuple containing the generated image
|
|
@@ -21,24 +21,31 @@ def create_checkerboard(board_size, square_size, color1, color2):
|
|
| 21 |
# Calculate the total size of the image
|
| 22 |
image_size = board_size * square_size
|
| 23 |
|
| 24 |
-
# Create a new blank image
|
| 25 |
-
|
|
|
|
| 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
|
| 35 |
y2 = y1 + square_size
|
| 36 |
-
|
| 37 |
-
# Determine the color of the square
|
| 38 |
-
if (row + col) % 2 == 0:
|
| 39 |
-
square_color = color1
|
| 40 |
-
else:
|
| 41 |
-
square_color = color2
|
| 42 |
|
| 43 |
# Draw the rectangle
|
| 44 |
draw.rectangle([x1, y1, x2, y2], fill=square_color)
|
|
@@ -60,13 +67,13 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 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 |
-
# Define a list of standard colors
|
| 64 |
-
color_choices = ["White", "Black", "Gray", "Red", "Green", "Blue", "Yellow", "Purple", "Orange", "Cyan", "Magenta"]
|
| 65 |
|
| 66 |
with gr.Row():
|
| 67 |
-
#
|
| 68 |
-
dropdown_1 = gr.Dropdown(choices=color_choices, value="
|
| 69 |
-
dropdown_2 = gr.Dropdown(choices=color_choices, value="Black", label="Color 2
|
| 70 |
|
| 71 |
# The button to trigger the image generation
|
| 72 |
generate_button = gr.Button("Generate Image")
|
|
@@ -80,7 +87,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 80 |
# Link the button to the function
|
| 81 |
generate_button.click(
|
| 82 |
fn=create_checkerboard,
|
| 83 |
-
inputs=[board_size_slider, square_size_slider, dropdown_1, dropdown_2],
|
| 84 |
outputs=[output_image, download_button]
|
| 85 |
)
|
| 86 |
|
|
|
|
| 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 |
|
| 17 |
Returns:
|
| 18 |
(PIL.Image.Image, str): A tuple containing the generated image
|
|
|
|
| 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
|
| 46 |
y1 = row * square_size
|
| 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)
|
|
|
|
| 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")
|
|
|
|
| 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 |
|