jasvir-singh1021 commited on
Commit
bf97f77
·
verified ·
1 Parent(s): e308d28

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -0
app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image, ImageEnhance, ImageOps, ImageFilter
3
+
4
+
5
+ def apply_sepia(image):
6
+ sepia = ImageOps.colorize(ImageOps.grayscale(image), "#704214", "#C0A080")
7
+ return sepia
8
+
9
+
10
+ def enhance_image(
11
+ image, posterize_bits, contrast, brightness, sharpness, color,
12
+ auto_enhance, grayscale, blur_radius,
13
+ edge_enhance, emboss, invert, solarize_threshold, equalize, sepia
14
+ ):
15
+ # Posterize
16
+ if posterize_bits < 8:
17
+ image = ImageOps.posterize(image, posterize_bits)
18
+
19
+ # Contrast
20
+ image = ImageEnhance.Contrast(image).enhance(contrast)
21
+
22
+ # Brightness
23
+ image = ImageEnhance.Brightness(image).enhance(brightness)
24
+
25
+ # Sharpness
26
+ image = ImageEnhance.Sharpness(image).enhance(sharpness)
27
+
28
+ # Color (Saturation)
29
+ image = ImageEnhance.Color(image).enhance(color)
30
+
31
+ # Auto enhance
32
+ if auto_enhance:
33
+ image = ImageOps.autocontrast(image)
34
+
35
+ # Blur
36
+ if blur_radius > 0:
37
+ image = image.filter(ImageFilter.GaussianBlur(blur_radius))
38
+
39
+ # Edge Enhance
40
+ if edge_enhance:
41
+ image = image.filter(ImageFilter.EDGE_ENHANCE)
42
+
43
+ # Emboss
44
+ if emboss:
45
+ image = image.filter(ImageFilter.EMBOSS)
46
+
47
+ # Invert
48
+ if invert:
49
+ image = ImageOps.invert(image.convert("RGB"))
50
+
51
+ # Solarize
52
+ if solarize_threshold < 256:
53
+ image = ImageOps.solarize(image, threshold=solarize_threshold)
54
+
55
+ # Equalize
56
+ if equalize:
57
+ image = ImageOps.equalize(image)
58
+
59
+ # Grayscale
60
+ if grayscale:
61
+ image = ImageOps.grayscale(image)
62
+
63
+ # Sepia
64
+ if sepia:
65
+ image = apply_sepia(image)
66
+
67
+ return image
68
+
69
+
70
+ with gr.Blocks() as demo:
71
+ gr.Markdown("## 🖼️ Advanced Image Enhancer")
72
+
73
+ with gr.Row():
74
+ input_image = gr.Image(type="pil", label="Upload Image")
75
+ output_image = gr.Image(type="pil", label="Enhanced Image")
76
+
77
+ with gr.Accordion("Basic Enhancements", open=True):
78
+ posterize_bits = gr.Slider(1, 8, value=8, step=1, label="Posterize Bits")
79
+ contrast = gr.Slider(0.5, 3.0, value=1.0, step=0.1, label="Contrast")
80
+ brightness = gr.Slider(0.5, 3.0, value=1.0, step=0.1, label="Brightness")
81
+ sharpness = gr.Slider(0.5, 5.0, value=1.0, step=0.1, label="Sharpness")
82
+ color = gr.Slider(0.0, 3.0, value=1.0, step=0.1, label="Color (Saturation)")
83
+ auto_enhance = gr.Checkbox(label="Auto Enhance")
84
+ grayscale = gr.Checkbox(label="Grayscale")
85
+
86
+ with gr.Accordion("Creative Effects", open=False):
87
+ blur_radius = gr.Slider(0, 10, value=0, step=1, label="Blur Radius")
88
+ edge_enhance = gr.Checkbox(label="Edge Enhance")
89
+ emboss = gr.Checkbox(label="Emboss")
90
+ invert = gr.Checkbox(label="Invert Colors")
91
+ solarize_threshold = gr.Slider(0, 255, value=256, step=1, label="Solarize Threshold (<=255 to apply)")
92
+ equalize = gr.Checkbox(label="Equalize Histogram")
93
+ sepia = gr.Checkbox(label="Apply Sepia Tone")
94
+
95
+ enhance_button = gr.Button("Enhance Image")
96
+
97
+ enhance_button.click(
98
+ fn=enhance_image,
99
+ inputs=[
100
+ input_image, posterize_bits, contrast, brightness, sharpness, color,
101
+ auto_enhance, grayscale, blur_radius,
102
+ edge_enhance, emboss, invert, solarize_threshold, equalize, sepia
103
+ ],
104
+ outputs=output_image,
105
+ )
106
+
107
+ demo.launch()