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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from datetime import datetime
|
5 |
+
import random
|
6 |
+
|
7 |
+
def basic_filter(image, filter_type):
|
8 |
+
"""Apply basic image filters"""
|
9 |
+
if filter_type == "Gray Toning":
|
10 |
+
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
11 |
+
elif filter_type == "Sepia":
|
12 |
+
sepia_filter = np.array([
|
13 |
+
[0.272, 0.534, 0.131],
|
14 |
+
[0.349, 0.686, 0.168],
|
15 |
+
[0.393, 0.769, 0.189]
|
16 |
+
])
|
17 |
+
return cv2.transform(image, sepia_filter)
|
18 |
+
elif filter_type == "Voyeur":
|
19 |
+
# Improved X-ray effect
|
20 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
21 |
+
inverted = cv2.bitwise_not(gray)
|
22 |
+
# Increase contrast
|
23 |
+
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
|
24 |
+
enhanced = clahe.apply(inverted)
|
25 |
+
# Sharpen
|
26 |
+
kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
|
27 |
+
sharpened = cv2.filter2D(enhanced, -1, kernel)
|
28 |
+
return cv2.cvtColor(sharpened, cv2.COLOR_GRAY2BGR)
|
29 |
+
elif filter_type == "Bulanıklaştır":
|
30 |
+
return cv2.GaussianBlur(image, (15, 15), 0)
|
31 |
+
|
32 |
+
def classic_filter(image, filter_type):
|
33 |
+
"""Classical display filters"""
|
34 |
+
if filter_type == "Karakalem Effect":
|
35 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
36 |
+
inverted = cv2.bitwise_not(gray)
|
37 |
+
blurred = cv2.GaussianBlur(inverted, (21, 21), 0)
|
38 |
+
sketch = cv2.divide(gray, cv2.subtract(255, blurred), scale=256)
|
39 |
+
return cv2.cvtColor(sketch, cv2.COLOR_GRAY2BGR)
|
40 |
+
|
41 |
+
elif filter_type == "Sharpen":
|
42 |
+
kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
|
43 |
+
return cv2.filter2D(image, -1, kernel)
|
44 |
+
|
45 |
+
elif filter_type == "Embossing":
|
46 |
+
kernel = np.array([[0,-1,-1], [1,0,-1], [1,1,0]])
|
47 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
48 |
+
emboss = cv2.filter2D(gray, -1, kernel) + 128
|
49 |
+
return cv2.cvtColor(emboss, cv2.COLOR_GRAY2BGR)
|
50 |
+
|
51 |
+
elif filter_type == "Edge Detection":
|
52 |
+
edges = cv2.Canny(image, 100, 200)
|
53 |
+
return cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
|
54 |
+
|
55 |
+
def createer_filter(image, filter_type):
|
56 |
+
"""Creative and unusual image filters"""
|
57 |
+
if filter_type == "Pixel Art":
|
58 |
+
h, w = image.shape[:2]
|
59 |
+
piksel_size = 20
|
60 |
+
small = cv2.resize(image, (w//piksel_size, h//piksel_size))
|
61 |
+
return cv2.resize(small, (w, h), interpolation=cv2.INTER_NEAREST)
|
62 |
+
|
63 |
+
elif filter_type == "Mosaic Effect":
|
64 |
+
h, w = image.shape[:2]
|
65 |
+
mozaik_size = 30
|
66 |
+
for i in range(0, h, mozaik_size):
|
67 |
+
for j in range(0, w, mozaik_size):
|
68 |
+
roi = image[i:i+mozaik_size, j:j+mozaik_size]
|
69 |
+
if roi.size > 0:
|
70 |
+
color = np.mean(roi, axis=(0,1))
|
71 |
+
image[i:i+mozaik_size, j:j+mozaik_size] = color
|
72 |
+
return image
|
73 |
+
|
74 |
+
elif filter_type == "Rainbow":
|
75 |
+
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
|
76 |
+
h, w = image.shape[:2]
|
77 |
+
for i in range(h):
|
78 |
+
hsv[i, :, 0] = (hsv[i, :, 0] + i % 180).astype(np.uint8)
|
79 |
+
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
|
80 |
+
|
81 |
+
elif filter_type == "Night Vision":
|
82 |
+
green_image = image.copy()
|
83 |
+
green_image[:,:,0] = 0 # Blue channel
|
84 |
+
green_image[:,:,2] = 0 # Red channel
|
85 |
+
return cv2.addWeighted(green_image, 1.5, np.zeros(image.shape, image.dtype), 0, -50)
|
86 |
+
|
87 |
+
def special_effects(image, filter_type):
|
88 |
+
"""Apply special effects"""
|
89 |
+
if filter_type == "Matrix Effect":
|
90 |
+
green_matrix = np.zeros_like(image)
|
91 |
+
green_matrix[:,:,1] = image[:,:,1] # Only green channel
|
92 |
+
random_brightness = np.random.randint(0, 255, size=image.shape[:2])
|
93 |
+
green_matrix[:,:,1] = np.minimum(green_matrix[:,:,1] + random_brightness, 255)
|
94 |
+
return green_matrix
|
95 |
+
|
96 |
+
elif filter_type == "Wave Effect":
|
97 |
+
rows, cols = image.shape[:2]
|
98 |
+
img_output = np.zeros(image.shape, dtype=image.dtype)
|
99 |
+
|
100 |
+
for i in range(rows):
|
101 |
+
for j in range(cols):
|
102 |
+
offset_x = int(25.0 * np.sin(2 * 3.14 * i / 180))
|
103 |
+
offset_y = int(25.0 * np.cos(2 * 3.14 * j / 180))
|
104 |
+
if i+offset_x < rows and j+offset_y < cols:
|
105 |
+
img_output[i,j] = image[(i+offset_x)%rows,(j+offset_y)%cols]
|
106 |
+
else:
|
107 |
+
img_output[i,j] = 0
|
108 |
+
return img_output
|
109 |
+
|
110 |
+
elif filter_type == "Time Stamp":
|
111 |
+
output = image.copy()
|
112 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
113 |
+
font = cv2.FONT_HERSHEY_SIMPLEX
|
114 |
+
cv2.putText(output, timestamp, (10, 30), font, 1, (255, 255, 255), 2)
|
115 |
+
return output
|
116 |
+
|
117 |
+
elif filter_type == "Glitch Effect":
|
118 |
+
glitch = image.copy()
|
119 |
+
h, w = image.shape[:2]
|
120 |
+
for _ in range(10):
|
121 |
+
x1 = random.randint(0, w-50)
|
122 |
+
y1 = random.randint(0, h-50)
|
123 |
+
x2 = random.randint(x1, min(x1+50, w))
|
124 |
+
y2 = random.randint(y1, min(y1+50, h))
|
125 |
+
glitch[y1:y2, x1:x2] = np.roll(glitch[y1:y2, x1:x2],
|
126 |
+
random.randint(-20, 20),
|
127 |
+
axis=random.randint(0, 1))
|
128 |
+
return glitch
|
129 |
+
|
130 |
+
def artistic_filter(image, filter_type):
|
131 |
+
"""Apply artistic image filters"""
|
132 |
+
if filter_type == "Pop Art":
|
133 |
+
img_small = cv2.resize(image, None, fx=0.5, fy=0.5)
|
134 |
+
img_color = cv2.resize(img_small, (image.shape[1], image.shape[0]))
|
135 |
+
for _ in range(2):
|
136 |
+
img_color = cv2.bilateralFilter(img_color, 9, 300, 300)
|
137 |
+
hsv = cv2.cvtColor(img_color, cv2.COLOR_BGR2HSV)
|
138 |
+
hsv[:,:,1] = hsv[:,:,1]*1.5
|
139 |
+
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
|
140 |
+
|
141 |
+
elif filter_type == "Oil Paint":
|
142 |
+
ret = np.float32(image.copy())
|
143 |
+
ret = cv2.bilateralFilter(ret, 9, 75, 75)
|
144 |
+
ret = cv2.detailEnhance(ret, sigma_s=15, sigma_r=0.15)
|
145 |
+
ret = cv2.edgePreservingFilter(ret, flags=1, sigma_s=60, sigma_r=0.4)
|
146 |
+
return np.uint8(ret)
|
147 |
+
|
148 |
+
elif filter_type == "Cartoon Film":
|
149 |
+
# Improved cartoon effect
|
150 |
+
color = image.copy()
|
151 |
+
gray = cv2.cvtColor(color, cv2.COLOR_BGR2GRAY)
|
152 |
+
gray = cv2.medianBlur(gray, 5)
|
153 |
+
edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9)
|
154 |
+
color = cv2.bilateralFilter(color, 9, 300, 300)
|
155 |
+
cartoon = cv2.bitwise_and(color, color, mask=edges)
|
156 |
+
# Increase color saturation
|
157 |
+
hsv = cv2.cvtColor(cartoon, cv2.COLOR_BGR2HSV)
|
158 |
+
hsv[:,:,1] = hsv[:,:,1]*1.4 # Increase in saturation
|
159 |
+
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
|
160 |
+
|
161 |
+
def atmospheric_filter(image, filter_type):
|
162 |
+
"""Atmospheric Filters"""
|
163 |
+
if filter_type == "Autumn":
|
164 |
+
# Improved autumn effect
|
165 |
+
autumn_filter = np.array([
|
166 |
+
[0.393, 0.769, 0.189],
|
167 |
+
[0.349, 0.686, 0.168],
|
168 |
+
[0.272, 0.534, 0.131]
|
169 |
+
])
|
170 |
+
autumn = cv2.transform(image, autumn_filter)
|
171 |
+
# Increase color temperature
|
172 |
+
hsv = cv2.cvtColor(autumn, cv2.COLOR_BGR2HSV)
|
173 |
+
hsv[:,:,0] = hsv[:,:,0]*0.8 # Orange/yellow tones
|
174 |
+
hsv[:,:,1] = hsv[:,:,1]*1.2 # Increase saturation
|
175 |
+
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
|
176 |
+
|
177 |
+
elif filter_type == "Nostalgia":
|
178 |
+
# Improved nostalgia effect
|
179 |
+
# Reduce contrast and add yellowish tone
|
180 |
+
image = cv2.convertScaleAbs(image, alpha=0.9, beta=10)
|
181 |
+
sepia = cv2.transform(image, np.array([
|
182 |
+
[0.393, 0.769, 0.189],
|
183 |
+
[0.349, 0.686, 0.168],
|
184 |
+
[0.272, 0.534, 0.131]
|
185 |
+
]))
|
186 |
+
# Dimming effect in corners
|
187 |
+
h, w = image.shape[:2]
|
188 |
+
kernel = np.zeros((h, w))
|
189 |
+
center = (h//2, w//2)
|
190 |
+
for i in range(h):
|
191 |
+
for j in range(w):
|
192 |
+
dist = np.sqrt((i-center[0])**2 + (j-center[1])**2)
|
193 |
+
kernel[i,j] = 1 - min(1, dist/(np.sqrt(h**2 + w**2)/2))
|
194 |
+
kernel = np.dstack([kernel]*3)
|
195 |
+
return cv2.multiply(sepia, kernel).astype(np.uint8)
|
196 |
+
|
197 |
+
elif filter_type == "Increase Brightness":
|
198 |
+
# Improved brightness enhancement
|
199 |
+
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
|
200 |
+
# Increase brightness
|
201 |
+
hsv[:,:,2] = cv2.convertScaleAbs(hsv[:,:,2], alpha=1.2, beta=30)
|
202 |
+
# Increase the contrast slightly
|
203 |
+
return cv2.convertScaleAbs(cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR), alpha=1.1, beta=0)
|
204 |
+
|
205 |
+
def goruntu_isleme(image, filter_type):
|
206 |
+
"""Function of main image processing"""
|
207 |
+
if image is None:
|
208 |
+
return None
|
209 |
+
|
210 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
211 |
+
|
212 |
+
# Process by filter categories
|
213 |
+
basic_filter_listesi = ["Grey Toning", "Sepia", "Röntgen", "Frain"]
|
214 |
+
Classic_filters_listesi = ["Caracalem Effect", "Cutify", "Emovation", "Blank Detection"]
|
215 |
+
creative_filters_listesi = ["Pixel Art", "Mosaic Effect", "Rainbow", "Night Vision"]
|
216 |
+
ozel_efektler_listesi = ["Matrix Effect", "Window", "Time Stamp," "Glitch Effect"]
|
217 |
+
artistic_filters_listesi = ["Pop Art", "Oil Paint", "Cartoon"]
|
218 |
+
atmospheric_filters_list = ["Autumn", "Nostalgia", "Increasing Shiny"]
|
219 |
+
|
220 |
+
if filter_type in basic_filter_list:
|
221 |
+
output = basic_filter(image, filter_type)
|
222 |
+
elif filter_type in classic_filter_list:
|
223 |
+
output = classic_filters(image, filter_type)
|
224 |
+
elif filter_type's creation_filter_list:
|
225 |
+
output = createer_filters(image, filter_type)
|
226 |
+
elif filter_type in special_efektler_list:
|
227 |
+
output = special_efekts(image, filter_type)
|
228 |
+
elif filter_type in artistic_filters_list:
|
229 |
+
output = artistic_filters (image, filter_type)
|
230 |
+
elif filter_type in atmospheric_filters_list:
|
231 |
+
output = atmospheric_filters(image, filter_type)
|
232 |
+
else:
|
233 |
+
output = image
|
234 |
+
|
235 |
+
return cv2.cvtColor(output, cv2.COLOR_BGR2RGB) if len(output.shape) == 3 else output
|
236 |
+
|
237 |
+
# Gradio interface
|
238 |
+
with gr.Blocks(theme=gr.themes.Monochrome()) as app:
|
239 |
+
gr.Markdown("# 🎨 Super and Unusual Image Filtering Studio")
|
240 |
+
gr.Markdown("### 🌈 Add magical touches to your photos!")
|
241 |
+
|
242 |
+
with gr.Row():
|
243 |
+
with gr.Column():
|
244 |
+
image_input = gr.Image(type="numpy", label="📸 Upload Photos")
|
245 |
+
with gr.Accordion("ℹ️ Filter Categories", open=True):
|
246 |
+
filter_type = gr.Radio(
|
247 |
+
[
|
248 |
+
# Basic Filters
|
249 |
+
"Grey Toning", "Sepia", "Röntgen", "Frain",
|
250 |
+
#classic_filters_list
|
251 |
+
"Karakalem Effect," "Cutify", "Emovation", "Bens Perception,"
|
252 |
+
# Creative Filters
|
253 |
+
"Picles Art," "Mosaic Effect," "Rainbow," "Night Vision,"
|
254 |
+
# Special Effects
|
255 |
+
"Matrix Effect," "Window Effect", "Time Stamp," "Glitch Effect,"
|
256 |
+
# Artistic Filters
|
257 |
+
"Pop Art", "Oil Paint", "Cartoon",
|
258 |
+
#Atmospheric Filters
|
259 |
+
"Autumn", "Nostalgia", "Increasing Shiny"# Basic Filters
|
260 |
+
"Grey Toning", "Sepia", "Röntgen", "Frain",
|
261 |
+
#classic_filters_list
|
262 |
+
"Karakalem Effect," "Cutify", "Emovation", "Bens Perception,"
|
263 |
+
# Creative Filters
|
264 |
+
"Picles Art," "Mosaic Effect," "Rainbow," "Night Vision,"
|
265 |
+
# Special Effects
|
266 |
+
"Matrix Effect," "Window Effect", "Time Stamp," "Glitch Effect,"
|
267 |
+
# Artistic Filters
|
268 |
+
"Pop Art", "Oil Paint", "Cartoon",
|
269 |
+
#Atmospheric Filters
|
270 |
+
"Autumn", "Nostalgia", "Increase of Shiny"
|
271 |
+
],
|
272 |
+
label="🎭 Select Filter",
|
273 |
+
info="Choose the magic effect you want"
|
274 |
+
)
|
275 |
+
submit_button = gr.Button("✨ Apply Filter", variant="primary")
|
276 |
+
|
277 |
+
with gr.Column():
|
278 |
+
image_output = gr.Image(label="🖼️ Filtered Photo")
|
279 |
+
|
280 |
+
with gr.Accordion("📝 Filter Descriptions", open=False):
|
281 |
+
gr.Markdown("""
|
282 |
+
### 🎨 Filter Categories and Effects
|
283 |
+
|
284 |
+
#### 📊 Basic Filters
|
285 |
+
- **Grey Toning**: Turns the image into a classic style in black and white tones
|
286 |
+
- **Sepia**: Adds warm brown tones that give the photo an old photo vibe
|
287 |
+
- **Cennant**: Acronyce scanning effect by adding reverse lighting to the image
|
288 |
+
- **Speaking": It reduces details by creating a soft blur in the image.
|
289 |
+
|
290 |
+
#### Classic Filters List
|
291 |
+
- **Karakalem Effect**: It makes the image look like a charcoal drawing
|
292 |
+
- **Cutify**: Declare the details in the image
|
293 |
+
- **Emboss**: Adds embossing and depth effect to the image
|
294 |
+
- **Bulk Detection: Emphasizes edge lines in the image
|
295 |
+
|
296 |
+
#### s Creative Filters
|
297 |
+
- **Picsel Art***: Dividing the image into small frames in retro pixel style
|
298 |
+
- **Mosaic Effect**: Divids the photo into small pieces of mosaics
|
299 |
+
- **Rainbow**: Adds colored rainbow effects to the image
|
300 |
+
- **Night Vision***: Simulates the night vision device effect
|
301 |
+
|
302 |
+
#### s Special Effects
|
303 |
+
- **Matrix Effect**: Matrix movie effect
|
304 |
+
- **Ware Effect**: Adds a water wave-like twisted degradation to the image, creating a feeling of ripple
|
305 |
+
- **Time Stamp***: Adds the date and time of taking the photo on it gives a nostalgic atmosphere
|
306 |
+
- **Glitch Effect***: Adding digital distortions, adds a retro style error effect to the photo
|
307 |
+
|
308 |
+
#### s Artistic Filters
|
309 |
+
- **Pop Art**: With vivid and contrasted colors, Andy Warhol-style creates iconic pop-art effect
|
310 |
+
- **Oil Paint***: Simulates brush strokes, giving the image the appearance of oil painting
|
311 |
+
- **Texture Effect***: Adding surface texture to the image gives a touching depth and a vial of artwork
|
312 |
+
""")
|
313 |
+
|
314 |
+
submit_button.click(
|
315 |
+
goruntu_isleme,
|
316 |
+
inputs=[image_input, filter_type],
|
317 |
+
outputs=image_output
|
318 |
+
)
|
319 |
+
|
320 |
+
app.launch(share=True)
|