devbernie commited on
Commit
823367f
·
verified ·
1 Parent(s): 5b2d7d3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ from PIL import Image
4
+ import gradio as gr
5
+
6
+ def sharpen_image(img: Image.Image, strength: float = 1.0) -> Image.Image:
7
+ """
8
+ Apply sharpening to an image using OpenCV's unsharp masking technique.
9
+
10
+ Args:
11
+ img: Input PIL Image
12
+ strength: Sharpening intensity (0.0 to 3.0)
13
+
14
+ Returns:
15
+ Sharpened PIL Image
16
+ """
17
+ # Handle different image modes
18
+ if img.mode not in ['L', 'RGB']:
19
+ img = img.convert('RGB')
20
+ original_mode = 'RGB'
21
+ else:
22
+ original_mode = img.mode
23
+
24
+ # Convert PIL Image to numpy array
25
+ img_array = np.array(img)
26
+
27
+ try:
28
+ # Convert to BGR format for OpenCV processing
29
+ if original_mode == 'L':
30
+ img_bgr = cv2.cvtColor(img_array, cv2.COLOR_GRAY2BGR)
31
+ else:
32
+ img_bgr = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
33
+
34
+ # Apply Gaussian Blur
35
+ blurred = cv2.GaussianBlur(img_bgr, (0, 0), 3)
36
+
37
+ # Unsharp masking
38
+ sharpened = cv2.addWeighted(img_bgr, 1.0 + strength, blurred, -strength, 0)
39
+ sharpened = np.clip(sharpened, 0, 255).astype(np.uint8)
40
+
41
+ # Convert back to original color space
42
+ if original_mode == 'L':
43
+ processed = cv2.cvtColor(sharpened, cv2.COLOR_BGR2GRAY)
44
+ else:
45
+ processed = cv2.cvtColor(sharpened, cv2.COLOR_BGR2RGB)
46
+
47
+ except Exception as e:
48
+ raise gr.Error(f"Image processing failed: {str(e)}")
49
+
50
+ return Image.fromarray(processed, mode=original_mode)
51
+
52
+ # Gradio interface
53
+ with gr.Blocks(theme=gr.themes.Soft()) as app:
54
+ gr.Markdown("# 🔍 Image Sharpening Tool")
55
+
56
+ with gr.Row():
57
+ with gr.Column():
58
+ input_image = gr.Image(type="pil", label="Original Image", sources=["upload"])
59
+ strength_slider = gr.Slider(
60
+ minimum=0.0,
61
+ maximum=3.0,
62
+ value=1.0,
63
+ step=0.1,
64
+ label="Sharpening Strength",
65
+ info="Adjust between 0.0 (no effect) to 3.0 (strongest)"
66
+ )
67
+ process_btn = gr.Button("Process Image", variant="primary")
68
+
69
+ output_image = gr.Image(type="pil", label="Sharpened Image", interactive=False)
70
+
71
+ # Example section
72
+ gr.Examples(
73
+ examples=[
74
+ ["https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg", 1.2],
75
+ ["https://upload.wikimedia.org/wikipedia/commons/thumb/4/41/Sunflower_from_Silesia2.jpg/1280px-Sunflower_from_Silesia2.jpg", 1.5]
76
+ ],
77
+ inputs=[input_image, strength_slider],
78
+ outputs=output_image,
79
+ fn=sharpen_image,
80
+ cache_examples=True
81
+ )
82
+
83
+ # Event handling
84
+ process_btn.click(
85
+ fn=sharpen_image,
86
+ inputs=[input_image, strength_slider],
87
+ outputs=output_image
88
+ )
89
+
90
+ if __name__ == "__main__":
91
+ app.launch(show_error=True)