| |
|
|
| import gradio as gr |
| from PIL import Image, ImageDraw |
| import tempfile |
|
|
| |
| PRESET_SIZES = { |
| "Square (1:1)": (1080, 1080), |
| "Classic TV (4:3)": (1200, 900), |
| "Widescreen (16:9)": (1920, 1080), |
| "Tumblr Header": (3000, 1055), |
| } |
|
|
| def create_checkerboard(preset_name, square_size, color1, color2): |
| """ |
| Generates a checkerboard image by filling a preset canvas with perfect squares. |
| |
| Args: |
| preset_name (str): The key for the PRESET_SIZES dictionary. |
| square_size (int): The width and height of each perfect square in pixels. |
| color1 (str): The name of the first color. |
| color2 (str): The name of the second color. |
| |
| Returns: |
| (PIL.Image.Image, str): A tuple containing the generated image |
| and the path to a temporary file for download. |
| """ |
| |
| image_width, image_height = PRESET_SIZES[preset_name] |
|
|
| |
| board_size_w = int(image_width / square_size) + 1 |
| board_size_h = int(image_height / square_size) + 1 |
|
|
| |
| image = Image.new("RGBA", (image_width, image_height), (0, 0, 0, 0)) |
| draw = ImageDraw.Draw(image) |
|
|
| |
| for row in range(board_size_h): |
| for col in range(board_size_w): |
| |
| if (row + col) % 2 == 0: |
| color_name = color1 |
| else: |
| color_name = color2 |
| |
| |
| square_color = (0, 0, 0, 0) if color_name == "Transparent" else color_name |
|
|
| |
| x1 = col * square_size |
| y1 = row * square_size |
| x2 = x1 + square_size |
| y2 = y1 + square_size |
| |
| |
| draw.rectangle([x1, y1, x2, y2], fill=square_color) |
|
|
| |
| with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file: |
| image.save(temp_file.name) |
| download_path = temp_file.name |
|
|
| return image, download_path |
|
|
| |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: |
| gr.Markdown("# Checkerboard Pattern Generator") |
| gr.Markdown("Select a preset canvas size, the size of the squares, and colors, then click Generate.") |
|
|
| with gr.Row(): |
| |
| preset_dropdown = gr.Dropdown( |
| choices=list(PRESET_SIZES.keys()), |
| value="Square (1:1)", |
| label="Canvas Size Preset" |
| ) |
| |
| square_size_slider = gr.Slider( |
| minimum=10, |
| maximum=200, |
| value=50, |
| step=1, |
| label="Square Size (pixels)" |
| ) |
|
|
| |
| color_choices = ["Transparent", "White", "Black", "Gray", "Red", "Green", "Blue", "Yellow", "Purple", "Orange"] |
|
|
| with gr.Row(): |
| |
| dropdown_1 = gr.Dropdown(choices=color_choices, value="Transparent", label="Color 1") |
| dropdown_2 = gr.Dropdown(choices=color_choices, value="Black", label="Color 2") |
|
|
| generate_button = gr.Button("Generate Image") |
| output_image = gr.Image(label="Generated Checkerboard") |
| download_button = gr.File(label="Download Image as PNG") |
|
|
| |
| generate_button.click( |
| fn=create_checkerboard, |
| inputs=[preset_dropdown, square_size_slider, dropdown_1, dropdown_2], |
| outputs=[output_image, download_button] |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch() |
|
|