File size: 13,865 Bytes
ff94c33
 
 
 
1962cca
ff94c33
 
3998a26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ba8051
 
 
ff94c33
 
 
 
 
 
 
 
 
 
 
d1c2160
ff94c33
 
 
 
 
 
 
 
 
 
35887ec
ff94c33
 
 
 
d1c2160
ff94c33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98104e8
ff94c33
98104e8
ff94c33
98104e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff94c33
 
 
 
 
 
 
 
 
 
 
 
21e6a30
ff94c33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98104e8
ff94c33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
283f6d9
 
ff94c33
 
 
 
 
 
 
 
 
283f6d9
ff94c33
 
 
 
 
 
283f6d9
 
ff94c33
 
 
 
 
 
283f6d9
ff94c33
283f6d9
ff94c33
 
283f6d9
 
 
 
ff94c33
 
 
 
 
 
283f6d9
ff94c33
283f6d9
 
ff94c33
 
283f6d9
 
 
ff94c33
 
 
 
 
 
283f6d9
ff94c33
 
 
 
 
 
 
 
 
 
e2d0b33
283f6d9
ff94c33
283f6d9
ff94c33
283f6d9
ff94c33
 
283f6d9
 
ff94c33
 
 
 
 
 
98104e8
 
35887ec
98104e8
e2d0b33
eecfa1c
ff94c33
 
 
 
98104e8
 
d1c2160
 
 
ff94c33
98104e8
ff94c33
 
 
 
 
 
733923d
ff94c33
10f9f14
 
 
 
 
 
 
98104e8
ff94c33
10f9f14
 
9bb9431
98104e8
282467a
98104e8
 
 
 
 
4ba8051
 
282467a
4ba8051
fbe032c
4ba8051
 
98104e8
 
 
4ba8051
35887ec
4ba8051
 
 
e2d0b33
98104e8
eecfa1c
4ba8051
 
7970a22
4ba8051
 
98104e8
4ba8051
282467a
98104e8
 
 
 
 
 
733923d
 
 
1962cca
 
 
 
 
ff94c33
47b6ab1
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import gradio as gr
import cv2
import numpy as np
from datetime import datetime
from diffusers import DiffusionPipeline
import random

#----------Start of theme----------
theme = gr.themes.Soft(
    primary_hue="zinc",
    secondary_hue="stone",
    font=[gr.themes.GoogleFont('Kavivanar'), gr.themes.GoogleFont('Kavivanar'), 'system-ui', 'sans-serif'],
    font_mono=[gr.themes.GoogleFont('Source Code Pro'), gr.themes.GoogleFont('Inconsolata'), gr.themes.GoogleFont('Inconsolata'), 'monospace'],
).set(
    body_background_fill='*primary_100',
    body_text_color='secondary_600',
    body_text_color_subdued='*primary_500',
    body_text_weight='500',
    background_fill_primary='*primary_100',
    background_fill_secondary='*secondary_200',
    color_accent='*primary_300',
    border_color_accent_subdued='*primary_400',
    border_color_primary='*primary_400',
    block_background_fill='*primary_300',
    block_border_width='*panel_border_width',
    block_info_text_color='*primary_700',
    block_info_text_size='*text_md',
    panel_background_fill='*primary_200',
    accordion_text_color='*primary_600',
    table_text_color='*primary_600',
    input_background_fill='*primary_50',
    input_background_fill_focus='*primary_100',
    button_primary_background_fill='*primary_500',
    button_primary_background_fill_hover='*primary_400',
    button_primary_text_color='*primary_50',
    button_primary_text_color_hover='*primary_100',
    button_cancel_background_fill='*primary_500',
    button_cancel_background_fill_hover='*primary_400'
)
#----------End of theme----------

def flip_image(x):
    return np.fliplr(x)

