File size: 8,939 Bytes
bc5c57f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2 as cv
import numpy as np
import gradio as gr

def filters(image, filter_type, Brightness, Contrast, Saturation, Hue_Shift, Gamma, Blur_Level):
    image = np.array(image, dtype=np.float32) / 255.0  # Normalize the image
    # TR = Görüntüyü 0-1 arasında normalize eder
    # EN = Normalizes the image to a range of 0 to 1

    # Brightness adjustment
    image = np.clip(image * Brightness, 0, 1)
    # TR = Parlaklık değerini ayarlar
    # EN = Adjusts the brightness value

    # Contrast adjustment
    image = np.clip((image - 0.5) * Contrast + 0.5, 0, 1)
    # TR = Kontrast değerini ayarlar
    # EN = Adjusts the contrast value

    # Saturation adjustment (applies only to BGR images)
    hsv_image = cv.cvtColor(image, cv.COLOR_BGR2HSV)
    hsv_image[..., 1] = np.clip(hsv_image[..., 1] * Saturation, 0, 1)
    image = cv.cvtColor(hsv_image, cv.COLOR_HSV2BGR)
    # TR = Doygunluk seviyesini ayarlar (sadece BGR görüntülerine uygulanır)
    # EN = Adjusts the saturation level (applies only to BGR images)

    # Hue shift
    hsv_image[..., 0] = (hsv_image[..., 0] + Hue_Shift) % 180
    image = cv.cvtColor(hsv_image, cv.COLOR_HSV2BGR)
    # TR = Renk tonunu kaydırır
    # EN = Shifts the hue of the image

    # Gamma adjustment
    image = np.clip(image ** (1.0 / Gamma), 0, 1)
    # TR = Gamma ayarını yapar
    # EN = Adjusts the gamma of the image

    # Blur level
    if Blur_Level > 0:
        ksize = int(Blur_Level * 2 + 1)
        image = cv.GaussianBlur(image, (ksize, ksize), 0)
    # TR = Bulanıklık seviyesini ayarlar
    # EN = Adjusts the blur level of the image

    image = (image * 255).astype(np.uint8)  # Convert to uint8 format
    # TR = Görüntüyü uint8 formatına dönüştürür
    # EN = Converts the image back to uint8 format

    if filter_type == "Gray Scale":
        filtered_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    elif filter_type == "Sepia":
        filtered_image = cv.applyColorMap(cv.cvtColor(image, cv.COLOR_BGR2GRAY), cv.COLORMAP_AUTUMN)
    elif filter_type == "X-Ray":
        filtered_image = cv.bitwise_not(image)
    elif filter_type == "Blur":
        filtered_image = cv.GaussianBlur(image, (15, 15), 0)
    elif filter_type == "Sketch":
        gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
        filtered_image = cv.divide(gray, cv.GaussianBlur(gray, (21, 21), 0), scale=256)
    elif filter_type == "Sharpen":
        kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
        filtered_image = cv.filter2D(image, -1, kernel)
    elif filter_type == "Emboss":
        kernel = np.array([[-2, -1, 0], [-1, 1, 1], [0, 1, 2]])
        filtered_image = cv.filter2D(image, -1, kernel)
    elif filter_type == "Edge Detection":
        filtered_image = cv.Canny(image, 100, 200)
    elif filter_type == "Pixel Art":
        small = cv.resize(image, (32, 32), interpolation=cv.INTER_LINEAR)
        filtered_image = cv.resize(small, image.shape[:2][::-1], interpolation=cv.INTER_NEAREST)
    elif filter_type == "Mosaic":
        small = cv.resize(image, (16, 16), interpolation=cv.INTER_LINEAR)
        filtered_image = cv.resize(small, image.shape[:2][::-1], interpolation=cv.INTER_NEAREST)
    elif filter_type == "Rainbow":
        filtered_image = cv.applyColorMap(image, cv.COLORMAP_JET)
    elif filter_type == "Night Vision":
        filtered_image = cv.applyColorMap(cv.cvtColor(image, cv.COLOR_BGR2GRAY), cv.COLORMAP_OCEAN)
    elif filter_type == "Matrix Effect":
        filtered_image = image.copy()
    elif filter_type == "Wave Effect":
        filtered_image = cv.GaussianBlur(image, (21, 21), 0)
    elif filter_type == "Timestamp":
        filtered_image = image.copy()
    elif filter_type == "Glitch Effect":
        filtered_image = image.copy()
    elif filter_type == "Pop Art":
        filtered_image = cv.cvtColor(image, cv.COLOR_BGR2HSV)
    elif filter_type == "Oil Paint":
        filtered_image = cv.GaussianBlur(image, (21, 21), 0)
    elif filter_type == "Texture":
        filtered_image = cv.GaussianBlur(image, (21, 21), 0)
    else:
        filtered_image = image  # Default to the original image
    # TR = Farklı filtre türlerine göre filtreyi uygular
    # EN = Applies the selected filter based on the filter type

    return filtered_image

# Interface setup
with gr.Blocks() as demo:
    gr.Markdown("**Live Color Filters**")
    # TR = Arayüz başlığını belirler
    # EN = Sets the title of the interface

    filter_dropdown = gr.Dropdown(
        ["Gray Scale", "Sepia", "X-Ray", "Blur", 
         "Sketch", "Sharpen", "Emboss", "Edge Detection",
         "Pixel Art", "Mosaic", "Rainbow", "Night Vision",
         "Matrix Effect", "Wave Effect", "Timestamp", "Glitch Effect",
         "Pop Art", "Oil Paint", "Texture"],
        label="Select Filter",
    )
    # TR = Kullanıcıya filtre türü seçme imkanı tanır
    # EN = Provides a dropdown for the user to select a filter type

    Brightness = gr.Slider(minimum=0.5, maximum=5.0, value=1.0, label="Brightness")
    Contrast = gr.Slider(minimum=-100, maximum=100, value=50, label="Contrast")
    Saturation = gr.Slider(minimum=0.0, maximum=5.0, value=1.0, label="Saturation")
    Hue_Shift = gr.Slider(minimum=-100, maximum=100, value=0, label="Hue Shift")
    Gamma = gr.Slider(minimum=0, maximum=5, value=1.0, label="Gamma")
    Blur_Level = gr.Slider(minimum=0, maximum=10, value=1, label="Blur Level")
    # TR = Kullanıcıya görsel düzenlemeleri yapma seçenekleri tanır
    # EN = Provides sliders for adjusting image properties

    image_input = gr.Image(type='pil', label='Resmi Yükle')
    image_output = gr.Image(type='numpy', label='Filtrelenmiş Resim')
    # TR = Görsel yükleme ve çıktıyı görüntüleme seçenekleri sunar
    # EN = Provides options for uploading the image and displaying the output

    btn = gr.Button("Filtreyi Uygula")
    btn.click(fn=filters, inputs=[image_input, filter_dropdown, Brightness, Contrast, Saturation, Hue_Shift, Gamma, Blur_Level], outputs=image_output)
    # TR = Filtreyi uygulamak için bir buton ekler
    # EN = Adds a button to apply the selected filter

    with gr.Accordion('Filtrelerin Açıklanması',open=False):
        # Açıklama kısmı
        gr.Markdown("""

                    **Temel Filtreler**  

                    - **Gri Tonlama**: Görüntüyü siyah-beyaz tonlarında, klasik bir tarzda dönüştürür  

                    - **Sepya**: Fotoğrafa eski bir fotoğraf havası veren, sıcak kahverengi tonları ekler  

                    - **Röntgen**: Görüntüye ters ışıklandırma ekleyerek X-ray taraması etkisi oluşturur  

                    - **Bulanıklaştır**: Görüntüde yumuşak bir bulanıklık oluşturarak detayları azaltır.



                    **Klasik Filtreler Listesi**  

                    - **Karakalem Efekti**: Görüntüyü karakalem çizimi gibi gösterir  

                    - **Keskinleştir**: Görüntüdeki detayları belirginleştirir  

                    - **Kabartma**: Görüntüye kabartma ve derinlik efekti ekler  

                    - **Kenar Algılama**: Görüntüdeki kenar hatlarını vurgular  



                    **Yaratıcı Filtreler**  

                    - **Piksel Sanatı**: Görüntüyü retro piksel tarzında küçük karelere böler  

                    - **Mozaik Efekti**: Fotoğrafı küçük mozaik parçalarına böler  

                    - **Gökkuşağı**: Görüntüye renkli gökkuşağı efektleri ekler  

                    - **Gece Görüşü**: Gece görüş cihazı efektini simüle eder  



                    **Özel Efektler**  

                    - **Matrix Efekti**: Matrix film efekti  

                    - **Dalga Efekti**: Görüntüye su dalgası benzeri kıvrımlı bir bozulma ekler, dalgalanma hissi yaratır  

                    - **Zaman Damgası**: Fotoğrafın üzerine çekildiği tarih ve saati ekleyerek nostaljik bir hava verir  

                    - **Glitch Efekti**: Dijital bozulmalar ekleyerek, fotoğrafa retro tarzda bir hata efekti katar 



                    **Sanatsal Filtreler**  

                    - **Pop Art**: Canlı ve kontrastlı renklerle, Andy Warhol tarzı ikonik pop-art efekti oluşturur  

                    - **Yağlı Boya**: Fırça darbelerini simüle ederek görüntüye yağlı boya tablosu görünümü kazandırır  

                    - **Doku Efekti**: Görüntüye yüzey dokusu ekleyerek dokunsal bir derinlik ve sanat eseri havası verir  

                    """)
    # TR = Filtre açıklamalarını ekler
    # EN = Adds filter descriptions to the interface

if __name__ == "__main__":
    demo.launch(share=True)
    # TR = Uygulamayı başlatır
    # EN = Launches the application