File size: 5,373 Bytes
ec0dbf0
16f9a51
ec0dbf0
26ae524
27e15cf
ec0dbf0
16f9a51
27e15cf
 
 
16f9a51
 
b99b09d
16f9a51
 
 
8a91ef2
0ce6512
 
16f9a51
0ce6512
16f9a51
 
8a91ef2
26ae524
 
24cb82a
16f9a51
 
24cb82a
16f9a51
 
24cb82a
16f9a51
24cb82a
16f9a51
 
223546c
 
24cb82a
16f9a51
223546c
 
24cb82a
16f9a51
8621860
 
ec0dbf0
16f9a51
223546c
 
ec0dbf0
16f9a51
8621860
 
ec0dbf0
16f9a51
 
ec0dbf0
58f5984
16f9a51
24cb82a
58f5984
0bee58f
24cb82a
 
83aa54c
24cb82a
 
 
83aa54c
c52ffb7
 
 
 
 
ec0dbf0
4e7b811
 
 
 
 
 
 
 
 
 
 
 
 
7c01b96
 
 
 
 
 
 
 
 
 
 
ec0dbf0
7c01b96
27e15cf
c52ffb7
27e15cf
 
4e7b811
7c01b96
27e15cf
 
 
c52ffb7
27e15cf
 
 
 
4e7b811
ec0dbf0
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
import gradio as gr
import cv2
import numpy as np
from PIL import Image, ImageEnhance
from gradio_imageslider import ImageSlider

def apply_filter(image, filter_type, intensity):
    # PIL ์ด๋ฏธ์ง€๋ฅผ numpy array๋กœ ๋ณ€ํ™˜
    image = np.array(image)

    # ๊ฐ•๋„๋ฅผ 0.0์—์„œ 1.0 ์‚ฌ์ด๋กœ ์ •๊ทœํ™”
    normalized_intensity = intensity / 100.0

    if filter_type == "Grayscale":
        return convert_to_grayscale(image)
    elif filter_type == "Soft Glow":
        base_intensity = 0.1
        adjusted_intensity = base_intensity + (normalized_intensity * (1 - base_intensity))
        
        gaussian = cv2.GaussianBlur(image, (15, 15), 0)
        soft_glow = cv2.addWeighted(image, 1 - adjusted_intensity, gaussian, adjusted_intensity, 0)
        return soft_glow
    elif filter_type == "Portrait Enhancer":
        base_intensity = 0.5
        adjusted_intensity = base_intensity + (normalized_intensity * (1 - base_intensity))
        
        image_pil = Image.fromarray(image)
        
        enhancer = ImageEnhance.Sharpness(image_pil)
        image_pil = enhancer.enhance(1 + 0.5 * adjusted_intensity)
        
        enhancer = ImageEnhance.Color(image_pil)
        image_pil = enhancer.enhance(1 + 0.5 * adjusted_intensity)
        
        enhanced_image = np.array(image_pil)
        return enhanced_image
    elif filter_type == "Warm Tone":
        # ๊ฐ•๋„๋ฅผ 30%๋กœ ์„ค์ • (๋”ฐ๋œปํ•œ ํ†ค ์ ์šฉ)
        warm_image = cv2.addWeighted(image, 1.0, np.full(image.shape, (20, 66, 112), dtype=np.uint8), 0.3 * normalized_intensity, 0)
        return warm_image
    elif filter_type == "Cold Tone":
        # ๊ฐ•๋„๋ฅผ 30%๋กœ ์„ค์ • (์ฐจ๊ฐ€์šด ํ†ค ์ ์šฉ)
        cold_image = cv2.addWeighted(image, 1.0, np.full(image.shape, (112, 66, 20), dtype=np.uint8), 0.3 * normalized_intensity, 0)
        return cold_image
    elif filter_type == "High-Key":
        # ๊ฐ•๋„๋ฅผ 30%๋กœ ์„ค์ •
        high_key = cv2.convertScaleAbs(image, alpha=1.0 + 0.3 * normalized_intensity, beta=20)
        return high_key
    elif filter_type == "Low-Key":
        # ๊ฐ•๋„๋ฅผ 10%๋กœ ์„ค์ •
        low_key = cv2.convertScaleAbs(image, alpha=1.0 - 0.1 * normalized_intensity, beta=-10)
        return low_key
    elif filter_type == "Haze":
        # ๊ฐ•๋„๋ฅผ 30%๋กœ ์„ค์ •
        haze = cv2.addWeighted(image, 1.0, np.full(image.shape, 255, dtype=np.uint8), 0.3 * normalized_intensity, 0)
        return haze
    else:
        return image

