Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def sharpen_image(image):
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
def process_image(image, gamma=1.0, brightness=0, contrast=0, saturation=0):
|
| 6 |
+
# 흑백 변환
|
| 7 |
+
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 8 |
+
gray_image = cv2.cvtColor(gray_image, cv2.COLOR_GRAY2BGR)
|
| 9 |
+
|
| 10 |
+
# 감마 보정
|
| 11 |
+
gamma_corrected = np.array(255 * (gray_image / 255) ** (1 / gamma), dtype='uint8')
|
| 12 |
+
|
| 13 |
+
# 밝기 및 대비 조정
|
| 14 |
+
adjusted = cv2.convertScaleAbs(gamma_corrected, alpha=1 + contrast / 100, beta=brightness)
|
| 15 |
+
|
| 16 |
+
# 채도 조정 (흑백이라 의미 없음, 하지만 처리 구조 포함)
|
| 17 |
+
hsv_image = cv2.cvtColor(adjusted, cv2.COLOR_BGR2HSV).astype(np.float32)
|
| 18 |
+
hsv_image[..., 1] *= 1 + saturation / 100
|
| 19 |
+
hsv_image = np.clip(hsv_image, 0, 255).astype(np.uint8)
|
| 20 |
+
final_image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR)
|
| 21 |
+
|
| 22 |
+
return final_image
|
| 23 |
+
|
| 24 |
+
def remove_noise(image):
|
| 25 |
+
return cv2.fastNlMeansDenoisingColored(image, None, 10, 10, 7, 21)
|
| 26 |
+
|
| 27 |
def sharpen_image(image):
|
| 28 |
+
kernel = np.array([[0, -1, 0], [-1, 5,-1], [0, -1, 0]])
|
| 29 |
+
return cv2.filter2D(image, -1, kernel)
|
| 30 |
+
|
| 31 |
+
def enhance_image(image, gamma, brightness, contrast, saturation):
|
| 32 |
+
denoised = remove_noise(image)
|
| 33 |
+
sharpened = sharpen_image(denoised)
|
| 34 |
+
return process_image(sharpened, gamma, brightness, contrast, saturation)
|
| 35 |
+
|
| 36 |
+
def main(image, gamma, brightness, contrast, saturation):
|
| 37 |
+
enhanced_image = enhance_image(image, gamma, brightness, contrast, saturation)
|
| 38 |
+
return enhanced_image
|
| 39 |
+
|
| 40 |
+
def save_image(image):
|
| 41 |
+
path = "output.jpg"
|
| 42 |
+
cv2.imwrite(path, image)
|
| 43 |
+
return path
|
| 44 |
+
|
| 45 |
+
# Gradio 인터페이스
|
| 46 |
+
with gr.Blocks() as demo:
|
| 47 |
+
gr.Markdown("## 분위기 있는 흑백 사진 변환기")
|
| 48 |
+
|
| 49 |
+
with gr.Row():
|
| 50 |
+
image_input = gr.Image(label="이미지 업로드", type="numpy")
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
gamma_slider = gr.Slider(0.1, 3.0, value=1.0, step=0.1, label="감마 보정")
|
| 54 |
+
brightness_slider = gr.Slider(-100, 100, value=0, step=1, label="밝기 조정")
|
| 55 |
+
contrast_slider = gr.Slider(-100, 100, value=0, step=1, label="대비 조정")
|
| 56 |
+
saturation_slider = gr.Slider(-100, 100, value=0, step=1, label="채도 조정")
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
image_output = gr.Image(label="결과 이미지")
|
| 60 |
+
|
| 61 |
+
with gr.Row():
|
| 62 |
+
download_button = gr.File(label="결과 이미지 다운로드")
|
| 63 |
+
|
| 64 |
+
def update(image, gamma, brightness, contrast, saturation):
|
| 65 |
+
processed_image = main(image, gamma, brightness, contrast, saturation)
|
| 66 |
+
path = save_image(processed_image)
|
| 67 |
+
return processed_image, path
|
| 68 |
+
|
| 69 |
+
demo_func = gr.Interface(
|
| 70 |
+
fn=update,
|
| 71 |
+
inputs=[image_input, gamma_slider, brightness_slider, contrast_slider, saturation_slider],
|
| 72 |
+
outputs=[image_output, download_button]
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
demo.launch()
|