Create an Image Tiling App with Python & Gradio

πŸ“Έ

Want to generate a tiled pattern from an image? Here’s a simple Python app using Pillow and Gradio to let users resize an image and tile it as a grid. πŸš€

πŸ›  What You’ll Need

  • Pillow: For image processing
  • Gradio: To create a simple web UI

πŸ“Œ Step 1: Install Dependencies

Make sure you have the required libraries installed:

pip install pillow gradio

πŸ“Œ Step 2: Create the Python Script

Here’s the full code to create the tiling app:

from PIL import Image
import gradio as gr

def tile_image(img, resize_value, repeat_x, repeat_y):
    # Resize input image
    img = img.resize((resize_value, resize_value))
    
    # Calculate new image dimensions
    new_width = img.width * repeat_x
    new_height = img.height * repeat_y
    
    # Create blank canvas
    new_img = Image.new('RGB', (new_width, new_height))
    
    # Tile the image
    for i in range(repeat_x):
        for j in range(repeat_y):
            new_img.paste(img, (i * img.width, j * img.height))
    
    return new_img

# Gradio UI
iface = gr.Interface(
    fn=tile_image,
    inputs=[
        gr.Image(type="pil", label="Upload Image"),
        gr.Slider(50, 500, step=10, label="Resize Image"),
        gr.Slider(1, 10, step=1, label="Horizontal Repeats"),
        gr.Slider(1, 10, step=1, label="Vertical Repeats")
    ],
    outputs=gr.Image(type="pil"),
    title="Image Tiling Generator",
    description="Upload an image, resize it, and set horizontal & vertical repeats to generate a tiled pattern.",
    live=True
)

if __name__ == "__main__":
    iface.launch()

πŸ“Œ Step 3: Run the App

Run the script and Gradio will launch a web interface:

python image_tiling.py

You can now upload an image, choose the resize value, and set tiling parameters to generate a patterned output.

✨ Enjoy creating beautiful image mosaics! ✨

Let me know if you have any questions! πŸ˜ŠπŸš€

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *