CSB261 commited on
Commit
7aba3fe
ยท
verified ยท
1 Parent(s): 264e399

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -28
app.py CHANGED
@@ -3,9 +3,8 @@ import cv2
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,12 +15,11 @@ def process_image(file,
16
  """
17
  ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜
18
  """
19
- if file is None:
20
  return None, None
21
 
22
- # Read the binary data
23
- image = Image.open(BytesIO(file)).convert("RGB")
24
- original_image = image.copy()
25
 
26
  # Convert PIL Image to OpenCV format
27
  img = np.array(image)
@@ -34,7 +32,7 @@ def process_image(file,
34
  # ์ƒคํ”„๋‹
35
  if sharpen:
36
  kernel = np.array([[0, -1, 0],
37
- [-1, 5,-1],
38
  [0, -1, 0]])
39
  img = cv2.filter2D(img, -1, kernel)
40
 
@@ -69,36 +67,29 @@ def process_image(file,
69
  transformed_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
70
  transformed_image_pil = Image.fromarray(transformed_image)
71
 
72
- # Prepare the download file
73
  buf = BytesIO()
74
  transformed_image_pil.save(buf, format="JPEG")
75
- buf.seek(0)
76
 
77
- # Set the file name
78
- # Extract original file name from the uploaded file
79
- # Since gr.File with type='binary' provides bytes, the filename needs to be extracted separately
80
- # For simplicity, set a default filename
81
- original_filename = "transformed_image.jpg"
82
- buf.name = original_filename
83
 
84
- return transformed_image_pil, buf
85
-
86
- def blend_images(original, transformed, alpha):
87
  """
88
  ๋‘ ์ด๋ฏธ์ง€๋ฅผ ์•ŒํŒŒ ๊ฐ’์— ๋”ฐ๋ผ ํ˜ผํ•ฉํ•˜๋Š” ํ•จ์ˆ˜
89
  """
90
- if original is None or transformed is None:
91
  return None
92
- blended = Image.blend(original, transformed, alpha)
93
  return blended
94
 
95
- def get_original_image(file):
96
  """
97
- Get the original image as PIL.Image
98
  """
99
- if file is None:
100
  return None
101
- image = Image.open(BytesIO(file)).convert("RGB")
102
  return image
103
 
104
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌ์„ฑ
@@ -108,7 +99,7 @@ def create_interface():
108
 
109
  with gr.Row():
110
  with gr.Column(scale=1):
111
- input_image = gr.File(type="binary", label="์›๋ณธ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ")
112
  convert_bw = gr.Checkbox(label="ํ‘๋ฐฑ์œผ๋กœ ๋ณ€ํ™˜", value=True)
113
  denoise = gr.Checkbox(label="๋…ธ์ด์ฆˆ ์ œ๊ฑฐ", value=False)
114
  sharpen = gr.Checkbox(label="์ƒคํ”„๋‹", value=False)
@@ -118,13 +109,13 @@ def create_interface():
118
  saturation = gr.Slider(label="์ฑ„๋„ ์กฐ์ •", minimum=0.0, maximum=2.0, step=0.1, value=1.0)
119
  submit = gr.Button("๋ณ€ํ™˜ํ•˜๊ธฐ")
120
 
121
- # State๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›๋ณธ ๋ฐ ๋ณ€ํ™˜๋œ ์ด๋ฏธ์ง€ ์ €์žฅ
122
  original_state = gr.State()
123
  transformed_state = gr.State()
124
 
125
  with gr.Column(scale=1):
126
  transformed_image = gr.Image(type="pil", label="๋ณ€ํ™˜๋œ ์ด๋ฏธ์ง€")
127
- download_btn = gr.File(label="JPG๋กœ ๋‹ค์šด๋กœ๋“œ")
128
 
129
  # ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ State์— ์ €์žฅํ•˜๊ณ  ๋ณ€ํ™˜๋œ ์ด๋ฏธ์ง€ ๋ฐ ๋‹ค์šด๋กœ๋“œ ํŒŒ์ผ ํ‘œ์‹œ
130
  submit.click(
@@ -133,7 +124,7 @@ def create_interface():
133
  outputs=[transformed_image, download_btn]
134
  )
135
 
136
- # ์›๋ณธ ์ด๋ฏธ์ง€๋ฅผ ์ƒํƒœ์— ์ €์žฅ
137
  input_image.change(
138
  fn=get_original_image,
139
  inputs=input_image,
 
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,
 
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)
 
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
 
 
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 ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌ์„ฑ
 
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)
 
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(
 
124
  outputs=[transformed_image, download_btn]
125
  )
126
 
127
+ # ์›๋ณธ ์ด๋ฏธ์ง€๋ฅผ State์— ์ €์žฅ
128
  input_image.change(
129
  fn=get_original_image,
130
  inputs=input_image,