Update app.py
Browse files
app.py
CHANGED
@@ -1,76 +1,70 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
import numpy as np
|
4 |
|
5 |
-
def apply_filter(image,
|
6 |
-
|
7 |
-
|
8 |
-
enhanced_image = Image.blend(image, glow, 0.5)
|
9 |
-
return enhanced_image
|
10 |
-
|
11 |
-
elif filter_name == "Portrait Enhancer":
|
12 |
-
enhancer = ImageEnhance.Sharpness(image)
|
13 |
-
image = enhancer.enhance(1 + 0.1 * intensity)
|
14 |
-
enhancer = ImageEnhance.Color(image)
|
15 |
-
image = enhancer.enhance(1 + 0.1 * intensity)
|
16 |
-
return image
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
enhancer = ImageEnhance.
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
return high_key
|
35 |
-
|
36 |
-
|
37 |
-
enhancer = ImageEnhance.Contrast(image)
|
38 |
-
low_key = enhancer.enhance(1 + 0.1 * intensity)
|
39 |
-
enhancer = ImageEnhance.Brightness(low_key)
|
40 |
-
low_key = enhancer.enhance(1 - 0.05 * intensity)
|
41 |
return low_key
|
42 |
-
|
43 |
-
|
44 |
-
haze = image.filter(ImageFilter.GaussianBlur(2 * intensity))
|
45 |
-
enhancer = ImageEnhance.Brightness(haze)
|
46 |
-
haze = enhancer.enhance(1 + 0.05 * intensity)
|
47 |
return haze
|
48 |
-
|
49 |
-
|
50 |
-
mono = image.convert("L")
|
51 |
-
mono = ImageOps.colorize(mono, black="black", white="white")
|
52 |
-
enhancer = ImageEnhance.Contrast(mono)
|
53 |
-
mono = enhancer.enhance(1 + 0.1 * intensity)
|
54 |
-
return mono
|
55 |
-
|
56 |
-
return image
|
57 |
|
58 |
def convert_to_grayscale(image):
|
59 |
-
gray_image =
|
60 |
return gray_image
|
61 |
|
62 |
def convert_and_save(image, filter_type, intensity):
|
63 |
filtered_image = apply_filter(image, filter_type, intensity)
|
64 |
output_path = "output.jpg"
|
65 |
-
|
66 |
return filtered_image, output_path
|
67 |
|
68 |
iface = gr.Interface(
|
69 |
fn=convert_and_save,
|
70 |
inputs=[
|
71 |
-
|
72 |
gr.Radio(
|
73 |
-
["Grayscale", "Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone", "High-Key", "Low-Key", "Haze"
|
74 |
label="ํํฐ ์ ํ"
|
75 |
),
|
76 |
gr.Slider(minimum=1, maximum=100, value=50, label="ํํฐ ๊ฐ๋")
|
|
|
1 |
import gradio as gr
|
2 |
+
import cv2
|
3 |
import numpy as np
|
4 |
|
5 |
+
def apply_filter(image, filter_type, intensity):
|
6 |
+
# ๊ฐ๋๋ฅผ 0.0์์ 1.0 ์ฌ์ด๋ก ์ ๊ทํ
|
7 |
+
normalized_intensity = intensity / 100.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
if filter_type == "Grayscale":
|
10 |
+
return convert_to_grayscale(image)
|
11 |
+
elif filter_type == "Soft Glow":
|
12 |
+
gaussian = cv2.GaussianBlur(image, (15, 15), 0)
|
13 |
+
soft_glow = cv2.addWeighted(image, 1 - normalized_intensity, gaussian, normalized_intensity, 0)
|
14 |
+
return soft_glow
|
15 |
+
elif filter_type == "Portrait Enhancer":
|
16 |
+
# ์ฐธ์กฐ ์ฝ๋์ ๋ฐ๋ผ PIL์ ์ฌ์ฉํ์ฌ ํผ๋ถ ํค์ ๊ท ์ผํ๊ฒ ํ๊ณ ์ ๋ช
๋๋ฅผ ์กฐ์
|
17 |
+
from PIL import Image, ImageEnhance
|
18 |
+
image_pil = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
19 |
+
|
20 |
+
enhancer = ImageEnhance.Sharpness(image_pil)
|
21 |
+
image_pil = enhancer.enhance(1 + 0.1 * intensity)
|
22 |
+
|
23 |
+
enhancer = ImageEnhance.Color(image_pil)
|
24 |
+
image_pil = enhancer.enhance(1 + 0.1 * intensity)
|
25 |
+
|
26 |
+
enhanced_image = cv2.cvtColor(np.array(image_pil), cv2.COLOR_RGB2BGR)
|
27 |
+
return enhanced_image
|
28 |
+
elif filter_type == "Warm Tone":
|
29 |
+
increase_red = np.array([[1.0 + 0.2 * normalized_intensity, 0.0, 0.0],
|
30 |
+
[0.0, 1.0, 0.0],
|
31 |
+
[0.0, 0.0, 1.0 - 0.2 * normalized_intensity]])
|
32 |
+
warm_image = cv2.transform(image, increase_red)
|
33 |
+
return warm_image
|
34 |
+
elif filter_type == "Cold Tone":
|
35 |
+
increase_blue = np.array([[1.0 - 0.2 * normalized_intensity, 0.0, 0.0],
|
36 |
+
[0.0, 1.0, 0.0],
|
37 |
+
[0.0, 0.0, 1.0 + 0.2 * normalized_intensity]])
|
38 |
+
cold_image = cv2.transform(image, increase_blue)
|
39 |
+
return cold_image
|
40 |
+
elif filter_type == "High-Key":
|
41 |
+
high_key = cv2.convertScaleAbs(image, alpha=1.0 + 0.2 * normalized_intensity, beta=30)
|
42 |
return high_key
|
43 |
+
elif filter_type == "Low-Key":
|
44 |
+
low_key = cv2.convertScaleAbs(image, alpha=1.0 - 0.3 * normalized_intensity, beta=-30)
|
|
|
|
|
|
|
|
|
45 |
return low_key
|
46 |
+
elif filter_type == "Haze":
|
47 |
+
haze = cv2.addWeighted(image, 1.0 - 0.3 * normalized_intensity, np.full(image.shape, 255, dtype=np.uint8), 0.3 * normalized_intensity, 0)
|
|
|
|
|
|
|
48 |
return haze
|
49 |
+
else:
|
50 |
+
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
def convert_to_grayscale(image):
|
53 |
+
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
54 |
return gray_image
|
55 |
|
56 |
def convert_and_save(image, filter_type, intensity):
|
57 |
filtered_image = apply_filter(image, filter_type, intensity)
|
58 |
output_path = "output.jpg"
|
59 |
+
cv2.imwrite(output_path, filtered_image)
|
60 |
return filtered_image, output_path
|
61 |
|
62 |
iface = gr.Interface(
|
63 |
fn=convert_and_save,
|
64 |
inputs=[
|
65 |
+
"image",
|
66 |
gr.Radio(
|
67 |
+
["Grayscale", "Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone", "High-Key", "Low-Key", "Haze"],
|
68 |
label="ํํฐ ์ ํ"
|
69 |
),
|
70 |
gr.Slider(minimum=1, maximum=100, value=50, label="ํํฐ ๊ฐ๋")
|