Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,9 @@ import cv2
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
from io import BytesIO
|
|
|
|
| 6 |
|
| 7 |
-
def process_image(
|
| 8 |
convert_bw,
|
| 9 |
denoise,
|
| 10 |
sharpen,
|
|
@@ -15,56 +16,78 @@ def process_image(image,
|
|
| 15 |
"""
|
| 16 |
์ด๋ฏธ์ง ์ฒ๋ฆฌ ํจ์
|
| 17 |
"""
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
original_image = image.copy()
|
| 20 |
-
|
| 21 |
# Convert PIL Image to OpenCV format
|
| 22 |
img = np.array(image)
|
| 23 |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 24 |
-
|
| 25 |
# ๋
ธ์ด์ฆ ์ ๊ฑฐ
|
| 26 |
if denoise:
|
| 27 |
img = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21)
|
| 28 |
-
|
| 29 |
# ์คํ๋
|
| 30 |
if sharpen:
|
| 31 |
kernel = np.array([[0, -1, 0],
|
| 32 |
[-1, 5,-1],
|
| 33 |
[0, -1, 0]])
|
| 34 |
img = cv2.filter2D(img, -1, kernel)
|
| 35 |
-
|
| 36 |
# ๊ฐ๋ง ๋ณด์
|
| 37 |
if gamma != 1.0:
|
| 38 |
invGamma = 1.0 / gamma
|
| 39 |
table = np.array([((i / 255.0) ** invGamma) * 255
|
| 40 |
for i in np.arange(256)]).astype("uint8")
|
| 41 |
img = cv2.LUT(img, table)
|
| 42 |
-
|
| 43 |
# ๋ฐ๊ธฐ ์กฐ์
|
| 44 |
if brightness != 0:
|
| 45 |
img = cv2.convertScaleAbs(img, alpha=1, beta=brightness)
|
| 46 |
-
|
| 47 |
# ๋๋น ์กฐ์
|
| 48 |
if contrast != 1.0:
|
| 49 |
img = cv2.convertScaleAbs(img, alpha=contrast, beta=0)
|
| 50 |
-
|
| 51 |
# ์ฑ๋ ์กฐ์
|
| 52 |
if saturation != 1.0:
|
| 53 |
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV).astype(np.float32)
|
| 54 |
hsv[...,1] = hsv[...,1] * saturation
|
| 55 |
hsv[...,1] = np.clip(hsv[...,1], 0, 255)
|
| 56 |
img = cv2.cvtColor(hsv.astype(np.uint8), cv2.COLOR_HSV2BGR)
|
| 57 |
-
|
| 58 |
# ํ๋ฐฑ ๋ณํ
|
| 59 |
if convert_bw:
|
| 60 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 61 |
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
| 62 |
-
|
| 63 |
# ๋ณํ๋ ์ด๋ฏธ์ง๋ฅผ PIL ํ์์ผ๋ก ๋ณํ
|
| 64 |
transformed_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 65 |
transformed_image = Image.fromarray(transformed_image)
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
def blend_images(original, transformed, alpha):
|
| 70 |
"""
|
|
@@ -75,17 +98,6 @@ def blend_images(original, transformed, alpha):
|
|
| 75 |
blended = Image.blend(original, transformed, alpha)
|
| 76 |
return blended
|
| 77 |
|
| 78 |
-
def download_image(transformed):
|
| 79 |
-
"""
|
| 80 |
-
๋ณํ๋ ์ด๋ฏธ์ง๋ฅผ ๋ค์ด๋ก๋ํ ์ ์๋๋ก ํ๋ ํจ์
|
| 81 |
-
"""
|
| 82 |
-
if transformed is None:
|
| 83 |
-
return None
|
| 84 |
-
buf = BytesIO()
|
| 85 |
-
transformed.save(buf, format="JPEG")
|
| 86 |
-
buf.seek(0)
|
| 87 |
-
return buf
|
| 88 |
-
|
| 89 |
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 90 |
def create_interface():
|
| 91 |
with gr.Blocks() as demo:
|
|
@@ -93,7 +105,7 @@ def create_interface():
|
|
| 93 |
|
| 94 |
with gr.Row():
|
| 95 |
with gr.Column(scale=1):
|
| 96 |
-
input_image = gr.
|
| 97 |
convert_bw = gr.Checkbox(label="ํ๋ฐฑ์ผ๋ก ๋ณํ", value=True)
|
| 98 |
denoise = gr.Checkbox(label="๋
ธ์ด์ฆ ์ ๊ฑฐ", value=False)
|
| 99 |
sharpen = gr.Checkbox(label="์คํ๋", value=False)
|
|
@@ -109,20 +121,13 @@ def create_interface():
|
|
| 109 |
|
| 110 |
with gr.Column(scale=1):
|
| 111 |
transformed_image = gr.Image(type="pil", label="๋ณํ๋ ์ด๋ฏธ์ง")
|
| 112 |
-
download_btn = gr.
|
| 113 |
|
| 114 |
-
# ์ด๋ฏธ์ง ์ฒ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ State์ ์ ์ฅํ๊ณ ๋ณํ๋ ์ด๋ฏธ์ง ํ์
|
| 115 |
submit.click(
|
| 116 |
fn=process_image,
|
| 117 |
inputs=[input_image, convert_bw, denoise, sharpen, gamma, brightness, contrast, saturation],
|
| 118 |
-
outputs=[transformed_image, original_state, transformed_state]
|
| 119 |
-
)
|
| 120 |
-
|
| 121 |
-
# ๋ค์ด๋ก๋ ๋ฒํผ์ ๋ณํ๋ ์ด๋ฏธ์ง ์ฐ๊ฒฐ
|
| 122 |
-
download_btn.click(
|
| 123 |
-
fn=download_image,
|
| 124 |
-
inputs=transformed_state,
|
| 125 |
-
outputs=download_btn
|
| 126 |
)
|
| 127 |
|
| 128 |
gr.Markdown("### ์ด๋ฏธ์ง ๋น๊ต")
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
from io import BytesIO
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
def process_image(file,
|
| 9 |
convert_bw,
|
| 10 |
denoise,
|
| 11 |
sharpen,
|
|
|
|
| 16 |
"""
|
| 17 |
์ด๋ฏธ์ง ์ฒ๋ฆฌ ํจ์
|
| 18 |
"""
|
| 19 |
+
if file is None:
|
| 20 |
+
return None, None, None, None
|
| 21 |
+
|
| 22 |
+
# Extract original filename
|
| 23 |
+
original_filename = os.path.basename(file.name)
|
| 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
|
| 30 |
img = np.array(image)
|
| 31 |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 32 |
+
|
| 33 |
# ๋
ธ์ด์ฆ ์ ๊ฑฐ
|
| 34 |
if denoise:
|
| 35 |
img = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21)
|
| 36 |
+
|
| 37 |
# ์คํ๋
|
| 38 |
if sharpen:
|
| 39 |
kernel = np.array([[0, -1, 0],
|
| 40 |
[-1, 5,-1],
|
| 41 |
[0, -1, 0]])
|
| 42 |
img = cv2.filter2D(img, -1, kernel)
|
| 43 |
+
|
| 44 |
# ๊ฐ๋ง ๋ณด์
|
| 45 |
if gamma != 1.0:
|
| 46 |
invGamma = 1.0 / gamma
|
| 47 |
table = np.array([((i / 255.0) ** invGamma) * 255
|
| 48 |
for i in np.arange(256)]).astype("uint8")
|
| 49 |
img = cv2.LUT(img, table)
|
| 50 |
+
|
| 51 |
# ๋ฐ๊ธฐ ์กฐ์
|
| 52 |
if brightness != 0:
|
| 53 |
img = cv2.convertScaleAbs(img, alpha=1, beta=brightness)
|
| 54 |
+
|
| 55 |
# ๋๋น ์กฐ์
|
| 56 |
if contrast != 1.0:
|
| 57 |
img = cv2.convertScaleAbs(img, alpha=contrast, beta=0)
|
| 58 |
+
|
| 59 |
# ์ฑ๋ ์กฐ์
|
| 60 |
if saturation != 1.0:
|
| 61 |
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV).astype(np.float32)
|
| 62 |
hsv[...,1] = hsv[...,1] * saturation
|
| 63 |
hsv[...,1] = np.clip(hsv[...,1], 0, 255)
|
| 64 |
img = cv2.cvtColor(hsv.astype(np.uint8), cv2.COLOR_HSV2BGR)
|
| 65 |
+
|
| 66 |
# ํ๋ฐฑ ๋ณํ
|
| 67 |
if convert_bw:
|
| 68 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 69 |
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
| 70 |
+
|
| 71 |
# ๋ณํ๋ ์ด๋ฏธ์ง๋ฅผ PIL ํ์์ผ๋ก ๋ณํ
|
| 72 |
transformed_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 73 |
transformed_image = Image.fromarray(transformed_image)
|
| 74 |
+
|
| 75 |
+
# Prepare the download file
|
| 76 |
+
buf = BytesIO()
|
| 77 |
+
transformed_image.save(buf, format="JPEG")
|
| 78 |
+
buf.seek(0)
|
| 79 |
+
|
| 80 |
+
# ์๋ก์ด ํ์ผ๋ช
์ค์ (์: original_transformed.jpg)
|
| 81 |
+
if '.' in original_filename:
|
| 82 |
+
base, ext = original_filename.rsplit('.', 1)
|
| 83 |
+
else:
|
| 84 |
+
base, ext = original_filename, 'jpg'
|
| 85 |
+
new_filename = f"{base}_transformed.{ext}"
|
| 86 |
+
|
| 87 |
+
# gr.File์ ์ํ ํ์ผ ์ด๋ฆ ์ค์
|
| 88 |
+
buf.name = new_filename
|
| 89 |
+
|
| 90 |
+
return transformed_image, original_image, transformed_image, buf
|
| 91 |
|
| 92 |
def blend_images(original, transformed, alpha):
|
| 93 |
"""
|
|
|
|
| 98 |
blended = Image.blend(original, transformed, alpha)
|
| 99 |
return blended
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 102 |
def create_interface():
|
| 103 |
with gr.Blocks() as demo:
|
|
|
|
| 105 |
|
| 106 |
with gr.Row():
|
| 107 |
with gr.Column(scale=1):
|
| 108 |
+
input_image = gr.File(type="file", label="์๋ณธ ์ด๋ฏธ์ง ์
๋ก๋")
|
| 109 |
convert_bw = gr.Checkbox(label="ํ๋ฐฑ์ผ๋ก ๋ณํ", value=True)
|
| 110 |
denoise = gr.Checkbox(label="๋
ธ์ด์ฆ ์ ๊ฑฐ", value=False)
|
| 111 |
sharpen = gr.Checkbox(label="์คํ๋", value=False)
|
|
|
|
| 121 |
|
| 122 |
with gr.Column(scale=1):
|
| 123 |
transformed_image = gr.Image(type="pil", label="๋ณํ๋ ์ด๋ฏธ์ง")
|
| 124 |
+
download_btn = gr.File(label="JPG๋ก ๋ค์ด๋ก๋")
|
| 125 |
|
| 126 |
+
# ์ด๋ฏธ์ง ์ฒ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ State์ ์ ์ฅํ๊ณ ๋ณํ๋ ์ด๋ฏธ์ง ๋ฐ ๋ค์ด๋ก๋ ํ์ผ ํ์
|
| 127 |
submit.click(
|
| 128 |
fn=process_image,
|
| 129 |
inputs=[input_image, convert_bw, denoise, sharpen, gamma, brightness, contrast, saturation],
|
| 130 |
+
outputs=[transformed_image, original_state, transformed_state, download_btn]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
)
|
| 132 |
|
| 133 |
gr.Markdown("### ์ด๋ฏธ์ง ๋น๊ต")
|