|
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): |
|
|
|
base_image = base_image.convert("RGBA") |
|
|
|
|
|
if overlay_image: |
|
overlay_image = overlay_image.convert("RGBA") |
|
|
|
|
|
if resize_dims: |
|
width, height = map(int, resize_dims.split(',')) |
|
overlay_image = overlay_image.resize((width, height)) |
|
|
|
|
|
if overlay_opacity is not None: |
|
alpha = overlay_image.split()[3].point(lambda p: p * (overlay_opacity / 100.0)) |
|
overlay_image.putalpha(alpha) |
|
|
|
|
|
if overlay_position: |
|
overlay_position = tuple(map(int, overlay_position.split(','))) |
|
base_image.paste(overlay_image, overlay_position, overlay_image) |
|
|
|
|
|
if crop_box: |
|
left, top, right, bottom = map(int, crop_box.split(',')) |
|
base_image = base_image.crop((left, top, right, bottom)) |
|
|
|
|
|
if resize_dims: |
|
width, height = map(int, resize_dims.split(',')) |
|
base_image = base_image.resize((width, height)) |
|
|
|
|
|
if brightness is not None: |
|
enhancer = ImageEnhance.Brightness(base_image) |
|
base_image = enhancer.enhance(brightness) |
|
|
|
|
|
if rotate_angle: |
|
base_image = base_image.rotate(rotate_angle, expand=True) |
|
|
|
|
|
if flip_horizontal: |
|
base_image = base_image.transpose(Image.FLIP_LEFT_RIGHT) |
|
if flip_vertical: |
|
base_image = base_image.transpose(Image.FLIP_TOP_BOTTOM) |
|
|
|
|
|
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) |
|
|
|
|
|
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) |
|
|
|
|
|
buffer = io.BytesIO() |
|
base_image.save(buffer, format="PNG") |
|
return buffer.getvalue() |
|
|
|
|
|
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() |
|
|