File size: 4,150 Bytes
408242e
01ce57c
8bfb11f
bfed5d9
 
 
ba0a843
bfed5d9
01ce57c
8bfb11f
01ce57c
 
 
 
 
 
519ee0e
01ce57c
 
 
8bfb11f
01ce57c
 
 
8bfb11f
01ce57c
 
 
 
 
bfed5d9
 
 
01ce57c
 
 
bfed5d9
 
01ce57c
 
 
 
 
 
 
 
 
 
 
 
bfed5d9
 
01ce57c
 
ba0a843
bfed5d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8bfb11f
01ce57c
 
ba0a843
01ce57c
 
 
 
 
 
 
bfed5d9
01ce57c
 
 
 
 
 
 
ba0a843
01ce57c
 
 
bfed5d9
 
 
 
ba0a843
 
bfed5d9
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import gradio as gr
from PIL import Image, ImageOps, ImageEnhance, ImageFilter, ImageDraw
import numpy as np
import base64
from io import BytesIO
import json

def edit_image(image, grayscale, flip, rotate, brightness, contrast, color, crop_data, resize, blur, sharpness, draw_text, text_position, text_color, text_size):
    img = Image.open(image)
    
    if grayscale:
        img = ImageOps.grayscale(img)
    if flip:
        img = ImageOps.flip(img)
    if rotate:
        img = img.rotate(rotate)
    
    # Apply brightness
    enhancer = ImageEnhance.Brightness(img)
    img = enhancer.enhance(brightness)
    
    # Apply contrast
    enhancer = ImageEnhance.Contrast(img)
    img = enhancer.enhance(contrast)
    
    # Apply color
    enhancer = ImageEnhance.Color(img)
    img = enhancer.enhance(color)
    
    # Apply crop
    if crop_data:
        crop = json.loads(crop_data)
        img = img.crop((crop['x'], crop['y'], crop['x'] + crop['width'], crop['y'] + crop['height']))
    
    # Apply resize
    if resize:
        width, height = map(int, resize.split(","))
        img = img.resize((width, height))
    
    # Apply blur
    if blur > 0:
        img = img.filter(ImageFilter.GaussianBlur(blur))
    
    # Apply sharpness
    enhancer = ImageEnhance.Sharpness(img)
    img = enhancer.enhance(sharpness)
    
    # Draw text
    if draw_text:
        draw = ImageDraw.Draw(img)
        x, y = map(int, text_position.split(","))
        draw.text((x, y), draw_text, fill=text_color, font=None, anchor=None, spacing=4, align="left")
    
    return img

html_content = """
<!DOCTYPE html>
<html lang="en">
<head>
    <link href="https://unpkg.com/cropperjs/dist/cropper.css" rel="stylesheet">
</head>
<body>
    <div>
        <img id="image" style="max-width: 100%;">
    </div>
    <script src="https://unpkg.com/cropperjs"></script>
    <script>
        const image = document.getElementById('image');
        const cropper = new Cropper(image, {
            aspectRatio: NaN,
            viewMode: 1,
            autoCropArea: 1,
            crop(event) {
                const cropData = {
                    x: event.detail.x,
                    y: event.detail.y,
                    width: event.detail.width,
                    height: event.detail.height
                };
                document.querySelector('textarea[name="data"]').value = JSON.stringify(cropData);
            }
        });
        // Function to load image into cropper
        window.loadImage = function (src) {
            image.src = src;
            cropper.replace(src);
        }
    </script>
</body>
</html>
"""

interface = gr.Interface(
    fn=edit_image,
    inputs=[
        gr.Image(type="filepath", label="Upload Image"),
        gr.Checkbox(label="Grayscale"),
        gr.Checkbox(label="Flip Vertically"),
        gr.Slider(minimum=0, maximum=360, step=1, value=0, label="Rotate Angle"),
        gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Brightness"),
        gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Contrast"),
        gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Color"),
        gr.Textbox(label="Crop Data (JSON)", placeholder="{'x': 100, 'y': 100, 'width': 400, 'height': 400}"),
        gr.Textbox(label="Resize (width, height)", placeholder="e.g., 800,600"),
        gr.Slider(minimum=0, maximum=10, step=0.1, value=0, label="Blur"),
        gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Sharpness"),
        gr.Textbox(label="Draw Text", placeholder="e.g., Hello World"),
        gr.Textbox(label="Text Position (x, y)", placeholder="e.g., 100,100"),
        gr.ColorPicker(label="Text Color"),
        gr.Slider(minimum=10, maximum=100, step=1, value=30, label="Text Size")
    ],
    outputs=gr.Image(),
    live=True,
    title="Advanced Image Editor",
    description="Upload an image and apply various transformations including brightness, contrast, color adjustments, cropping, resizing, blurring, and adding text.",
    layout="vertical",
    theme="default",
    css=html_content
)

interface.launch(share=True, debug=True, inline=True)