CSB261 commited on
Commit
eed6196
·
verified ·
1 Parent(s): 89943e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -22
app.py CHANGED
@@ -1,23 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  def sharpen_image(image):
2
- # Sharpen the image using a kernel
3
- kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
4
- sharpened = cv2.filter2D(image, -1, kernel)
5
- return sharpened
6
-
7
- def denoise_image(image):
8
- # Remove noise using GaussianBlur
9
- denoised = cv2.GaussianBlur(image, (5, 5), 0)
10
- return denoised
11
-
12
- def process_image(image):
13
- # Convert to grayscale
14
- grayscale_image = convert_to_grayscale(image)
15
- # Enhance the image
16
- enhanced_image = enhance_image(grayscale_image)
17
- # Apply sharpening
18
- sharpened_image = sharpen_image(enhanced_image)
19
- # Apply denoising
20
- final_image = denoise_image(sharpened_image)
21
- # Save and return the file path
22
- file_path = save_image(final_image)
23
- return final_image, file_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()