Spaces:
Sleeping
Sleeping
File size: 1,817 Bytes
408242e a8602f1 408242e e12ff47 e910e8f e12ff47 e910e8f e12ff47 e910e8f e12ff47 a8602f1 e12ff47 a8602f1 e7ff32b a8602f1 e12ff47 a8602f1 e7ff32b 408242e a8602f1 e7ff32b e12ff47 e7ff32b a8602f1 e7ff32b a8602f1 408242e e7ff32b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
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()
|