import uuid import os from PIL import Image import gradio as gr COMMON_RESOLUTIONS = { "16:9": (1920, 1080), "9:16": (1080, 1920), "4:3": (1440, 1080), "3:4": (1080, 1440), "1:1": (1080, 1080), "21:9": (2560, 1080), "2:1": (2160, 1080), "3:2": (1620, 1080), "5:4": (1350, 1080), "4:5": (864, 1080), } os.makedirs("./download", exist_ok=True) def prompt_maker(aspect_ratio): prompt=f"""Take the subject from the first image and place it inside the second image canvas. Expand the background of the first image naturally to fill the extra space of the second image. Keep the subject exactly the same, without cropping or distortion. Extend the background seamlessly in a photorealistic way, matching the original style, details, colors, lighting, and textures. Ensure smooth blending with no visible edges, borders, or repetition. Final result should be in perfect {aspect_ratio}.""" return prompt def generate_aspect_image(aspect_ratio): """Generate image from preset aspect ratio""" width, height = COMMON_RESOLUTIONS[aspect_ratio] img = Image.new("RGBA", (width, height), (0, 0, 0, 0)) # random_str=str(uuid.uuid4())[:5] width,height=aspect_ratio.split(":") random_str=f"{width}_{height}" filename = f"./download/{random_str}.png" img.save(filename, "PNG") aspect_ratio=f"[{aspect_ratio}] ratio" prompt=prompt_maker(aspect_ratio) return prompt,filename def generate_custom_image(width, height): """Generate image from custom width/height""" try: width, height = int(width), int(height) except ValueError: return None if width <= 0 or height <= 0: return None img = Image.new("RGBA", (width, height), (0, 0, 0, 0)) # random_str=str(uuid.uuid4())[:5] random_str=f"{width}_{height}" filename = f"./download/{random_str}.png" img.save(filename, "PNG") aspect_ratio=f"[{width}x{height}] pixels" prompt=prompt_maker(aspect_ratio) return prompt,filename with gr.Blocks() as demo: gr.HTML("""