Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
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)
|
@@ -9,20 +9,55 @@ def edit_image(image, grayscale, flip, rotate):
|
|
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.Image(type="filepath", label="Upload Image"),
|
18 |
gr.Checkbox(label="Grayscale"),
|
19 |
gr.Checkbox(label="Flip Vertically"),
|
20 |
-
gr.Slider(minimum=0, maximum=360, step=1, value=0, label="Rotate Angle")
|
|
|
|
|
|
|
|
|
|
|
21 |
],
|
22 |
outputs=gr.Image(),
|
23 |
live=True,
|
24 |
-
title="Image Editor",
|
25 |
-
description="Upload an image and apply transformations"
|
26 |
)
|
27 |
|
28 |
-
interface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from PIL import Image, ImageOps, ImageEnhance, ImageDraw
|
3 |
|
4 |
+
def edit_image(image, grayscale, flip, rotate, brightness, contrast, color, color_picker, coordinates):
|
5 |
img = Image.open(image)
|
6 |
if grayscale:
|
7 |
img = ImageOps.grayscale(img)
|
|
|
9 |
img = ImageOps.flip(img)
|
10 |
if rotate:
|
11 |
img = img.rotate(rotate)
|
12 |
+
|
13 |
+
# Apply brightness
|
14 |
+
enhancer = ImageEnhance.Brightness(img)
|
15 |
+
img = enhancer.enhance(brightness)
|
16 |
+
|
17 |
+
# Apply contrast
|
18 |
+
enhancer = ImageEnhance.Contrast(img)
|
19 |
+
img = enhancer.enhance(contrast)
|
20 |
+
|
21 |
+
# Apply color
|
22 |
+
enhancer = ImageEnhance.Color(img)
|
23 |
+
img = enhancer.enhance(color)
|
24 |
+
|
25 |
+
# Change color of selected area
|
26 |
+
if coordinates:
|
27 |
+
draw = ImageDraw.Draw(img)
|
28 |
+
for coord in coordinates:
|
29 |
+
draw.rectangle(coord, fill=color_picker, outline=None)
|
30 |
+
|
31 |
return img
|
32 |
|
33 |
+
def parse_coordinates(coordinate_str):
|
34 |
+
coordinates = []
|
35 |
+
if coordinate_str:
|
36 |
+
for coord in coordinate_str.split(';'):
|
37 |
+
try:
|
38 |
+
x1, y1, x2, y2 = map(int, coord.split(','))
|
39 |
+
coordinates.append((x1, y1, x2, y2))
|
40 |
+
except ValueError:
|
41 |
+
continue
|
42 |
+
return coordinates
|
43 |
+
|
44 |
interface = gr.Interface(
|
45 |
fn=edit_image,
|
46 |
inputs=[
|
47 |
gr.Image(type="filepath", label="Upload Image"),
|
48 |
gr.Checkbox(label="Grayscale"),
|
49 |
gr.Checkbox(label="Flip Vertically"),
|
50 |
+
gr.Slider(minimum=0, maximum=360, step=1, value=0, label="Rotate Angle"),
|
51 |
+
gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Brightness"),
|
52 |
+
gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Contrast"),
|
53 |
+
gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Color"),
|
54 |
+
gr.ColorPicker(label="Select Color"),
|
55 |
+
gr.Textbox(label="Coordinates (x1,y1,x2,y2;...)", placeholder="Enter coordinates separated by semicolons")
|
56 |
],
|
57 |
outputs=gr.Image(),
|
58 |
live=True,
|
59 |
+
title="Advanced Image Editor",
|
60 |
+
description="Upload an image and apply various transformations including brightness, contrast, color adjustments, and change the color of specific areas."
|
61 |
)
|
62 |
|
63 |
+
interface.launch()
|