CSB261 commited on
Commit
b722020
ยท
verified ยท
1 Parent(s): 95de9c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -35
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(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
- return transformed_image, original_image, transformed_image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.Image(type="pil", label="์›๋ณธ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ")
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.DownloadButton("JPG๋กœ ๋‹ค์šด๋กœ๋“œ", file_name="transformed_image.jpg")
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("### ์ด๋ฏธ์ง€ ๋น„๊ต")