Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,120 +1,83 @@
|
|
1 |
import gradio as gr
|
2 |
-
from PIL import Image, ImageOps,
|
3 |
-
import
|
4 |
|
5 |
-
def
|
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 |
if resize:
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
# Apply sharpness
|
42 |
-
enhancer = ImageEnhance.Sharpness(img)
|
43 |
-
img = enhancer.enhance(sharpness)
|
44 |
-
|
45 |
-
# Draw text
|
46 |
-
if draw_text:
|
47 |
-
draw = ImageDraw.Draw(img)
|
48 |
-
x, y = map(int, text_position.split(","))
|
49 |
-
draw.text((x, y), draw_text, fill=text_color, font=None, anchor=None, spacing=4, align="left")
|
50 |
-
|
51 |
-
return img
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
</div>
|
63 |
-
<script src="https://unpkg.com/cropperjs"></script>
|
64 |
-
<script>
|
65 |
-
const image = document.getElementById('image');
|
66 |
-
const cropper = new Cropper(image, {
|
67 |
-
aspectRatio: NaN,
|
68 |
-
viewMode: 1,
|
69 |
-
autoCropArea: 1,
|
70 |
-
crop(event) {
|
71 |
-
const cropData = {
|
72 |
-
x: event.detail.x,
|
73 |
-
y: event.detail.y,
|
74 |
-
width: event.detail.width,
|
75 |
-
height: event.detail.height
|
76 |
-
};
|
77 |
-
document.querySelector('textarea[name="data"]').value = JSON.stringify(cropData);
|
78 |
-
}
|
79 |
-
});
|
80 |
-
// Function to load image into cropper
|
81 |
-
window.loadImage = function (src) {
|
82 |
-
image.src = src;
|
83 |
-
cropper.replace(src);
|
84 |
-
}
|
85 |
-
</script>
|
86 |
-
</body>
|
87 |
-
</html>
|
88 |
-
"""
|
89 |
|
90 |
interface = gr.Interface(
|
91 |
-
fn=
|
|
|
|
|
92 |
inputs=[
|
93 |
-
gr.Image(type="
|
94 |
-
gr.
|
95 |
-
gr.
|
96 |
-
gr.
|
97 |
-
gr.
|
98 |
-
gr.
|
99 |
-
gr.Slider(
|
100 |
-
gr.
|
101 |
-
gr.
|
102 |
-
gr.Slider(
|
103 |
-
gr.
|
104 |
-
gr.Textbox(label="Draw Text", placeholder="e.g., Hello World"),
|
105 |
-
gr.Textbox(label="Text Position (x, y)", placeholder="e.g., 100,100"),
|
106 |
-
gr.ColorPicker(label="Text Color"),
|
107 |
-
gr.Slider(minimum=10, maximum=100, step=1, value=30, label="Text Size")
|
108 |
],
|
109 |
-
outputs=gr.Image(),
|
110 |
-
live=True
|
111 |
-
title="Advanced Image Editor",
|
112 |
-
description="Upload an image and apply various transformations including brightness, contrast, color adjustments, cropping, resizing, blurring, and adding text.",
|
113 |
)
|
114 |
|
115 |
-
|
116 |
-
with gr.Blocks() as demo:
|
117 |
-
gr.HTML(html_content)
|
118 |
-
interface.render()
|
119 |
-
|
120 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from PIL import Image, ImageOps, ImageDraw, ImageFont
|
3 |
+
import numpy as np
|
4 |
|
5 |
+
def apply_color(image, color):
|
6 |
+
color = Image.new('RGB', image.size, color)
|
7 |
+
colored_image = Image.blend(image.convert('RGB'), color, alpha=0.5)
|
8 |
+
return colored_image
|
9 |
+
|
10 |
+
def add_design(image, design, position):
|
11 |
+
if design:
|
12 |
+
image.paste(design, position, design)
|
13 |
+
return image
|
14 |
+
|
15 |
+
def resize_image(image, size):
|
16 |
+
if size:
|
17 |
+
return image.resize(size)
|
18 |
+
return image
|
19 |
+
|
20 |
+
def rotate_image(image, angle):
|
21 |
+
if angle:
|
22 |
+
return image.rotate(angle, expand=True)
|
23 |
+
return image
|
24 |
+
|
25 |
+
def crop_image(image, crop_coords):
|
26 |
+
if crop_coords:
|
27 |
+
return image.crop(crop_coords)
|
28 |
+
return image
|
29 |
+
|
30 |
+
def add_text(image, text, position, font_size, font_color):
|
31 |
+
if text:
|
32 |
+
draw = ImageDraw.Draw(image)
|
33 |
+
font = ImageFont.load_default()
|
34 |
+
draw.text(position, text, fill=font_color, font=font)
|
35 |
+
return image
|
36 |
+
|
37 |
+
def process_image(image, color, design, design_position, text, text_position, font_size, font_color, resize, rotate, crop_coords):
|
38 |
+
if color:
|
39 |
+
image = apply_color(image, color)
|
40 |
+
if design:
|
41 |
+
image = add_design(image, design, design_position)
|
42 |
+
if text:
|
43 |
+
image = add_text(image, text, text_position, font_size, font_color)
|
44 |
if resize:
|
45 |
+
image = resize_image(image, resize)
|
46 |
+
if rotate:
|
47 |
+
image = rotate_image(image, rotate)
|
48 |
+
if crop_coords:
|
49 |
+
image = crop_image(image, crop_coords)
|
50 |
+
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
+
def get_crop_coords(crop_points):
|
53 |
+
if crop_points:
|
54 |
+
x_coords = [point[0] for point in crop_points]
|
55 |
+
y_coords = [point[1] for point in crop_points]
|
56 |
+
return (min(x_coords), min(y_coords), max(x_coords), max(y_coords))
|
57 |
+
return None
|
58 |
+
|
59 |
+
design_position = (100, 100) # Default position for design
|
60 |
+
text_position = (50, 50) # Default position for text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
interface = gr.Interface(
|
63 |
+
fn=lambda image, color, design, design_position, text, text_position, font_size, font_color, resize, rotate, crop_points: process_image(
|
64 |
+
image, color, design, design_position, text, text_position, font_size, font_color, resize, rotate, get_crop_coords(crop_points)
|
65 |
+
),
|
66 |
inputs=[
|
67 |
+
gr.Image(type="pil"),
|
68 |
+
gr.ColorPicker(label="Color"),
|
69 |
+
gr.Image(type="pil", optional=True, label="Design"),
|
70 |
+
gr.Point(label="Design Position"),
|
71 |
+
gr.Textbox(label="Text"),
|
72 |
+
gr.Point(label="Text Position"),
|
73 |
+
gr.Slider(10, 100, step=1, default=20, label="Font Size"),
|
74 |
+
gr.ColorPicker(label="Font Color"),
|
75 |
+
gr.Points(label="Resize", optional=True),
|
76 |
+
gr.Slider(0, 360, step=1, default=None, label="Rotate"),
|
77 |
+
gr.Points(label="Crop Area")
|
|
|
|
|
|
|
|
|
78 |
],
|
79 |
+
outputs=gr.Image(type="pil"),
|
80 |
+
live=True
|
|
|
|
|
81 |
)
|
82 |
|
83 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|