def convert_to_grayscale(image):
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    return cv2.cvtColor(gray_image, cv2.COLOR_GRAY2BGR)

def convert_and_save(image, filter_type, intensity):
    image_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
    filtered_image = apply_filter(image_cv, filter_type, intensity)
    
    # OpenCV ์ด๋ฏธ์ง€๋ฅผ ๋‹ค์‹œ PIL๋กœ ๋ณ€ํ™˜
    original_image_pil = Image.fromarray(cv2.cvtColor(image_cv, cv2.COLOR_BGR2RGB))
    filtered_image_pil = Image.fromarray(cv2.cvtColor(filtered_image, cv2.COLOR_BGR2RGB))

    # ํ•„ํ„ฐ ์ ์šฉ๋œ ์ด๋ฏธ์ง€๋ฅผ JPG๋กœ ์ €์žฅ
    output_path = "filtered_image.jpg"
    filtered_image_pil.save(output_path)

    return original_image_pil, filtered_image_pil, output_path  # ์›๋ณธ, ํ•„ํ„ฐ ์ ์šฉ๋œ ์ด๋ฏธ์ง€์™€ ํŒŒ์ผ ๊ฒฝ๋กœ ๋ฐ˜ํ™˜

def get_filter_description(filter_type):
    descriptions = {
        "Grayscale": "์ด๋ฏธ์ง€๋ฅผ ํ‘๋ฐฑ์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.",
        "Soft Glow": "๋ถ€๋“œ๋Ÿฌ์šด ๋น›์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ์€์€ํ•˜๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค.",
        "Portrait Enhancer": "ํ”ผ๋ถ€ ํ†ค์„ ๊ท ์ผํ•˜๊ฒŒ ํ•˜๊ณ  ์„ ๋ช…๋„๋ฅผ ์กฐ์ ˆํ•˜์—ฌ ์ธ๋ฌผ์„ ๋”์šฑ ๋‹๋ณด์ด๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค.",
        "Warm Tone": "๋”ฐ๋œปํ•œ ์ƒ‰์กฐ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์ด๋ฏธ์ง€์— ์˜จ๊ธฐ๋ฅผ ๋”ํ•ฉ๋‹ˆ๋‹ค.",
        "Cold Tone": "์ฐจ๊ฐ€์šด ์ƒ‰์กฐ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์ด๋ฏธ์ง€์— ์‹œ์›ํ•จ์„ ๋”ํ•ฉ๋‹ˆ๋‹ค.",
        "High-Key": "๋ฐ๊ณ  ํ™”์‚ฌํ•œ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค์–ด๋ƒ…๋‹ˆ๋‹ค.",
        "Low-Key": "์–ด๋‘์šด ํ†ค์„ ๊ฐ•์กฐํ•˜์—ฌ ๋ถ„์œ„๊ธฐ ์žˆ๋Š” ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.",
        "Haze": "๋ถ€๋“œ๋Ÿฝ๊ณ  ํ๋ฆฟํ•œ ํšจ๊ณผ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ชฝํ™˜์ ์ธ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค."
    }
    return descriptions.get(filter_type, "")

with gr.Blocks() as iface:
    with gr.Row():
        with gr.Column():
            image_input = gr.Image(type="pil", label="์ด๋ฏธ์ง€ ์—…๋กœ๋“œ")
            filter_input = gr.Radio(
                ["Grayscale", "Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone", "High-Key", "Low-Key", "Haze"], 
                label="ํ•„ํ„ฐ ์„ ํƒ",
                value="Soft Glow"
            )
            intensity_slider = gr.Slider(1, 100, value=50, label="ํ•„ํ„ฐ ๊ฐ•๋„")
            description_output = gr.Markdown(get_filter_description("Soft Glow"))

        with gr.Column():
            slider_output = ImageSlider(label="Before and After", type="pil")
            download_link = gr.File(label="Download Filtered Image")

    filter_input.change(fn=get_filter_description, inputs=filter_input, outputs=description_output)

    process_button = gr.Button("ํ•„ํ„ฐ ์ ์šฉ")
    process_button.click(
        fn=convert_and_save,
        inputs=[image_input, filter_input, intensity_slider],
        outputs=[slider_output, download_link]
    )

    iface.title = "์ธ๋ฌผ ์‚ฌ์ง„์— ์ตœ์ ํ™”๋œ ํ•„ํ„ฐ"
    iface.description = "์ธ๋ฌผ ์‚ฌ์ง„์— ์ตœ์ ํ™”๋œ ๋‹ค์–‘ํ•œ ํ•„ํ„ฐ๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."

iface.launch()