Update app.py
Browse files
app.py
CHANGED
|
@@ -17,32 +17,34 @@ def process_image(
|
|
| 17 |
์
๋ก๋๋ ์ด๋ฏธ์ง๋ฅผ ํ๋ผ๋ฏธํฐ(๋
ธ์ด์ฆ ์ ๊ฑฐ, ์คํ๋, ๊ฐ๋ง ๋ณด์ , ๋ฐ๊ธฐ, ๋๋น, ์ฑ๋)์ ๋ฐ๋ผ
|
| 18 |
์ต์ข
์ ์ผ๋ก ํ๋ฐฑ ์ฌ์ง์ผ๋ก ๋ณํํ์ฌ ๋ฐํํฉ๋๋ค.
|
| 19 |
"""
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
# Pillow Image -> Numpy array ๋ณํ
|
| 22 |
img_np = np.array(img)
|
| 23 |
|
| 24 |
# ================ 1. ๋
ธ์ด์ฆ ์ ๊ฑฐ ================
|
| 25 |
-
# ์ด๋ฏธ์ง
|
| 26 |
if len(img_np.shape) == 3:
|
| 27 |
-
# RGB ์ด๋ฏธ์ง์ ๊ฒฝ์ฐ channel_axis=-1
|
| 28 |
sigma_est = np.mean(estimate_sigma(img_np, channel_axis=-1))
|
| 29 |
denoised = denoise_nl_means(
|
| 30 |
img_np,
|
| 31 |
h=denoise_strength * sigma_est,
|
| 32 |
patch_size=5,
|
| 33 |
patch_distance=3,
|
| 34 |
-
channel_axis=-1,
|
| 35 |
fast_mode=True
|
| 36 |
)
|
| 37 |
else:
|
| 38 |
-
# ํ๋ฐฑ(2์ฐจ์) ์ด๋ฏธ์ง
|
| 39 |
sigma_est = np.mean(estimate_sigma(img_np, channel_axis=None))
|
| 40 |
denoised = denoise_nl_means(
|
| 41 |
img_np,
|
| 42 |
h=denoise_strength * sigma_est,
|
| 43 |
patch_size=5,
|
| 44 |
patch_distance=3,
|
| 45 |
-
channel_axis=None,
|
| 46 |
fast_mode=True
|
| 47 |
)
|
| 48 |
|
|
@@ -56,7 +58,6 @@ def process_image(
|
|
| 56 |
sharpened_img = enhancer_sharpness.enhance(sharpen_strength)
|
| 57 |
|
| 58 |
# ================ 3. ๊ฐ๋ง ๋ณด์ ================
|
| 59 |
-
# Pillow์ ๊ฐ๋ง ๋ณด์ ์ด ์์ผ๋ฏ๋ก, NumPy๋ก ์ฒ๋ฆฌ
|
| 60 |
gamma_np = np.array(sharpened_img).astype(np.float32) / 255.0
|
| 61 |
gamma_corrected = np.power(gamma_np, 1.0 / gamma)
|
| 62 |
gamma_corrected = (gamma_corrected * 255).astype(np.uint8)
|
|
@@ -95,7 +96,7 @@ def generate_output(
|
|
| 95 |
if input_image is None:
|
| 96 |
return None, None
|
| 97 |
|
| 98 |
-
#
|
| 99 |
transformed = process_image(
|
| 100 |
input_image,
|
| 101 |
denoise_strength,
|
|
@@ -106,7 +107,7 @@ def generate_output(
|
|
| 106 |
saturation
|
| 107 |
)
|
| 108 |
|
| 109 |
-
#
|
| 110 |
with io.BytesIO() as output:
|
| 111 |
transformed.save(output, format="JPEG")
|
| 112 |
contents = output.getvalue()
|
|
@@ -114,7 +115,7 @@ def generate_output(
|
|
| 114 |
# Gradio์ File ํ์: (ํ์ผ ๋ฐ์ดํธ, MIME ํ์
, ๋ค์ด๋ก๋ ์ ํ์๋ ํ์ผ๋ช
)
|
| 115 |
file_data = (contents, "image/jpeg", "transformed.jpg")
|
| 116 |
|
| 117 |
-
# ๋ณํ๋ ์ด๋ฏธ์ง๋ฅผ
|
| 118 |
return transformed, file_data
|
| 119 |
|
| 120 |
def main():
|
|
@@ -198,6 +199,7 @@ def main():
|
|
| 198 |
):
|
| 199 |
if input_image is None:
|
| 200 |
return None
|
|
|
|
| 201 |
transformed = process_image(
|
| 202 |
input_image,
|
| 203 |
denoise_strength,
|
|
@@ -207,16 +209,18 @@ def main():
|
|
| 207 |
contrast,
|
| 208 |
saturation
|
| 209 |
)
|
| 210 |
-
# ๋ ์ด๋ฏธ์ง๋ฅผ ๊ฐ๋ก๋ก ํฉ
|
| 211 |
-
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
| 213 |
new_w = input_w + transformed_w
|
| 214 |
new_h = max(input_h, transformed_h)
|
| 215 |
|
| 216 |
new_image = Image.new("RGB", (new_w, new_h))
|
| 217 |
-
|
| 218 |
-
new_image.paste(
|
| 219 |
-
new_image.paste(transformed.convert("RGB"), (input_w, 0))
|
| 220 |
|
| 221 |
return new_image
|
| 222 |
|
|
|
|
| 17 |
์
๋ก๋๋ ์ด๋ฏธ์ง๋ฅผ ํ๋ผ๋ฏธํฐ(๋
ธ์ด์ฆ ์ ๊ฑฐ, ์คํ๋, ๊ฐ๋ง ๋ณด์ , ๋ฐ๊ธฐ, ๋๋น, ์ฑ๋)์ ๋ฐ๋ผ
|
| 18 |
์ต์ข
์ ์ผ๋ก ํ๋ฐฑ ์ฌ์ง์ผ๋ก ๋ณํํ์ฌ ๋ฐํํฉ๋๋ค.
|
| 19 |
"""
|
| 20 |
+
# ํน์ ๋ชจ๋ฅผ Alpha ์ฑ๋ ๋ฌธ์ ๋ฐฉ์ง (4์ฑ๋ -> 3์ฑ๋)
|
| 21 |
+
img = img.convert("RGB") # RGBA์ธ ๊ฒฝ์ฐ์๋ RGB๋ก ๊ฐ์ ๋ณํ
|
| 22 |
+
|
| 23 |
# Pillow Image -> Numpy array ๋ณํ
|
| 24 |
img_np = np.array(img)
|
| 25 |
|
| 26 |
# ================ 1. ๋
ธ์ด์ฆ ์ ๊ฑฐ ================
|
| 27 |
+
# ์ด๋ฏธ์ง๊ฐ RGB(3์ฐจ์)์ธ์ง, ํ๋ฐฑ(2์ฐจ์)์ธ์ง ํ๋ณ
|
| 28 |
if len(img_np.shape) == 3:
|
| 29 |
+
# RGB ์ด๋ฏธ์ง์ ๊ฒฝ์ฐ channel_axis=-1
|
| 30 |
sigma_est = np.mean(estimate_sigma(img_np, channel_axis=-1))
|
| 31 |
denoised = denoise_nl_means(
|
| 32 |
img_np,
|
| 33 |
h=denoise_strength * sigma_est,
|
| 34 |
patch_size=5,
|
| 35 |
patch_distance=3,
|
| 36 |
+
channel_axis=-1,
|
| 37 |
fast_mode=True
|
| 38 |
)
|
| 39 |
else:
|
| 40 |
+
# ํ๋ฐฑ(2์ฐจ์) ์ด๋ฏธ์ง์ธ ๊ฒฝ์ฐ channel_axis=None
|
| 41 |
sigma_est = np.mean(estimate_sigma(img_np, channel_axis=None))
|
| 42 |
denoised = denoise_nl_means(
|
| 43 |
img_np,
|
| 44 |
h=denoise_strength * sigma_est,
|
| 45 |
patch_size=5,
|
| 46 |
patch_distance=3,
|
| 47 |
+
channel_axis=None,
|
| 48 |
fast_mode=True
|
| 49 |
)
|
| 50 |
|
|
|
|
| 58 |
sharpened_img = enhancer_sharpness.enhance(sharpen_strength)
|
| 59 |
|
| 60 |
# ================ 3. ๊ฐ๋ง ๋ณด์ ================
|
|
|
|
| 61 |
gamma_np = np.array(sharpened_img).astype(np.float32) / 255.0
|
| 62 |
gamma_corrected = np.power(gamma_np, 1.0 / gamma)
|
| 63 |
gamma_corrected = (gamma_corrected * 255).astype(np.uint8)
|
|
|
|
| 96 |
if input_image is None:
|
| 97 |
return None, None
|
| 98 |
|
| 99 |
+
# ๋ณํ๋ ์ด๋ฏธ์ง
|
| 100 |
transformed = process_image(
|
| 101 |
input_image,
|
| 102 |
denoise_strength,
|
|
|
|
| 107 |
saturation
|
| 108 |
)
|
| 109 |
|
| 110 |
+
# ๋ค์ด๋ก๋ ๊ฐ๋ฅํ JPG ํ์ผ ์์ฑ
|
| 111 |
with io.BytesIO() as output:
|
| 112 |
transformed.save(output, format="JPEG")
|
| 113 |
contents = output.getvalue()
|
|
|
|
| 115 |
# Gradio์ File ํ์: (ํ์ผ ๋ฐ์ดํธ, MIME ํ์
, ๋ค์ด๋ก๋ ์ ํ์๋ ํ์ผ๋ช
)
|
| 116 |
file_data = (contents, "image/jpeg", "transformed.jpg")
|
| 117 |
|
| 118 |
+
# ๋ณํ๋ ์ด๋ฏธ์ง๋ฅผ ๊ทธ๋ฆฌ๋์ค Image๋ก ๋ณด์ฌ์ฃผ๊ณ , ๋ค์ด๋ก๋ ํ์ผ๋ ํจ๊ป ๋ฐํ
|
| 119 |
return transformed, file_data
|
| 120 |
|
| 121 |
def main():
|
|
|
|
| 199 |
):
|
| 200 |
if input_image is None:
|
| 201 |
return None
|
| 202 |
+
# ๋ณํ
|
| 203 |
transformed = process_image(
|
| 204 |
input_image,
|
| 205 |
denoise_strength,
|
|
|
|
| 209 |
contrast,
|
| 210 |
saturation
|
| 211 |
)
|
| 212 |
+
# ๋ ์ด๋ฏธ์ง๋ฅผ ๊ฐ๋ก๋ก ํฉ์นจ
|
| 213 |
+
input_image_rgb = input_image.convert("RGB")
|
| 214 |
+
transformed_rgb = transformed.convert("RGB")
|
| 215 |
+
|
| 216 |
+
input_w, input_h = input_image_rgb.size
|
| 217 |
+
transformed_w, transformed_h = transformed_rgb.size
|
| 218 |
new_w = input_w + transformed_w
|
| 219 |
new_h = max(input_h, transformed_h)
|
| 220 |
|
| 221 |
new_image = Image.new("RGB", (new_w, new_h))
|
| 222 |
+
new_image.paste(input_image_rgb, (0, 0))
|
| 223 |
+
new_image.paste(transformed_rgb, (input_w, 0))
|
|
|
|
| 224 |
|
| 225 |
return new_image
|
| 226 |
|