Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,87 +1,28 @@
|
|
1 |
import gradio as gr
|
2 |
-
from PIL import Image, ImageOps
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
if
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
image = ImageOps.mirror(image)
|
30 |
-
else:
|
31 |
-
image = ImageOps.flip(image)
|
32 |
-
elif operation == "color":
|
33 |
-
factor = float(args[0])
|
34 |
-
image = ImageEnhance.Color(image).enhance(factor)
|
35 |
-
|
36 |
-
return image
|
37 |
-
|
38 |
-
def create_demo():
|
39 |
-
with gr.Blocks() as demo:
|
40 |
-
with gr.Row():
|
41 |
-
gr.Markdown("# Image Editor")
|
42 |
-
|
43 |
-
with gr.Row():
|
44 |
-
with gr.Column():
|
45 |
-
edit_operation = gr.Dropdown(choices=["rotate", "crop", "resize", "flip", "color"], label="Edit Operation")
|
46 |
-
input_args = gr.State([])
|
47 |
-
|
48 |
-
angle = gr.Slider(0, 360, step=1, label="Angle", visible=False)
|
49 |
-
left = gr.Slider(0, 500, step=1, label="Left", visible=False)
|
50 |
-
top = gr.Slider(0, 500, step=1, label="Top", visible=False)
|
51 |
-
right = gr.Slider(0, 500, step=1, label="Right", visible=False)
|
52 |
-
bottom = gr.Slider(0, 500, step=1, label="Bottom", visible=False)
|
53 |
-
width = gr.Slider(50, 1000, step=1, label="Width", visible=False)
|
54 |
-
height = gr.Slider(50, 1000, step=1, label="Height", visible=False)
|
55 |
-
flip_direction = gr.Dropdown(choices=["horizontal", "vertical"], label="Direction", visible=False)
|
56 |
-
color_factor = gr.Slider(0.1, 2.0, step=0.1, label="Color Factor", visible=False)
|
57 |
-
|
58 |
-
edit_button = gr.Button("Edit Image")
|
59 |
-
uploaded_image = gr.Image(label="Upload Image", type="pil")
|
60 |
-
edited_image = gr.Image(label="Edited Image", type="pil", interactive=True)
|
61 |
-
|
62 |
-
def update_inputs(operation):
|
63 |
-
if operation == "rotate":
|
64 |
-
return [gr.update(visible=True), angle]
|
65 |
-
elif operation == "crop":
|
66 |
-
return [gr.update(visible=True), left, top, right, bottom]
|
67 |
-
elif operation == "resize":
|
68 |
-
return [gr.update(visible=True), width, height]
|
69 |
-
elif operation == "flip":
|
70 |
-
return [gr.update(visible=True), flip_direction]
|
71 |
-
elif operation == "color":
|
72 |
-
return [gr.update(visible=True), color_factor]
|
73 |
-
else:
|
74 |
-
return []
|
75 |
-
|
76 |
-
edit_operation.change(fn=update_inputs, inputs=[edit_operation], outputs=[angle, left, top, right, bottom, width, height, flip_direction, color_factor])
|
77 |
-
|
78 |
-
edit_button.click(
|
79 |
-
fn=lambda img_data, operation, *args: edit_image(img_data, operation, *args),
|
80 |
-
inputs=[uploaded_image, edit_operation, angle, left, top, right, bottom, width, height, flip_direction, color_factor],
|
81 |
-
outputs=[edited_image]
|
82 |
-
)
|
83 |
-
|
84 |
-
return demo
|
85 |
-
|
86 |
-
demo = create_demo()
|
87 |
-
demo.queue().launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from PIL import Image, ImageOps
|
3 |
+
|
4 |
+
def edit_image(image, grayscale, flip, rotate):
|
5 |
+
img = Image.open(image)
|
6 |
+
if grayscale:
|
7 |
+
img = ImageOps.grayscale(img)
|
8 |
+
if flip:
|
9 |
+
img = ImageOps.flip(img)
|
10 |
+
if rotate:
|
11 |
+
img = img.rotate(rotate)
|
12 |
+
return img
|
13 |
+
|
14 |
+
interface = gr.Interface(
|
15 |
+
fn=edit_image,
|
16 |
+
inputs=[
|
17 |
+
gr.inputs.Image(type="file", label="Upload Image"),
|
18 |
+
gr.inputs.Checkbox(label="Grayscale"),
|
19 |
+
gr.inputs.Checkbox(label="Flip Vertically"),
|
20 |
+
gr.inputs.Slider(minimum=0, maximum=360, step=1, default=0, label="Rotate Angle")
|
21 |
+
],
|
22 |
+
outputs="image",
|
23 |
+
live=True,
|
24 |
+
title="Image Editor",
|
25 |
+
description="Upload an image and apply transformations"
|
26 |
+
)
|
27 |
+
|
28 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|