| |
|
|
| import gradio as gr |
| from PIL import Image, ImageDraw |
|
|
| def create_checkerboard(board_size=8, square_size=50): |
| """ |
| Generates a checkerboard image using the Pillow library. |
| |
| Args: |
| board_size (int): The number of squares per side (e.g., 8 for an 8x8 board). |
| square_size (int): The size of each square in pixels. |
| |
| Returns: |
| PIL.Image.Image: The generated checkerboard image. |
| """ |
| |
| image_size = board_size * square_size |
| |
| |
| image = Image.new("RGB", (image_size, image_size), "white") |
| draw = ImageDraw.Draw(image) |
|
|
| |
| color1 = (255, 255, 255) |
| color2 = (0, 0, 0) |
|
|
| |
| for row in range(board_size): |
| for col in range(board_size): |
| |
| x1 = col * square_size |
| y1 = row * square_size |
| x2 = x1 + square_size |
| y2 = y1 + square_size |
|
|
| |
| if (row + col) % 2 == 0: |
| square_color = color1 |
| else: |
| square_color = color2 |
| |
| |
| draw.rectangle([x1, y1, x2, y2], fill=square_color) |
|
|
| return image |
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown("# Checkerboard Pattern Generator") |
| gr.Markdown("Use the sliders to change the size of the board and the squares, then click Generate.") |
|
|
| with gr.Row(): |
| |
| board_size_slider = gr.Slider(minimum=2, maximum=20, value=8, step=1, label="Board Size (e.g., 8x8)") |
| square_size_slider = gr.Slider(minimum=10, maximum=100, value=50, step=5, label="Square Size (pixels)") |
|
|
| |
| generate_button = gr.Button("Generate Image") |
|
|
| |
| output_image = gr.Image(label="Generated Checkerboard") |
|
|
| |
| generate_button.click( |
| fn=create_checkerboard, |
| inputs=[board_size_slider, square_size_slider], |
| outputs=output_image |
| ) |
|
|
| |
| if __name__ == "__main__": |
| demo.launch() |
|
|