import gradio as gr from PIL import Image, ImageEnhance, ImageFilter, ImageDraw, ImageFont import io def edit_image(base_image, overlay_image, crop_box, resize_dims, brightness, rotate_angle, flip_horizontal, flip_vertical, filter_type, text_overlay, overlay_position, overlay_opacity): # Convert the base image to a PIL Image base_image = base_image.convert("RGBA") # Convert the overlay image to a PIL Image, if provided if overlay_image: overlay_image = overlay_image.convert("RGBA") # Resize overlay image if needed if resize_dims: width, height = map(int, resize_dims.split(',')) overlay_image = overlay_image.resize((width, height)) # Apply opacity to the overlay image if overlay_opacity is not None: alpha = overlay_image.split()[3].point(lambda p: p * (overlay_opacity / 100.0)) overlay_image.putalpha(alpha) # Add overlay image to base image if overlay_position: overlay_position = tuple(map(int, overlay_position.split(','))) base_image.paste(overlay_image, overlay_position, overlay_image) # Crop the base image if crop_box: left, top, right, bottom = map(int, crop_box.split(',')) base_image = base_image.crop((left, top, right, bottom)) # Resize the base image if resize_dims: width, height = map(int, resize_dims.split(',')) base_image = base_image.resize((width, height)) # Adjust brightness if brightness is not None: enhancer = ImageEnhance.Brightness(base_image) base_image = enhancer.enhance(brightness) # Rotate the base image if rotate_angle: base_image = base_image.rotate(rotate_angle, expand=True) # Flip the base image if flip_horizontal: base_image = base_image.transpose(Image.FLIP_LEFT_RIGHT) if flip_vertical: base_image = base_image.transpose(Image.FLIP_TOP_BOTTOM) # Apply filter to the base image if filter_type: if filter_type == "Blur": base_image = base_image.filter(ImageFilter.BLUR) elif filter_type == "Sharpen": base_image = base_image.filter(ImageFilter.SHARPEN) # Add text overlay if text_overlay: draw = ImageDraw.Draw(base_image) font = ImageFont.load_default() text = text_overlay width, height = base_image.size text_width, text_height = draw.textsize(text, font=font) position = (width - text_width - 10, height - text_height - 10) draw.text(position, text, (255, 255, 255), font=font) # Save image to bytes buffer = io.BytesIO() base_image.save(buffer, format="PNG") return buffer.getvalue() # Create Gradio interface with gr.Blocks() as demo: gr.Markdown("## Advanced Image Editor with Overlay") with gr.Row(): with gr.Column(): base_image_input = gr.Image(type="pil", label="Upload Base Image") overlay_image_input = gr.Image(type="pil", label="Upload Overlay Image (optional)") crop_box = gr.Textbox(label="Crop Box (left, top, right, bottom) - Comma-separated", placeholder="0,0,200,200") resize_dims = gr.Textbox(label="Resize Dimensions (width, height) - Comma-separated", placeholder="100,100") brightness = gr.Slider(minimum=0, maximum=2, step=0.1, value=1, label="Brightness") rotate_angle = gr.Slider(minimum=0, maximum=360, step=1, value=0, label="Rotate Angle") flip_horizontal = gr.Checkbox(label="Flip Horizontal") flip_vertical = gr.Checkbox(label="Flip Vertical") filter_type = gr.Dropdown(choices=["None", "Blur", "Sharpen"], label="Filter Type") text_overlay = gr.Textbox(label="Text Overlay", placeholder="Enter text to add to image") overlay_position = gr.Textbox(label="Overlay Position (x, y) - Comma-separated", placeholder="100,100") overlay_opacity = gr.Slider(minimum=0, maximum=100, step=1, value=100, label="Overlay Opacity (%)") submit_btn = gr.Button("Apply Edits") with gr.Column(): image_output = gr.Image(type="pil", label="Edited Image") submit_btn.click( fn=edit_image, inputs=[base_image_input, overlay_image_input, crop_box, resize_dims, brightness, rotate_angle, flip_horizontal, flip_vertical, filter_type, text_overlay, overlay_position, overlay_opacity], outputs=image_output ) demo.launch()