def basic_filter(image, filter_type):
    """Apply basic image filters"""
    if filter_type == "Gray Toning":
        return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    elif filter_type == "Sepia":
        sepia_filter = np.array([
            [0.272, 0.534, 0.131],
            [0.349, 0.686, 0.168],
            [0.393, 0.769, 0.189]
        ])
        return cv2.transform(image, sepia_filter)
    elif filter_type == "X-ray":
        # Improved X-ray effect
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        inverted = cv2.bitwise_not(gray)
        # Increase contrast
        clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
        enhanced = clahe.apply(inverted)
        # Sharpen
        kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
        sharpened = cv2.filter2D(enhanced, -1, kernel)
        return cv2.cvtColor(sharpened, cv2.COLOR_GRAY2BGR)
    elif filter_type == "Burn it":
        return cv2.GaussianBlur(image, (15, 15), 0)

def classic_filter(image, filter_type):
    """Classical display filters"""
    if filter_type == "Charcoal Effect":
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        inverted = cv2.bitwise_not(gray)
        blurred = cv2.GaussianBlur(inverted, (21, 21), 0)
        sketch = cv2.divide(gray, cv2.subtract(255, blurred), scale=256)
        return cv2.cvtColor(sketch, cv2.COLOR_GRAY2BGR)
    
    elif filter_type == "Sharpen":
        kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
        return cv2.filter2D(image, -1, kernel)
    
    elif filter_type == "Embossing":
        kernel = np.array([[0,-1,-1], [1,0,-1], [1,1,0]])
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        emboss = cv2.filter2D(gray, -1, kernel) + 128
        return cv2.cvtColor(emboss, cv2.COLOR_GRAY2BGR)
    
    elif filter_type == "Edge Detection":
        edges = cv2.Canny(image, 100, 200)
        return cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)

