Sangwon78 commited on
Commit
7d57d7a
ยท
verified ยท
1 Parent(s): 8637a9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -44
app.py CHANGED
@@ -1,61 +1,68 @@
1
  import gradio as gr
 
2
  import cv2
3
  import numpy as np
4
- from PIL import Image
5
- import io
6
 
7
- # ํ•„ํ„ฐ ์ ์šฉ ํ•จ์ˆ˜
8
- def apply_filter(image, filter_type, intensity):
9
- image = np.array(image)
 
10
  if filter_type == "Soft Glow":
11
- blur = cv2.GaussianBlur(image, (15, 15), intensity)
12
- image = cv2.addWeighted(image, 1.0, blur, 0.5, 0)
13
  elif filter_type == "Portrait Enhancer":
14
- image = cv2.detailEnhance(image, sigma_s=10, sigma_r=0.15 * intensity)
15
  elif filter_type == "Warm Tone":
16
- image = cv2.transform(image, np.array([[[1.1, 0.95, 0.8]]]) * intensity)
17
  elif filter_type == "Cold Tone":
18
- image = cv2.transform(image, np.array([[[0.9, 1.1, 1.2]]]) * intensity)
19
  elif filter_type == "High-Key":
20
- image = cv2.add(image, np.array([intensity * 50.0]))
21
  elif filter_type == "Low-Key":
22
- image = cv2.add(image, np.array([-intensity * 50.0]))
23
  elif filter_type == "Haze":
24
- haze = cv2.GaussianBlur(image, (int(5 * intensity), int(5 * intensity)), 0)
25
- image = cv2.addWeighted(image, 0.5, haze, 0.5, 0)
26
  elif filter_type == "Monochrome":
27
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
28
- image = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
29
- return image
30
 
31
- # Gradio ์ธํ„ฐํŽ˜์ด์Šค ํ•จ์ˆ˜
32
- def process_image(image, filter_type, intensity):
33
- filtered_image = apply_filter(image, filter_type, intensity)
34
- return filtered_image
 
 
 
35
 
36
- def save_image(image, file_format):
37
- # ์ด๋ฏธ์ง€๋ฅผ ์ €์žฅํ•  ์ž„์‹œ ํŒŒ์ผ ์ƒ์„ฑ
38
- image = Image.fromarray(image)
39
- temp_file = f"output.{file_format.lower()}"
40
- image.save(temp_file, format=file_format.upper())
41
- return temp_file
42
 
43
- # Gradio ์ธํ„ฐํŽ˜์ด์Šค
44
  with gr.Blocks() as demo:
 
45
  with gr.Row():
46
- with gr.Column():
47
- input_image = gr.Image(label="์—…๋กœ๋“œ ์ด๋ฏธ์ง€", type="numpy")
48
- filter_choice = gr.Dropdown(
49
- choices=["Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone", "High-Key", "Low-Key", "Haze", "Monochrome"],
50
- value="Soft Glow", label="ํ•„ํ„ฐ ์„ ํƒ"
51
- )
52
- intensity_slider = gr.Slider(0.1, 2.0, value=1.0, label="ํ•„ํ„ฐ ๊ฐ•๋„")
53
- format_dropdown = gr.Dropdown(choices=["JPEG", "PNG"], label="์ €์žฅ ํ˜•์‹")
54
- save_button = gr.Button("๋‹ค์šด๋กœ๋“œ")
55
- with gr.Column():
56
- output_image = gr.Image(label="๊ฒฐ๊ณผ ์ด๋ฏธ์ง€", type="numpy")
57
-
58
- input_image.change(process_image, [input_image, filter_choice, intensity_slider], output_image)
59
- save_button.click(save_image, [output_image, format_dropdown], outputs=gr.File(label="ํŒŒ์ผ ์ €์žฅ"))
60
-
61
- demo.launch()
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from PIL import Image, ImageEnhance, ImageOps
3
  import cv2
