Update app.py
Browse files
app.py
CHANGED
|
@@ -3,9 +3,8 @@ import cv2
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
from io import BytesIO
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
-
def process_image(
|
| 9 |
convert_bw,
|
| 10 |
denoise,
|
| 11 |
sharpen,
|
|
@@ -16,12 +15,11 @@ def process_image(file,
|
|
| 16 |
"""
|
| 17 |
์ด๋ฏธ์ง ์ฒ๋ฆฌ ํจ์
|
| 18 |
"""
|
| 19 |
-
if
|
| 20 |
return None, None
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
image = Image.open(
|
| 24 |
-
original_image = image.copy()
|
| 25 |
|
| 26 |
# Convert PIL Image to OpenCV format
|
| 27 |
img = np.array(image)
|
|
@@ -34,7 +32,7 @@ def process_image(file,
|
|
| 34 |
# ์คํ๋
|
| 35 |
if sharpen:
|
| 36 |
kernel = np.array([[0, -1, 0],
|
| 37 |
-
[-1, 5,-1],
|
| 38 |
[0, -1, 0]])
|
| 39 |
img = cv2.filter2D(img, -1, kernel)
|
| 40 |
|
|
@@ -69,36 +67,29 @@ def process_image(file,
|
|
| 69 |
transformed_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 70 |
transformed_image_pil = Image.fromarray(transformed_image)
|
| 71 |
|
| 72 |
-
#
|
| 73 |
buf = BytesIO()
|
| 74 |
transformed_image_pil.save(buf, format="JPEG")
|
| 75 |
-
buf.
|
| 76 |
|
| 77 |
-
|
| 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 |
-
|
| 85 |
-
|
| 86 |
-
def blend_images(original, transformed, alpha):
|
| 87 |
"""
|
| 88 |
๋ ์ด๋ฏธ์ง๋ฅผ ์ํ ๊ฐ์ ๋ฐ๋ผ ํผํฉํ๋ ํจ์
|
| 89 |
"""
|
| 90 |
-
if
|
| 91 |
return None
|
| 92 |
-
blended = Image.blend(
|
| 93 |
return blended
|
| 94 |
|
| 95 |
-
def get_original_image(
|
| 96 |
"""
|
| 97 |
-
|
| 98 |
"""
|
| 99 |
-
if
|
| 100 |
return None
|
| 101 |
-
image = Image.open(
|
| 102 |
return image
|
| 103 |
|
| 104 |
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
|
@@ -108,7 +99,7 @@ def create_interface():
|
|
| 108 |
|
| 109 |
with gr.Row():
|
| 110 |
with gr.Column(scale=1):
|
| 111 |
-
input_image = gr.File(type="
|
| 112 |
convert_bw = gr.Checkbox(label="ํ๋ฐฑ์ผ๋ก ๋ณํ", value=True)
|
| 113 |
denoise = gr.Checkbox(label="๋
ธ์ด์ฆ ์ ๊ฑฐ", value=False)
|
| 114 |
sharpen = gr.Checkbox(label="์คํ๋", value=False)
|
|
@@ -118,13 +109,13 @@ def create_interface():
|
|
| 118 |
saturation = gr.Slider(label="์ฑ๋ ์กฐ์ ", minimum=0.0, maximum=2.0, step=0.1, value=1.0)
|
| 119 |
submit = gr.Button("๋ณํํ๊ธฐ")
|
| 120 |
|
| 121 |
-
# State๋ฅผ ์ฌ์ฉํ์ฌ ์๋ณธ
|
| 122 |
original_state = gr.State()
|
| 123 |
transformed_state = gr.State()
|
| 124 |
|
| 125 |
with gr.Column(scale=1):
|
| 126 |
transformed_image = gr.Image(type="pil", label="๋ณํ๋ ์ด๋ฏธ์ง")
|
| 127 |
-
download_btn = gr.File(label="JPG๋ก ๋ค์ด๋ก๋")
|
| 128 |
|
| 129 |
# ์ด๋ฏธ์ง ์ฒ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ State์ ์ ์ฅํ๊ณ ๋ณํ๋ ์ด๋ฏธ์ง ๋ฐ ๋ค์ด๋ก๋ ํ์ผ ํ์
|
| 130 |
submit.click(
|
|
@@ -133,7 +124,7 @@ def create_interface():
|
|
| 133 |
outputs=[transformed_image, download_btn]
|
| 134 |
)
|
| 135 |
|
| 136 |
-
# ์๋ณธ ์ด๋ฏธ์ง๋ฅผ
|
| 137 |
input_image.change(
|
| 138 |
fn=get_original_image,
|
| 139 |
inputs=input_image,
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
from io import BytesIO
|
|
|
|
| 6 |
|
| 7 |
+
def process_image(file_path,
|
| 8 |
convert_bw,
|
| 9 |
denoise,
|
| 10 |
sharpen,
|
|
|
|
| 15 |
"""
|
| 16 |
์ด๋ฏธ์ง ์ฒ๋ฆฌ ํจ์
|
| 17 |
"""
|
| 18 |
+
if file_path is None:
|
| 19 |
return None, None
|
| 20 |
|
| 21 |
+
# Open the image
|
| 22 |
+
image = Image.open(file_path).convert("RGB")
|
|
|
|
| 23 |
|
| 24 |
# Convert PIL Image to OpenCV format
|
| 25 |
img = np.array(image)
|
|
|
|
| 32 |
# ์คํ๋
|
| 33 |
if sharpen:
|
| 34 |
kernel = np.array([[0, -1, 0],
|
| 35 |
+
[-1, 5, -1],
|
| 36 |
[0, -1, 0]])
|
| 37 |
img = cv2.filter2D(img, -1, kernel)
|
| 38 |
|
|
|
|
| 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 ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
|
|
|
| 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)
|
|
|
|
| 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(
|
|
|
|
| 124 |
outputs=[transformed_image, download_btn]
|
| 125 |
)
|
| 126 |
|
| 127 |
+
# ์๋ณธ ์ด๋ฏธ์ง๋ฅผ State์ ์ ์ฅ
|
| 128 |
input_image.change(
|
| 129 |
fn=get_original_image,
|
| 130 |
inputs=input_image,
|