def creative_filters(image, filter_type):
    """Creative and unusual image filters"""
    if filter_type == "Pixel Art":
        h, w = image.shape[:2]
        piksel_size = 20
        small = cv2.resize(image, (w//piksel_size, h//piksel_size))
        return cv2.resize(small, (w, h), interpolation=cv2.INTER_NEAREST)
    
    elif filter_type == "Mosaic Effect":
        h, w = image.shape[:2]
        mosaic_size = 30
        for i in range(0, h, mosaic_size):
            for j in range(0, w, mosaic_size):
                roi = image[i:i+mosaic_size, j:j+mosaic_size]
                if roi.size > 0:
                    color = np.mean(roi, axis=(0,1))
                    image[i:i+mosaic_size, j:j+mosaic_size] = color
        return image
    
    elif filter_type == "Rainbow":
        hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
        h, w = image.shape[:2]
        for i in range(h):
            hsv[i, :, 0] = (hsv[i, :, 0] + i % 180).astype(np.uint8)
        return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
    
    elif filter_type == "Night Vision":
        green_image = image.copy()
        green_image[:,:,0] = 0  # Blue channel
        green_image[:,:,2] = 0  # Red channel
        return cv2.addWeighted(green_image, 1.5, np.zeros(image.shape, image.dtype), 0, -50)

def special_effects(image, filter_type):
    """Apply special effects"""
    if filter_type == "Matrix Effect":
        green_matrix = np.zeros_like(image)
        green_matrix[:,:,1] = image[:,:,1]  # Only green channel
        random_brightness = np.random.randint(0, 255, size=image.shape[:2])
        green_matrix[:,:,1] = np.minimum(green_matrix[:,:,1] + random_brightness, 255)
        return green_matrix
    
    elif filter_type == "Wave Effect":
        rows, cols = image.shape[:2]
        img_output = np.zeros(image.shape, dtype=image.dtype)
        
        for i in range(rows):
            for j in range(cols):
                offset_x = int(25.0 * np.sin(2 * 3.14 * i / 180))
                offset_y = int(25.0 * np.cos(2 * 3.14 * j / 180))
                if i+offset_x < rows and j+offset_y < cols:
                    img_output[i,j] = image[(i+offset_x)%rows,(j+offset_y)%cols]
                else:
                    img_output[i,j] = 0
        return img_output
    
    elif filter_type == "Time Stamp":
        output = image.copy()
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.putText(output, timestamp, (10, 30), font, 1, (255, 255, 255), 2)
        return output
    
    elif filter_type == "Glitch Effect":
        glitch = image.copy()
        h, w = image.shape[:2]
        for _ in range(10):
            x1 = random.randint(0, w-50)
            y1 = random.randint(0, h-50)
            x2 = random.randint(x1, min(x1+50, w))
            y2 = random.randint(y1, min(y1+50, h))
            glitch[y1:y2, x1:x2] = np.roll(glitch[y1:y2, x1:x2], 
                                          random.randint(-20, 20), 
                                          axis=random.randint(0, 1))
        return glitch

def artistic_filters(image, filter_type):
    """Applies artistic image filters"""
    if filter_type == "Pop Art":
        img_small = cv2.resize(image, None, fx=0.5, fy=0.5)
        img_color = cv2.resize(img_small, (image.shape[1], image.shape[0]))
        for _ in range(2):
            img_color = cv2.bilateralFilter(img_color, 9, 300, 300)
        hsv = cv2.cvtColor(img_color, cv2.COLOR_BGR2HSV)
        hsv[:,:,1] = hsv[:,:,1]*1.5
        return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
    
    elif filter_type == "Oil Paint":
        ret = np.float32(image.copy())
        ret = cv2.bilateralFilter(ret, 9, 75, 75)
        ret = cv2.detailEnhance(ret, sigma_s=15, sigma_r=0.15)
        ret = cv2.edgePreservingFilter(ret, flags=1, sigma_s=60, sigma_r=0.4)
        return np.uint8(ret)
    
    elif filter_type == "Cartoon":
        # Improved cartoon effect
        color = image.copy()
        gray = cv2.cvtColor(color, cv2.COLOR_BGR2GRAY)
        gray = cv2.medianBlur(gray, 5)
        edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9)
        color = cv2.bilateralFilter(color, 9, 300, 300)
        cartoon = cv2.bitwise_and(color, color, mask=edges)
        # Increase color saturation
        hsv = cv2.cvtColor(cartoon, cv2.COLOR_BGR2HSV)
        hsv[:,:,1] = hsv[:,:,1]*1.4  # saturation increase
        return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)

def atmospheric_filters(image, filter_type):
    """atmospheric filters"""
    if filter_type == "Autumn":
        # Genhanced autumn effect
        autumn_filter = np.array([
            [0.393, 0.769, 0.189],
            [0.349, 0.686, 0.168],
            [0.272, 0.534, 0.131]
        ])
        autumn = cv2.transform(image, autumn_filter)
        # Increase color temperature
        hsv = cv2.cvtColor(autumn, cv2.COLOR_BGR2HSV)
        hsv[:,:,0] = hsv[:,:,0]*0.8  # Shift to orange/yellow tones
        hsv[:,:,1] = hsv[:,:,1]*1.2  # Increase saturation
        return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
    
    elif filter_type == "Nostalgia":
        # Improved nostalgia effect
        # Reduce contrast and add yellowish tone
        image = cv2.convertScaleAbs(image, alpha=0.9, beta=10)
        sepia = cv2.transform(image, np.array([
            [0.393, 0.769, 0.189],
            [0.349, 0.686, 0.168],
            [0.272, 0.534, 0.131]
        ]))
        # Darkening effect in corners
        h, w = image.shape[:2]
        kernel = np.zeros((h, w))
        center = (h//2, w//2)
        for i in range(h):
            for j in range(w):
                dist = np.sqrt((i-center[0])**2 + (j-center[1])**2)
                kernel[i,j] = 1 - min(1, dist/(np.sqrt(h**2 + w**2)/2))
        kernel = np.dstack([kernel]*3)
        return cv2.multiply(sepia, kernel).astype(np.uint8)
    
    elif filter_type == "Increase Brightness":
        # Improved brightness boost
        hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
        # Increase brightness
        hsv[:,:,2] = cv2.convertScaleAbs(hsv[:,:,2], alpha=1.2, beta=30)
        # Also increase the contrast slightly
        return cv2.convertScaleAbs(cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR), alpha=1.1, beta=0)