4
  import numpy as np
 
 
5
 
6
+ # ํ•„ํ„ฐ ๊ธฐ๋Šฅ ๊ตฌํ˜„
7
+ def apply_filter(image, filter_type, intensity):
8
+ img = np.array(image)
9
+
10
  if filter_type == "Soft Glow":
11
+ gaussian = cv2.GaussianBlur(img, (15, 15), 0)
12
+ img = cv2.addWeighted(img, 1 - intensity, gaussian, intensity, 0)
13
  elif filter_type == "Portrait Enhancer":
14
+ img = cv2.bilateralFilter(img, 9, 75, 75)
15
  elif filter_type == "Warm Tone":
16
+ img[:, :, 0] = cv2.add(img[:, :, 0], int(50 * intensity)) # Red ์ฆ๊ฐ€
17
  elif filter_type == "Cold Tone":
18
+ img[:, :, 2] = cv2.add(img[:, :, 2], int(50 * intensity)) # Blue ์ฆ๊ฐ€
19
  elif filter_type == "High-Key":
20
+ img = cv2.convertScaleAbs(img, alpha=1 + intensity, beta=int(30 * intensity))
21
  elif filter_type == "Low-Key":
22
+ img = cv2.convertScaleAbs(img, alpha=1 - intensity, beta=int(-30 * intensity))
23
  elif filter_type == "Haze":
24
+ img = cv2.addWeighted(img, 1 - intensity, np.full(img.shape, 255, dtype=np.uint8), intensity, 0)
 
25
  elif filter_type == "Monochrome":
26
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
27
+ img = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
 
28
 
29
+ return Image.fromarray(img)
30
+
31
+ # ํŒŒ์ผ ์ €์žฅ ๊ธฐ๋Šฅ
32
+ def save_file(image, format):
33
+ file_path = f"processed_image.{format.lower()}"
34
+ image.save(file_path, format=format)
35
+ return file_path
36
 
37
+ # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ •
38
+ def process_image(image, filter_type, intensity, file_format):
39
+ filtered_image = apply_filter(image, filter_type, intensity)
40
+ download_path = save_file(filtered_image, file_format)
41
+ return filtered_image, download_path
 
42
 
43
+ # UI ๊ตฌ์„ฑ
44
  with gr.Blocks() as demo:
45
+ gr.Markdown("# ์ด๋ฏธ์ง€ ํ•„ํ„ฐ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜")
46
  with gr.Row():
47
+ image_input = gr.Image(type="pil", label="์ด๋ฏธ์ง€ ์—…๋กœ๋“œ")
48
+ filter_selector = gr.Dropdown(
49
+ ["Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone",
50
+ "High-Key", "Low-Key", "Haze", "Monochrome"],
51
+ label="ํ•„ํ„ฐ ์„ ํƒ"
52
+ )
53
+ intensity_slider = gr.Slider(0.0, 1.0, value=0.5, label="ํ•„ํ„ฐ ๊ฐ•๋„")
54
+ file_format_selector = gr.Radio(["JPG", "PNG", "PDF"], label="ํŒŒ์ผ ํ˜•์‹ ์„ ํƒ")
55
+ output_image = gr.Image(label="๊ฒฐ๊ณผ ์ด๋ฏธ์ง€")
56
+ download_button = gr.File(label="๋‹ค์šด๋กœ๋“œ")
57
+
58
+ # ๋™์ž‘ ์—ฐ๊ฒฐ
59
+ submit_button = gr.Button("ํ•„ํ„ฐ ์ ์šฉ")
60
+ submit_button.click(
61
+ process_image,
62
+ inputs=[image_input, filter_selector, intensity_slider, file_format_selector],
63
+ outputs=[output_image, download_button]
64
+ )
65
+
66
+ # ์‹คํ–‰
67
+ if __name__ == "__main__":
68
+ demo.launch()