gaur3009's picture
Update app.py
e7ff32b verified
raw
history blame
1.82 kB
import gradio as gr
from PIL import Image, ImageOps, ImageEnhance
def edit_image(img_data, operation, *args):
image = Image.open(img_data)
if operation == "rotate":
angle = int(args[0])
image = image.rotate(angle, expand=True)
elif operation == "crop":
left, top, right, bottom = map(int, args)
image = image.crop((left, top, right, bottom))
elif operation == "resize":
width, height = map(int, args)
image = image.resize((width, height))
elif operation == "flip":
if args[0] == "horizontal":
image = ImageOps.mirror(image)
else:
image = ImageOps.flip(image)
elif operation == "color":
factor = float(args[0])
image = ImageEnhance.Color(image).enhance(factor)
return image
examples = [
["rotate", "90", "crop", "100,100,400,400"],
["resize", "400,400", "flip", "horizontal"],
["color", "1.5"]
]
with gr.Blocks() as demo:
with gr.Row():
gr.Markdown("# Image Editor")
with gr.Row():
with gr.Column():
edit_operation = gr.Dropdown(choices=["rotate", "crop", "resize", "flip", "color"], label="Edit Operation")
edit_args = gr.Textbox(label="Edit Arguments (comma-separated)", placeholder="For rotate: angle, For crop: left,top,right,bottom, For resize: width,height, For flip: horizontal/vertical, For color: factor")
edit_button = gr.Button("Edit Image")
edited_image = gr.Image(label="Edited Image", type="pil", interactive=True)
edit_button.click(
fn=lambda img_data, operation, args: edit_image(img_data, operation, *args.split(',')),
inputs=[edited_image, edit_operation, edit_args],
outputs=[edited_image]
)
demo.queue().launch()