def image_processing(image, filter_type):
    """Main image processing function"""
    if image is None:
        return None
    
    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    
    # Process by filter categories
    basic_filter_list = ["Gray Toning", "Sepia", "X-ray", "Burn it"]
    classic_filter_list = ["Charcoal Effect", "Sharpen", "Embossing", "Edge Detection"]
    creative_filters_list = ["Rainbow", "Night Vision"]
    special_effects_list = ["Matrix Effect", "Wave Effect", "Time Stamp", "Glitch Effect"]
    artistic_filters_list = ["Pop Art", "Oil Paint", "Cartoon"]
    atmospheric_filters_list = ["Autumn", "Increase Brightness"]
    
    if filter_type in basic_filter_list:
        output = basic_filter(image, filter_type)
    elif filter_type in classic_filter_list:
        output = classic_filter(image, filter_type)
    elif filter_type in creative_filters_list:
        output = creative_filters(image, filter_type)
    elif filter_type in special_effects_list:
        output = special_effects(image, filter_type)
    elif filter_type in artistic_filters_list:
        output = artistic_filters(image, filter_type)
    elif filter_type in atmospheric_filters_list:
        output = atmospheric_filters(image, filter_type)
    else:
        output = image
        
    return cv2.cvtColor(output, cv2.COLOR_BGR2RGB) if len(output.shape) == 3 else output
    

css = """
#app-container {
    max-width: 1200px;
    margin-left: auto;
    margin-right: auto;
}
"""

# Gradio interface
with gr.Blocks(theme=theme, css=css) as app:
    gr.HTML("<center><h6>🎨 Image Studio</h6></center>")
    with gr.Tab("Flip Image"):
                with gr.Row():
                    image_input = gr.Image(type="numpy", label="Upload Image")
                    image_output = gr.Image(format="png")
                with gr.Row():    
                    image_button = gr.Button("Run", variant='primary')
                    image_button.click(flip_image, inputs=image_input, outputs=image_output)
    with gr.Tab("Image Filters"):    
        with gr.Row():
            with gr.Column():
                image_input = gr.Image(type="numpy", label="Upload Image")
                with gr.Accordion("ℹ️ Filter Categories", open=True):
                    filter_type = gr.Dropdown(
                        [
                            # Basic Filters
                            "Gray Toning", "Sepia", "X-ray", "Burn it",
                            # Classic Filter 
                            "Charcoal Effect", "Sharpen", "Embossing", "Edge Detection",                       
                            # Creative Filters
                            "Rainbow", "Night Vision",
                            # Special Effects
                            "Matrix Effect", "Wave Effect", "Time Stamp", "Glitch Effect",
                            # Artistic Filters
                            "Pop Art", "Oil Paint", "Cartoon",
                            # Atmospheric Filters
                            "Autumn", "Increase Brightness"
                        ],
                        label="🎭 Select Filter",
                        info="Choose the effect you want"
                    )
                submit_button = gr.Button("✨ Apply Filter", variant="primary")
    
            with gr.Column():
                image_output = gr.Image(label="🖼️ Filtered Image")
                
            submit_button.click(
                image_processing,
                inputs=[image_input, filter_type],
                outputs=image_output
            )
    with gr.Tab("Text to Image"):
 
       # gr.load("models/digiplay/AnalogMadness-realistic-model-v7")
        pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev")
        pipe.load_lora_weights("XLabs-AI/flux-RealismLora")

        prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
        image = pipe(prompt).images[0]
    
app.launch(share=True)