CSB261 commited on
Commit
2f92abc
ยท
verified ยท
1 Parent(s): e297ab9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -146
app.py CHANGED
@@ -1,151 +1,40 @@
1
  import gradio as gr
2
- import cv2
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,
11
- gamma,
12
- brightness,
13
- contrast,
14
- saturation):
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)
26
- img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
27
-
28
- # ๋…ธ์ด์ฆˆ ์ œ๊ฑฐ
29
- if denoise:
30
- img = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21)
31
-
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
-
39
- # ๊ฐ๋งˆ ๋ณด์ •
40
- if gamma != 1.0:
41
- invGamma = 1.0 / gamma
42
- table = np.array([((i / 255.0) ** invGamma) * 255
43
- for i in np.arange(256)]).astype("uint8")
44
- img = cv2.LUT(img, table)
45
-
46
- # ๋ฐ๊ธฐ ์กฐ์ •
47
- if brightness != 0:
48
- img = cv2.convertScaleAbs(img, alpha=1, beta=brightness)
49
-
50
- # ๋Œ€๋น„ ์กฐ์ •
51
- if contrast != 1.0:
52
- img = cv2.convertScaleAbs(img, alpha=contrast, beta=0)
53
-
54
- # ์ฑ„๋„ ์กฐ์ •
55
- if saturation != 1.0:
56
- hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV).astype(np.float32)
57
- hsv[...,1] = hsv[...,1] * saturation
58
- hsv[...,1] = np.clip(hsv[...,1], 0, 255)
59
- img = cv2.cvtColor(hsv.astype(np.uint8), cv2.COLOR_HSV2BGR)
60
-
61
- # ํ‘๋ฐฑ ๋ณ€ํ™˜
62
- if convert_bw:
63
- img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
64
- img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
65
-
66
- # ๋ณ€ํ™˜๋œ ์ด๋ฏธ์ง€๋ฅผ PIL ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜
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 ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌ์„ฑ
96
- def create_interface():
97
- with gr.Blocks() as demo:
98
- gr.Markdown("## ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜")
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)
106
- gamma = gr.Slider(label="๊ฐ๋งˆ ๋ณด์ •", minimum=0.1, maximum=3.0, step=0.1, value=1.0)
107
- brightness = gr.Slider(label="๋ฐ๊ธฐ ์กฐ์ •", minimum=-100, maximum=100, step=1, value=0)
108
- contrast = gr.Slider(label="๋Œ€๋น„ ์กฐ์ •", minimum=0.5, maximum=1.5, step=0.1, value=1.0)
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(
122
- fn=process_image,
123
- inputs=[input_image, convert_bw, denoise, sharpen, gamma, brightness, contrast, saturation],
124
- outputs=[transformed_image, download_btn]
125
- )
126
-
127
- # ์›๋ณธ ์ด๋ฏธ์ง€๋ฅผ State์— ์ €์žฅ
128
- input_image.change(
129
- fn=get_original_image,
130
- inputs=input_image,
131
- outputs=original_state
132
- )
133
-
134
- gr.Markdown("### ์ด๋ฏธ์ง€ ๋น„๊ต")
135
-
136
- with gr.Row():
137
- alpha_slider = gr.Slider(label="๋น„๊ต ์Šฌ๋ผ์ด๋”", minimum=0.0, maximum=1.0, step=0.01, value=0.5)
138
- blended_image = gr.Image(type="pil", label="ํ˜ผํ•ฉ๋œ ์ด๋ฏธ๏ฟฝ๏ฟฝ", height=300)
139
-
140
- # ์Šฌ๋ผ์ด๋” ์กฐ์ž‘ ์‹œ ์ด๋ฏธ์ง€ ํ˜ผํ•ฉ
141
- alpha_slider.change(
142
- fn=blend_images,
143
- inputs=[original_state, transformed_image, alpha_slider],
144
- outputs=blended_image
145
- )
146
 
147
- return demo
 
148
 
149
- if __name__ == "__main__":
150
- interface = create_interface()
151
- interface.launch()
 
1
  import gradio as gr
2
+ from PIL import Image, ImageEnhance, ImageOps
3
  import numpy as np
 
 
4
 
5
+ def convert_to_moody_bw(image):
6
+ # ์ด๋ฏธ์ง€๋ฅผ ํ‘๋ฐฑ์œผ๋กœ ๋ณ€ํ™˜
7
+ bw_image = ImageOps.grayscale(image)
8
+
9
+ # ๋Œ€๋น„ ํ–ฅ์ƒ
10
+ enhancer = ImageEnhance.Contrast(bw_image)
11
+ bw_image = enhancer.enhance(1.5)
12
+
13
+ # ๋ฐ๊ธฐ ์กฐ์ ˆ
14
+ enhancer = ImageEnhance.Brightness(bw_image)
15
+ bw_image = enhancer.enhance(0.9)
16
+
17
+ # ์•ฝ๊ฐ„์˜ ํ๋ฆผ ํšจ๊ณผ ์ถ”๊ฐ€
18
+ bw_image = bw_image.filter(Image.Filter.GaussianBlur(radius=1))
19
+
20
+ return bw_image
21
+
22
+ def download_jpg(image):
23
+ # ์ด๋ฏธ์ง€๋ฅผ JPG ํ˜•์‹์œผ๋กœ ์ €์žฅ
24
+ return image.convert("RGB")
25
+
26
+ # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ •
27
+ with gr.Blocks() as demo:
28
+ gr.Markdown("## ๋ถ„์œ„๊ธฐ ์žˆ๋Š” ํ‘๋ฐฑ ์‚ฌ์ง„ ๋ณ€ํ™˜๊ธฐ")
29
+ with gr.Row():
30
+ with gr.Column():
31
+ input_image = gr.Image(type="pil", label="์›๋ณธ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ")
32
+ convert_button = gr.Button("๋ณ€ํ™˜ํ•˜๊ธฐ")
33
+ with gr.Column():
34
+ output_image = gr.Image(type="pil", label="๋ณ€ํ™˜๋œ ์ด๋ฏธ์ง€")
35
+ download_button = gr.Button("JPG๋กœ ๋‹ค์šด๋กœ๋“œ")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
+ convert_button.click(fn=convert_to_moody_bw, inputs=input_image, outputs=output_image)
38
+ download_button.click(fn=download_jpg, inputs=output_image, outputs=gr.File())
39
 
40
+ demo.launch()