Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image, ImageOps, ImageEnhance, ImageFilter, ImageDraw
|
3 |
import numpy as np
|
|
|
|
|
|
|
4 |
|
5 |
-
def edit_image(image, grayscale, flip, rotate, brightness, contrast, color,
|
6 |
img = Image.open(image)
|
7 |
|
8 |
if grayscale:
|
@@ -25,12 +28,14 @@ def edit_image(image, grayscale, flip, rotate, brightness, contrast, color, crop
|
|
25 |
img = enhancer.enhance(color)
|
26 |
|
27 |
# Apply crop
|
28 |
-
if
|
29 |
-
|
|
|
30 |
|
31 |
# Apply resize
|
32 |
if resize:
|
33 |
-
|
|
|
34 |
|
35 |
# Apply blur
|
36 |
if blur > 0:
|
@@ -43,12 +48,47 @@ def edit_image(image, grayscale, flip, rotate, brightness, contrast, color, crop
|
|
43 |
# Draw text
|
44 |
if draw_text:
|
45 |
draw = ImageDraw.Draw(img)
|
46 |
-
|
|
|
47 |
|
48 |
return img
|
49 |
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
interface = gr.Interface(
|
54 |
fn=edit_image,
|
@@ -60,7 +100,7 @@ interface = gr.Interface(
|
|
60 |
gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Brightness"),
|
61 |
gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Contrast"),
|
62 |
gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Color"),
|
63 |
-
gr.Textbox(label="Crop (
|
64 |
gr.Textbox(label="Resize (width, height)", placeholder="e.g., 800,600"),
|
65 |
gr.Slider(minimum=0, maximum=10, step=0.1, value=0, label="Blur"),
|
66 |
gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Sharpness"),
|
@@ -72,7 +112,10 @@ interface = gr.Interface(
|
|
72 |
outputs=gr.Image(),
|
73 |
live=True,
|
74 |
title="Advanced Image Editor",
|
75 |
-
description="Upload an image and apply various transformations including brightness, contrast, color adjustments, cropping, resizing, blurring, and adding text."
|
|
|
|
|
|
|
76 |
)
|
77 |
|
78 |
-
interface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image, ImageOps, ImageEnhance, ImageFilter, ImageDraw
|
3 |
import numpy as np
|
4 |
+
import base64
|
5 |
+
from io import BytesIO
|
6 |
+
import json
|
7 |
|
8 |
+
def edit_image(image, grayscale, flip, rotate, brightness, contrast, color, crop_data, resize, blur, sharpness, draw_text, text_position, text_color, text_size):
|
9 |
img = Image.open(image)
|
10 |
|
11 |
if grayscale:
|
|
|
28 |
img = enhancer.enhance(color)
|
29 |
|
30 |
# Apply crop
|
31 |
+
if crop_data:
|
32 |
+
crop = json.loads(crop_data)
|
33 |
+
img = img.crop((crop['x'], crop['y'], crop['x'] + crop['width'], crop['y'] + crop['height']))
|
34 |
|
35 |
# Apply resize
|
36 |
if resize:
|
37 |
+
width, height = map(int, resize.split(","))
|
38 |
+
img = img.resize((width, height))
|
39 |
|
40 |
# Apply blur
|
41 |
if blur > 0:
|
|
|
48 |
# Draw text
|
49 |
if draw_text:
|
50 |
draw = ImageDraw.Draw(img)
|
51 |
+
x, y = map(int, text_position.split(","))
|
52 |
+
draw.text((x, y), draw_text, fill=text_color, font=None, anchor=None, spacing=4, align="left")
|
53 |
|
54 |
return img
|
55 |
|
56 |
+
html_content = """
|
57 |
+
<!DOCTYPE html>
|
58 |
+
<html lang="en">
|
59 |
+
<head>
|
60 |
+
<link href="https://unpkg.com/cropperjs/dist/cropper.css" rel="stylesheet">
|
61 |
+
</head>
|
62 |
+
<body>
|
63 |
+
<div>
|
64 |
+
<img id="image" style="max-width: 100%;">
|
65 |
+
</div>
|
66 |
+
<script src="https://unpkg.com/cropperjs"></script>
|
67 |
+
<script>
|
68 |
+
const image = document.getElementById('image');
|
69 |
+
const cropper = new Cropper(image, {
|
70 |
+
aspectRatio: NaN,
|
71 |
+
viewMode: 1,
|
72 |
+
autoCropArea: 1,
|
73 |
+
crop(event) {
|
74 |
+
const cropData = {
|
75 |
+
x: event.detail.x,
|
76 |
+
y: event.detail.y,
|
77 |
+
width: event.detail.width,
|
78 |
+
height: event.detail.height
|
79 |
+
};
|
80 |
+
document.querySelector('textarea[name="data"]').value = JSON.stringify(cropData);
|
81 |
+
}
|
82 |
+
});
|
83 |
+
// Function to load image into cropper
|
84 |
+
window.loadImage = function (src) {
|
85 |
+
image.src = src;
|
86 |
+
cropper.replace(src);
|
87 |
+
}
|
88 |
+
</script>
|
89 |
+
</body>
|
90 |
+
</html>
|
91 |
+
"""
|
92 |
|
93 |
interface = gr.Interface(
|
94 |
fn=edit_image,
|
|
|
100 |
gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Brightness"),
|
101 |
gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Contrast"),
|
102 |
gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Color"),
|
103 |
+
gr.Textbox(label="Crop Data (JSON)", placeholder="{'x': 100, 'y': 100, 'width': 400, 'height': 400}"),
|
104 |
gr.Textbox(label="Resize (width, height)", placeholder="e.g., 800,600"),
|
105 |
gr.Slider(minimum=0, maximum=10, step=0.1, value=0, label="Blur"),
|
106 |
gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Sharpness"),
|
|
|
112 |
outputs=gr.Image(),
|
113 |
live=True,
|
114 |
title="Advanced Image Editor",
|
115 |
+
description="Upload an image and apply various transformations including brightness, contrast, color adjustments, cropping, resizing, blurring, and adding text.",
|
116 |
+
layout="vertical",
|
117 |
+
theme="default",
|
118 |
+
css=html_content
|
119 |
)
|
120 |
|
121 |
+
interface.launch(share=True, debug=True, inline=True)
|