Kims12's picture
Update app.py
7c01b96 verified
raw
history blame
4.84 kB
import gradio as gr
import cv2
import numpy as np
from PIL import Image, ImageEnhance
def apply_filter(image, filter_type, intensity):
# ๊ฐ•๋„๋ฅผ 0.0์—์„œ 1.0 ์‚ฌ์ด๋กœ ์ •๊ทœํ™”
normalized_intensity = intensity / 100.0
if filter_type == "Grayscale":
return convert_to_grayscale(image)
elif filter_type == "Soft Glow":
# ๊ธฐ๋ณธ 10% ๊ฐ•๋„์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ์ตœ๋Œ€ 100% ๊ฐ•๋„๊นŒ์ง€ ์กฐ์ ˆ
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":
# ๊ธฐ๋ณธ 50% ๊ฐ•๋„์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ์ตœ๋Œ€ 100% ๊ฐ•๋„๊นŒ์ง€ ์กฐ์ ˆ
base_intensity = 0.5
adjusted_intensity = base_intensity + (normalized_intensity * (1 - base_intensity))
image_pil = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
enhancer = ImageEnhance.Sharpness(image_pil)
image_pil = enhancer.enhance(1 + 0.1 * adjusted_intensity)
enhancer = ImageEnhance.Color(image_pil)
image_pil = enhancer.enhance(1 + 0.1 * adjusted_intensity)
enhanced_image = cv2.cvtColor(np.array(image_pil), cv2.COLOR_RGB2BGR)
return enhanced_image
elif filter_type == "Warm Tone":
increase_red = np.array([[1.0 + 0.2 * normalized_intensity, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0 - 0.2 * normalized_intensity]])
warm_image = cv2.transform(image, increase_red)
return warm_image
elif filter_type == "Cold Tone":
increase_blue = np.array([[1.0 - 0.2 * normalized_intensity, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0 + 0.2 * normalized_intensity]])
cold_image = cv2.transform(image, increase_blue)
return cold_image
elif filter_type == "High-Key":
high_key = cv2.convertScaleAbs(image, alpha=1.0 + 0.2 * normalized_intensity, beta=30)
return high_key
elif filter_type == "Low-Key":
low_key = cv2.convertScaleAbs(image, alpha=1.0 - 0.3 * normalized_intensity, beta=-30)
return low_key
elif filter_type == "Haze":
haze = cv2.addWeighted(image, 1.0 - 0.3 * normalized_intensity, 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 gray_image
def convert_and_save(image, filter_type, intensity):
filtered_image = apply_filter(image, filter_type, intensity)
output_path = "output.jpg"
cv2.imwrite(output_path, filtered_image)
return filtered_image, 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():
output_image = gr.Image(type="pil", label="๊ฒฐ๊ณผ ์ด๋ฏธ์ง€")
filter_input.change(fn=lambda filter_type: get_filter_description(filter_type), 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=output_image)
iface.launch()