Update app.py
Browse files
app.py
CHANGED
@@ -1,2 +1,104 @@
|
|
1 |
-
import
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image, ImageEnhance
|
5 |
+
from gradio_imageslider import ImageSlider
|
6 |
+
|
7 |
+
def apply_filter(image, filter_type, intensity):
|
8 |
+
image = np.array(image)
|
9 |
+
normalized_intensity = intensity / 100.0
|
10 |
+
|
11 |
+
if filter_type == "Grayscale":
|
12 |
+
return convert_to_grayscale(image)
|
13 |
+
elif filter_type == "Soft Glow":
|
14 |
+
base_intensity = 0.1
|
15 |
+
adjusted_intensity = base_intensity + (normalized_intensity * (1 - base_intensity))
|
16 |
+
gaussian = cv2.GaussianBlur(image, (15, 15), 0)
|
17 |
+
soft_glow = cv2.addWeighted(image, 1 - adjusted_intensity, gaussian, adjusted_intensity, 0)
|
18 |
+
return soft_glow
|
19 |
+
elif filter_type == "Portrait Enhancer":
|
20 |
+
base_intensity = 0.5
|
21 |
+
adjusted_intensity = base_intensity + (normalized_intensity * (1 - base_intensity))
|
22 |
+
image_pil = Image.fromarray(image)
|
23 |
+
enhancer = ImageEnhance.Sharpness(image_pil)
|
24 |
+
image_pil = enhancer.enhance(1 + 0.5 * adjusted_intensity)
|
25 |
+
enhancer = ImageEnhance.Color(image_pil)
|
26 |
+
image_pil = enhancer.enhance(1 + 0.5 * adjusted_intensity)
|
27 |
+
enhanced_image = np.array(image_pil)
|
28 |
+
return enhanced_image
|
29 |
+
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)
|
31 |
+
return warm_image
|
32 |
+
elif filter_type == "Cold Tone":
|
33 |
+
cold_image = cv2.addWeighted(image, 1.0, np.full(image.shape, (112, 66, 20), dtype=np.uint8), 0.3 * normalized_intensity, 0)
|
34 |
+
return cold_image
|
35 |
+
elif filter_type == "High-Key":
|
36 |
+
high_key = cv2.convertScaleAbs(image, alpha=1.0 + 0.3 * normalized_intensity, beta=20)
|
37 |
+
return high_key
|
38 |
+
elif filter_type == "Low-Key":
|
39 |
+
low_key = cv2.convertScaleAbs(image, alpha=1.0 - 0.1 * normalized_intensity, beta=-10)
|
40 |
+
return low_key
|
41 |
+
elif filter_type == "Haze":
|
42 |
+
haze = cv2.addWeighted(image, 1.0, np.full(image.shape, 255, dtype=np.uint8), 0.3 * normalized_intensity, 0)
|
43 |
+
return haze
|
44 |
+
else:
|
45 |
+
return image
|
46 |
+
|
47 |
+
def convert_to_grayscale(image):
|
48 |
+
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
49 |
+
return cv2.cvtColor(gray_image, cv2.COLOR_GRAY2BGR)
|
50 |
+
|
51 |
+
def convert_and_save(image, filter_type, intensity):
|
52 |
+
image_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
53 |
+
filtered_image = apply_filter(image_cv, filter_type, intensity)
|
54 |
+
|
55 |
+
original_image_pil = Image.fromarray(cv2.cvtColor(image_cv, cv2.COLOR_BGR2RGB))
|
56 |
+
filtered_image_pil = Image.fromarray(cv2.cvtColor(filtered_image, cv2.COLOR_BGR2RGB))
|
57 |
+
|
58 |
+
output_path = "filtered_image.jpg"
|
59 |
+
filtered_image_pil.save(output_path)
|
60 |
+
|
61 |
+
return [original_image_pil, filtered_image_pil], output_path
|
62 |
+
|
63 |
+
def get_filter_description(filter_type):
|
64 |
+
descriptions = {
|
65 |
+
"Grayscale": "์ด๋ฏธ์ง๋ฅผ ํ๋ฐฑ์ผ๋ก ๋ณํํฉ๋๋ค.",
|
66 |
+
"Soft Glow": "๋ถ๋๋ฌ์ด ๋น์ ์ถ๊ฐํ์ฌ ์ด๋ฏธ์ง๋ฅผ ์์ํ๊ฒ ๋ง๋ญ๋๋ค.",
|
67 |
+
"Portrait Enhancer": "ํผ๋ถ ํค์ ๊ท ์ผํ๊ฒ ํ๊ณ ์ ๋ช
๋๋ฅผ ์กฐ์ ํ์ฌ ์ธ๋ฌผ์ ๋์ฑ ๋๋ณด์ด๊ฒ ๋ง๋ญ๋๋ค.",
|
68 |
+
"Warm Tone": "๋ฐ๋ปํ ์์กฐ๋ฅผ ์ถ๊ฐํ์ฌ ์ด๋ฏธ์ง์ ์จ๊ธฐ๋ฅผ ๋ํฉ๋๋ค.",
|
69 |
+
"Cold Tone": "์ฐจ๊ฐ์ด ์์กฐ๋ฅผ ์ถ๊ฐํ์ฌ ์ด๋ฏธ์ง์ ์์ํจ์ ๋ํฉ๋๋ค.",
|
70 |
+
"High-Key": "๋ฐ๊ณ ํ์ฌํ ์ด๋ฏธ์ง๋ฅผ ๋ง๋ค์ด๋
๋๋ค.",
|
71 |
+
"Low-Key": "์ด๋์ด ํค์ ๊ฐ์กฐํ์ฌ ๋ถ์๊ธฐ ์๋ ์ด๋ฏธ์ง๋ฅผ ๋ง๋ญ๋๋ค.",
|
72 |
+
"Haze": "๋ถ๋๋ฝ๊ณ ํ๋ฆฟํ ํจ๊ณผ๋ฅผ ์ถ๊ฐํ์ฌ ๋ชฝํ์ ์ธ ์ด๋ฏธ์ง๋ฅผ ๋ง๋ญ๋๋ค."
|
73 |
+
}
|
74 |
+
return descriptions.get(filter_type, "")
|
75 |
+
|
76 |
+
with gr.Blocks() as iface:
|
77 |
+
with gr.Row():
|
78 |
+
with gr.Column():
|
79 |
+
image_input = gr.Image(type="pil", label="์ด๋ฏธ์ง ์
๋ก๋")
|
80 |
+
filter_input = gr.Radio(
|
81 |
+
["Grayscale", "Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone", "High-Key", "Low-Key", "Haze"],
|
82 |
+
label="ํํฐ ์ ํ",
|
83 |
+
value="Soft Glow"
|
84 |
+
)
|
85 |
+
intensity_slider = gr.Slider(1, 100, value=50, label="ํํฐ ๊ฐ๋")
|
86 |
+
description_output = gr.Markdown(get_filter_description("Soft Glow"))
|
87 |
+
|
88 |
+
with gr.Column():
|
89 |
+
slider_output = ImageSlider(label="Before and After", type="pil")
|
90 |
+
download_link = gr.File(label="Download Filtered Image")
|
91 |
+
|
92 |
+
filter_input.change(fn=get_filter_description, inputs=filter_input, outputs=description_output)
|
93 |
+
|
94 |
+
process_button = gr.Button("ํํฐ ์ ์ฉ")
|
95 |
+
process_button.click(
|
96 |
+
fn=convert_and_save,
|
97 |
+
inputs=[image_input, filter_input, intensity_slider],
|
98 |
+
outputs=[slider_output, download_link]
|
99 |
+
)
|
100 |
+
|
101 |
+
iface.title = "์ธ๋ฌผ ์ฌ์ง์ ์ต์ ํ๋ ํํฐ"
|
102 |
+
iface.description = "์ธ๋ฌผ ์ฌ์ง์ ์ต์ ํ๋ ๋ค์ํ ํํฐ๋ฅผ ์ ์ฉํ ์ ์์ต๋๋ค."
|
103 |
+
|
104 |
+
iface.launch()
|