Update app.py
Browse files
app.py
CHANGED
|
@@ -1,151 +1,40 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
import numpy as np
|
| 4 |
-
from PIL import Image
|
| 5 |
-
from io import BytesIO
|
| 6 |
|
| 7 |
-
def
|
| 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 |
-
if gamma != 1.0:
|
| 41 |
-
invGamma = 1.0 / gamma
|
| 42 |
-
table = np.array([((i / 255.0) ** invGamma) * 255
|
| 43 |
-
for i in np.arange(256)]).astype("uint8")
|
| 44 |
-
img = cv2.LUT(img, table)
|
| 45 |
-
|
| 46 |
-
# ๋ฐ๊ธฐ ์กฐ์
|
| 47 |
-
if brightness != 0:
|
| 48 |
-
img = cv2.convertScaleAbs(img, alpha=1, beta=brightness)
|
| 49 |
-
|
| 50 |
-
# ๋๋น ์กฐ์
|
| 51 |
-
if contrast != 1.0:
|
| 52 |
-
img = cv2.convertScaleAbs(img, alpha=contrast, beta=0)
|
| 53 |
-
|
| 54 |
-
# ์ฑ๋ ์กฐ์
|
| 55 |
-
if saturation != 1.0:
|
| 56 |
-
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV).astype(np.float32)
|
| 57 |
-
hsv[...,1] = hsv[...,1] * saturation
|
| 58 |
-
hsv[...,1] = np.clip(hsv[...,1], 0, 255)
|
| 59 |
-
img = cv2.cvtColor(hsv.astype(np.uint8), cv2.COLOR_HSV2BGR)
|
| 60 |
-
|
| 61 |
-
# ํ๋ฐฑ ๋ณํ
|
| 62 |
-
if convert_bw:
|
| 63 |
-
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 64 |
-
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
| 65 |
-
|
| 66 |
-
# ๋ณํ๋ ์ด๋ฏธ์ง๋ฅผ PIL ํ์์ผ๋ก ๋ณํ
|
| 67 |
-
transformed_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 68 |
-
transformed_image_pil = Image.fromarray(transformed_image)
|
| 69 |
-
|
| 70 |
-
# Save the transformed image to BytesIO
|
| 71 |
-
buf = BytesIO()
|
| 72 |
-
transformed_image_pil.save(buf, format="JPEG")
|
| 73 |
-
byte_data = buf.getvalue()
|
| 74 |
-
|
| 75 |
-
return transformed_image_pil, byte_data
|
| 76 |
-
|
| 77 |
-
def blend_images(original_image, transformed_image, alpha):
|
| 78 |
-
"""
|
| 79 |
-
๋ ์ด๋ฏธ์ง๋ฅผ ์ํ ๊ฐ์ ๋ฐ๋ผ ํผํฉํ๋ ํจ์
|
| 80 |
-
"""
|
| 81 |
-
if original_image is None or transformed_image is None:
|
| 82 |
-
return None
|
| 83 |
-
blended = Image.blend(original_image, transformed_image, alpha)
|
| 84 |
-
return blended
|
| 85 |
-
|
| 86 |
-
def get_original_image(file_path):
|
| 87 |
-
"""
|
| 88 |
-
์๋ณธ ์ด๋ฏธ์ง๋ฅผ PIL.Image ํ์์ผ๋ก ๋ฐํํ๋ ํจ์
|
| 89 |
-
"""
|
| 90 |
-
if file_path is None:
|
| 91 |
-
return None
|
| 92 |
-
image = Image.open(file_path).convert("RGB")
|
| 93 |
-
return image
|
| 94 |
-
|
| 95 |
-
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 96 |
-
def create_interface():
|
| 97 |
-
with gr.Blocks() as demo:
|
| 98 |
-
gr.Markdown("## ์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ ํ๋ฆฌ์ผ์ด์
")
|
| 99 |
-
|
| 100 |
-
with gr.Row():
|
| 101 |
-
with gr.Column(scale=1):
|
| 102 |
-
input_image = gr.File(type="filepath", label="์๋ณธ ์ด๋ฏธ์ง ์
๋ก๋")
|
| 103 |
-
convert_bw = gr.Checkbox(label="ํ๋ฐฑ์ผ๋ก ๋ณํ", value=True)
|
| 104 |
-
denoise = gr.Checkbox(label="๋
ธ์ด์ฆ ์ ๊ฑฐ", value=False)
|
| 105 |
-
sharpen = gr.Checkbox(label="์คํ๋", value=False)
|
| 106 |
-
gamma = gr.Slider(label="๊ฐ๋ง ๋ณด์ ", minimum=0.1, maximum=3.0, step=0.1, value=1.0)
|
| 107 |
-
brightness = gr.Slider(label="๋ฐ๊ธฐ ์กฐ์ ", minimum=-100, maximum=100, step=1, value=0)
|
| 108 |
-
contrast = gr.Slider(label="๋๋น ์กฐ์ ", minimum=0.5, maximum=1.5, step=0.1, value=1.0)
|
| 109 |
-
saturation = gr.Slider(label="์ฑ๋ ์กฐ์ ", minimum=0.0, maximum=2.0, step=0.1, value=1.0)
|
| 110 |
-
submit = gr.Button("๋ณํํ๊ธฐ")
|
| 111 |
-
|
| 112 |
-
# State๋ฅผ ์ฌ์ฉํ์ฌ ์๋ณธ ์ด๋ฏธ์ง ์ ์ฅ
|
| 113 |
-
original_state = gr.State()
|
| 114 |
-
transformed_state = gr.State()
|
| 115 |
-
|
| 116 |
-
with gr.Column(scale=1):
|
| 117 |
-
transformed_image = gr.Image(type="pil", label="๋ณํ๋ ์ด๋ฏธ์ง")
|
| 118 |
-
download_btn = gr.File(label="JPG๋ก ๋ค์ด๋ก๋", type="binary")
|
| 119 |
-
|
| 120 |
-
# ์ด๋ฏธ์ง ์ฒ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ State์ ์ ์ฅํ๊ณ ๋ณํ๋ ์ด๋ฏธ์ง ๋ฐ ๋ค์ด๋ก๋ ํ์ผ ํ์
|
| 121 |
-
submit.click(
|
| 122 |
-
fn=process_image,
|
| 123 |
-
inputs=[input_image, convert_bw, denoise, sharpen, gamma, brightness, contrast, saturation],
|
| 124 |
-
outputs=[transformed_image, download_btn]
|
| 125 |
-
)
|
| 126 |
-
|
| 127 |
-
# ์๋ณธ ์ด๋ฏธ์ง๋ฅผ State์ ์ ์ฅ
|
| 128 |
-
input_image.change(
|
| 129 |
-
fn=get_original_image,
|
| 130 |
-
inputs=input_image,
|
| 131 |
-
outputs=original_state
|
| 132 |
-
)
|
| 133 |
-
|
| 134 |
-
gr.Markdown("### ์ด๋ฏธ์ง ๋น๊ต")
|
| 135 |
-
|
| 136 |
-
with gr.Row():
|
| 137 |
-
alpha_slider = gr.Slider(label="๋น๊ต ์ฌ๋ผ์ด๋", minimum=0.0, maximum=1.0, step=0.01, value=0.5)
|
| 138 |
-
blended_image = gr.Image(type="pil", label="ํผํฉ๋ ์ด๋ฏธ๏ฟฝ๏ฟฝ", height=300)
|
| 139 |
-
|
| 140 |
-
# ์ฌ๋ผ์ด๋ ์กฐ์ ์ ์ด๋ฏธ์ง ํผํฉ
|
| 141 |
-
alpha_slider.change(
|
| 142 |
-
fn=blend_images,
|
| 143 |
-
inputs=[original_state, transformed_image, alpha_slider],
|
| 144 |
-
outputs=blended_image
|
| 145 |
-
)
|
| 146 |
|
| 147 |
-
|
|
|
|
| 148 |
|
| 149 |
-
|
| 150 |
-
interface = create_interface()
|
| 151 |
-
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image, ImageEnhance, ImageOps
|
| 3 |
import numpy as np
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
def convert_to_moody_bw(image):
|
| 6 |
+
# ์ด๋ฏธ์ง๋ฅผ ํ๋ฐฑ์ผ๋ก ๋ณํ
|
| 7 |
+
bw_image = ImageOps.grayscale(image)
|
| 8 |
+
|
| 9 |
+
# ๋๋น ํฅ์
|
| 10 |
+
enhancer = ImageEnhance.Contrast(bw_image)
|
| 11 |
+
bw_image = enhancer.enhance(1.5)
|
| 12 |
+
|
| 13 |
+
# ๋ฐ๊ธฐ ์กฐ์
|
| 14 |
+
enhancer = ImageEnhance.Brightness(bw_image)
|
| 15 |
+
bw_image = enhancer.enhance(0.9)
|
| 16 |
+
|
| 17 |
+
# ์ฝ๊ฐ์ ํ๋ฆผ ํจ๊ณผ ์ถ๊ฐ
|
| 18 |
+
bw_image = bw_image.filter(Image.Filter.GaussianBlur(radius=1))
|
| 19 |
+
|
| 20 |
+
return bw_image
|
| 21 |
+
|
| 22 |
+
def download_jpg(image):
|
| 23 |
+
# ์ด๋ฏธ์ง๋ฅผ JPG ํ์์ผ๋ก ์ ์ฅ
|
| 24 |
+
return image.convert("RGB")
|
| 25 |
+
|
| 26 |
+
# Gradio ์ธํฐํ์ด์ค ์ค์
|
| 27 |
+
with gr.Blocks() as demo:
|
| 28 |
+
gr.Markdown("## ๋ถ์๊ธฐ ์๋ ํ๋ฐฑ ์ฌ์ง ๋ณํ๊ธฐ")
|
| 29 |
+
with gr.Row():
|
| 30 |
+
with gr.Column():
|
| 31 |
+
input_image = gr.Image(type="pil", label="์๋ณธ ์ด๋ฏธ์ง ์
๋ก๋")
|
| 32 |
+
convert_button = gr.Button("๋ณํํ๊ธฐ")
|
| 33 |
+
with gr.Column():
|
| 34 |
+
output_image = gr.Image(type="pil", label="๋ณํ๋ ์ด๋ฏธ์ง")
|
| 35 |
+
download_button = gr.Button("JPG๋ก ๋ค์ด๋ก๋")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
convert_button.click(fn=convert_to_moody_bw, inputs=input_image, outputs=output_image)
|
| 38 |
+
download_button.click(fn=download_jpg, inputs=output_image, outputs=gr.File())
|
| 39 |
|
| 40 |
+
demo.launch()
|
|
|
|
|
|