Update app.py
Browse files
app.py
CHANGED
|
@@ -17,13 +17,10 @@ def process_image(file,
|
|
| 17 |
์ด๋ฏธ์ง ์ฒ๋ฆฌ ํจ์
|
| 18 |
"""
|
| 19 |
if file is None:
|
| 20 |
-
return None, None
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
# Open the image
|
| 26 |
-
image = Image.open(file).convert("RGB")
|
| 27 |
original_image = image.copy()
|
| 28 |
|
| 29 |
# Convert PIL Image to OpenCV format
|
|
@@ -70,24 +67,21 @@ def process_image(file,
|
|
| 70 |
|
| 71 |
# ๋ณํ๋ ์ด๋ฏธ์ง๋ฅผ PIL ํ์์ผ๋ก ๋ณํ
|
| 72 |
transformed_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 73 |
-
|
| 74 |
|
| 75 |
# Prepare the download file
|
| 76 |
buf = BytesIO()
|
| 77 |
-
|
| 78 |
buf.seek(0)
|
| 79 |
|
| 80 |
-
#
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
# gr.File์ ์ํ ํ์ผ ์ด๋ฆ ์ค์
|
| 88 |
-
buf.name = new_filename
|
| 89 |
|
| 90 |
-
return
|
| 91 |
|
| 92 |
def blend_images(original, transformed, alpha):
|
| 93 |
"""
|
|
@@ -98,6 +92,15 @@ def blend_images(original, transformed, alpha):
|
|
| 98 |
blended = Image.blend(original, transformed, alpha)
|
| 99 |
return blended
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 102 |
def create_interface():
|
| 103 |
with gr.Blocks() as demo:
|
|
@@ -105,7 +108,7 @@ def create_interface():
|
|
| 105 |
|
| 106 |
with gr.Row():
|
| 107 |
with gr.Column(scale=1):
|
| 108 |
-
input_image = gr.File(type="
|
| 109 |
convert_bw = gr.Checkbox(label="ํ๋ฐฑ์ผ๋ก ๋ณํ", value=True)
|
| 110 |
denoise = gr.Checkbox(label="๋
ธ์ด์ฆ ์ ๊ฑฐ", value=False)
|
| 111 |
sharpen = gr.Checkbox(label="์คํ๋", value=False)
|
|
@@ -127,7 +130,14 @@ def create_interface():
|
|
| 127 |
submit.click(
|
| 128 |
fn=process_image,
|
| 129 |
inputs=[input_image, convert_bw, denoise, sharpen, gamma, brightness, contrast, saturation],
|
| 130 |
-
outputs=[transformed_image,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
)
|
| 132 |
|
| 133 |
gr.Markdown("### ์ด๋ฏธ์ง ๋น๊ต")
|
|
@@ -139,7 +149,7 @@ def create_interface():
|
|
| 139 |
# ์ฌ๋ผ์ด๋ ์กฐ์ ์ ์ด๋ฏธ์ง ํผํฉ
|
| 140 |
alpha_slider.change(
|
| 141 |
fn=blend_images,
|
| 142 |
-
inputs=[original_state,
|
| 143 |
outputs=blended_image
|
| 144 |
)
|
| 145 |
|
|
|
|
| 17 |
์ด๋ฏธ์ง ์ฒ๋ฆฌ ํจ์
|
| 18 |
"""
|
| 19 |
if file is None:
|
| 20 |
+
return None, None
|
| 21 |
|
| 22 |
+
# Read the binary data
|
| 23 |
+
image = Image.open(BytesIO(file)).convert("RGB")
|
|
|
|
|
|
|
|
|
|
| 24 |
original_image = image.copy()
|
| 25 |
|
| 26 |
# Convert PIL Image to OpenCV format
|
|
|
|
| 67 |
|
| 68 |
# ๋ณํ๋ ์ด๋ฏธ์ง๋ฅผ PIL ํ์์ผ๋ก ๋ณํ
|
| 69 |
transformed_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 70 |
+
transformed_image_pil = Image.fromarray(transformed_image)
|
| 71 |
|
| 72 |
# Prepare the download file
|
| 73 |
buf = BytesIO()
|
| 74 |
+
transformed_image_pil.save(buf, format="JPEG")
|
| 75 |
buf.seek(0)
|
| 76 |
|
| 77 |
+
# Set the file name
|
| 78 |
+
# Extract original file name from the uploaded file
|
| 79 |
+
# Since gr.File with type='binary' provides bytes, the filename needs to be extracted separately
|
| 80 |
+
# For simplicity, set a default filename
|
| 81 |
+
original_filename = "transformed_image.jpg"
|
| 82 |
+
buf.name = original_filename
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
return transformed_image_pil, buf
|
| 85 |
|
| 86 |
def blend_images(original, transformed, alpha):
|
| 87 |
"""
|
|
|
|
| 92 |
blended = Image.blend(original, transformed, alpha)
|
| 93 |
return blended
|
| 94 |
|
| 95 |
+
def get_original_image(file):
|
| 96 |
+
"""
|
| 97 |
+
Get the original image as PIL.Image
|
| 98 |
+
"""
|
| 99 |
+
if file is None:
|
| 100 |
+
return None
|
| 101 |
+
image = Image.open(BytesIO(file)).convert("RGB")
|
| 102 |
+
return image
|
| 103 |
+
|
| 104 |
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 105 |
def create_interface():
|
| 106 |
with gr.Blocks() as demo:
|
|
|
|
| 108 |
|
| 109 |
with gr.Row():
|
| 110 |
with gr.Column(scale=1):
|
| 111 |
+
input_image = gr.File(type="binary", label="์๋ณธ ์ด๋ฏธ์ง ์
๋ก๋")
|
| 112 |
convert_bw = gr.Checkbox(label="ํ๋ฐฑ์ผ๋ก ๋ณํ", value=True)
|
| 113 |
denoise = gr.Checkbox(label="๋
ธ์ด์ฆ ์ ๊ฑฐ", value=False)
|
| 114 |
sharpen = gr.Checkbox(label="์คํ๋", value=False)
|
|
|
|
| 130 |
submit.click(
|
| 131 |
fn=process_image,
|
| 132 |
inputs=[input_image, convert_bw, denoise, sharpen, gamma, brightness, contrast, saturation],
|
| 133 |
+
outputs=[transformed_image, download_btn]
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
# ์๋ณธ ์ด๋ฏธ์ง๋ฅผ ์ํ์ ์ ์ฅ
|
| 137 |
+
input_image.change(
|
| 138 |
+
fn=get_original_image,
|
| 139 |
+
inputs=input_image,
|
| 140 |
+
outputs=original_state
|
| 141 |
)
|
| 142 |
|
| 143 |
gr.Markdown("### ์ด๋ฏธ์ง ๋น๊ต")
|
|
|
|
| 149 |
# ์ฌ๋ผ์ด๋ ์กฐ์ ์ ์ด๋ฏธ์ง ํผํฉ
|
| 150 |
alpha_slider.change(
|
| 151 |
fn=blend_images,
|
| 152 |
+
inputs=[original_state, transformed_image, alpha_slider],
|
| 153 |
outputs=blended_image
|
| 154 |
)
|
| 155 |
|