gaur3009 commited on
Commit
483cf17
1 Parent(s): 81ee834

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -110
app.py CHANGED
@@ -1,120 +1,83 @@
1
  import gradio as gr
2
- from PIL import Image, ImageOps, ImageEnhance, ImageFilter, ImageDraw
3
- import json
4
 
5
- def edit_image(image, grayscale, flip, rotate, brightness, contrast, color, crop_data, resize, blur, sharpness, draw_text, text_position, text_color, text_size):
6
- img = Image.open(image)
7
-
8
- if grayscale:
9
- img = ImageOps.grayscale(img)
10
- if flip:
11
- img = ImageOps.flip(img)
12
- if rotate:
13
- img = img.rotate(rotate)
14
-
15
- # Apply brightness
16
- enhancer = ImageEnhance.Brightness(img)
17
- img = enhancer.enhance(brightness)
18
-
19
- # Apply contrast
20
- enhancer = ImageEnhance.Contrast(img)
21
- img = enhancer.enhance(contrast)
22
-
23
- # Apply color
24
- enhancer = ImageEnhance.Color(img)
25
- img = enhancer.enhance(color)
26
-
27
- # Apply crop
28
- if crop_data:
29
- crop = json.loads(crop_data)
30
- img = img.crop((crop['x'], crop['y'], crop['x'] + crop['width'], crop['y'] + crop['height']))
31
-
32
- # Apply resize
 
 
 
 
 
 
 
 
 
 
 
33
  if resize:
34
- width, height = map(int, resize.split(","))
35
- img = img.resize((width, height))
36
-
37
- # Apply blur
38
- if blur > 0:
39
- img = img.filter(ImageFilter.GaussianBlur(blur))
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
- html_content = """
54
- <!DOCTYPE html>
55
- <html lang="en">
56
- <head>
57
- <link href="https://unpkg.com/cropperjs/dist/cropper.css" rel="stylesheet">
58
- </head>
59
- <body>
60
- <div>
61
- <img id="image" style="max-width: 100%;">
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=edit_image,
 
 
92
  inputs=[
93
- gr.Image(type="filepath", label="Upload Image"),
94
- gr.Checkbox(label="Grayscale"),
95
- gr.Checkbox(label="Flip Vertically"),
96
- gr.Slider(minimum=0, maximum=360, step=1, value=0, label="Rotate Angle"),
97
- gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Brightness"),
98
- gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Contrast"),
99
- gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Color"),
100
- gr.Textbox(label="Crop Data (JSON)", placeholder="{'x': 100, 'y': 100, 'width': 400, 'height': 400}"),
101
- gr.Textbox(label="Resize (width, height)", placeholder="e.g., 800,600"),
102
- gr.Slider(minimum=0, maximum=10, step=0.1, value=0, label="Blur"),
103
- gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Sharpness"),
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
- # Adding custom HTML to the Gradio interface
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()