CSB261 commited on
Commit
8cf5f12
ยท
verified ยท
1 Parent(s): 9fb516c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -16
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
- # ์ด๋ฏธ์ง€๋ฅผ RGB(3์ฐจ์›)์ธ์ง€, ํ‘๋ฐฑ(2์ฐจ์›)์ธ์ง€ ํŒ๋ณ„
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, # multichannel=True ๋Œ€์‹  channel_axis=-1
35
  fast_mode=True
36
  )
37
  else:
38
- # ํ‘๋ฐฑ(2์ฐจ์›) ์ด๋ฏธ์ง€์˜ ๊ฒฝ์šฐ channel_axis=None
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, # multichannel=False ๋Œ€์‹  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
- # 1) ๋ณ€ํ™˜๋œ ์ด๋ฏธ์ง€ (PIL)
99
  transformed = process_image(
100
  input_image,
101
  denoise_strength,
@@ -106,7 +107,7 @@ def generate_output(
106
  saturation
107
  )
108
 
109
- # 2) ๋‹ค์šด๋กœ๋“œ ๊ฐ€๋Šฅํ•œ JPG ํŒŒ์ผ ์ƒ์„ฑ
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
- # ๋ณ€ํ™˜๋œ ์ด๋ฏธ์ง€๋ฅผ Gradio์˜ Image๋กœ ๋ณด์—ฌ์ฃผ๊ณ , ๋‹ค์šด๋กœ๋“œ๋„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ๋” ๋‘ ๊ฐ€์ง€๋ฅผ ๋ฐ˜ํ™˜
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
- input_w, input_h = input_image.size
212
- transformed_w, transformed_h = transformed.size
 
 
 
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
- # ์›๋ณธ(RGB), ๋ณ€ํ™˜๋ณธ(RGB)
218
- new_image.paste(input_image.convert("RGB"), (0, 0))
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