Sangwon78 commited on
Commit
346e70b
Β·
verified Β·
1 Parent(s): f48a29b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -31
app.py CHANGED
@@ -1,65 +1,60 @@
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
4
- from PIL import Image, ImageEnhance
5
  import io
6
 
 
7
  def apply_filter(image, filter_type, intensity):
8
- image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
9
  if filter_type == "Soft Glow":
10
- result = cv2.GaussianBlur(image, (0, 0), intensity * 2)
11
- result = cv2.addWeighted(image, 1, result, 0.5, 0)
12
  elif filter_type == "Portrait Enhancer":
13
- result = cv2.detailEnhance(image, sigma_s=10, sigma_r=0.15 * intensity)
14
  elif filter_type == "Warm Tone":
15
- warm_filter = np.array([[[1.1, 0.95, 0.8]]])
16
- result = cv2.transform(image, warm_filter) * intensity
17
  elif filter_type == "Cold Tone":
18
- cold_filter = np.array([[[0.9, 1.1, 1.1]]])
19
- result = cv2.transform(image, cold_filter) * intensity
20
  elif filter_type == "High-Key":
21
- result = cv2.add(image, np.array([intensity * 50.0]))
22
  elif filter_type == "Low-Key":
23
- result = cv2.add(image, np.array([-intensity * 50.0]))
24
  elif filter_type == "Haze":
25
- haze = cv2.GaussianBlur(image, (15, 15), intensity)
26
- result = cv2.addWeighted(image, 0.5, haze, 0.5, 0)
27
  elif filter_type == "Monochrome":
28
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
29
- result = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
30
- else:
31
- result = image
32
-
33
- return cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
34
 
 
35
  def process_image(image, filter_type, intensity):
36
  filtered_image = apply_filter(image, filter_type, intensity)
37
- return filtered_image, filtered_image
38
 
39
- def save_image(image, format_choice):
40
  image = Image.fromarray(image)
41
  buf = io.BytesIO()
42
- image.save(buf, format=format_choice)
43
  buf.seek(0)
44
  return buf
45
 
46
  with gr.Blocks() as demo:
47
- gr.Markdown("# 이미지 ν•„ν„° 적용 및 λ‹€μš΄λ‘œλ“œ")
48
  with gr.Row():
49
  with gr.Column():
50
- input_image = gr.Image(label="이미지 μ—…λ‘œλ“œ", type="pil")
51
  filter_choice = gr.Dropdown(
52
  choices=["Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone", "High-Key", "Low-Key", "Haze", "Monochrome"],
53
  value="Soft Glow", label="ν•„ν„° 선택"
54
  )
55
- intensity_slider = gr.Slider(0.1, 2.0, value=1.0, step=0.1, label="ν•„ν„° 강도")
56
- format_choice = gr.Dropdown(choices=["JPG", "PNG", "PDF"], value="JPG", label="λ‹€μš΄λ‘œλ“œ ν˜•μ‹")
 
57
  with gr.Column():
58
- comparison_slider = gr.Image(label="λ³€κ²½ μ „/ν›„ 비ꡐ", type="numpy", interactive=True)
59
- download_button = gr.Button("λ‹€μš΄λ‘œλ“œ")
60
- processed_image = gr.Image(label="ν•„ν„° 적용 κ²°κ³Ό", type="numpy")
61
-
62
- input_image.change(process_image, [input_image, filter_choice, intensity_slider], [comparison_slider, processed_image])
63
- download_button.click(save_image, [processed_image, format_choice], "파일 λ‹€μš΄λ‘œλ“œ")
64
 
65
  demo.launch()
 
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
  image = Image.fromarray(image)
38
  buf = io.BytesIO()
39
+ image.save(buf, format=file_format)
40
  buf.seek(0)
41
  return buf
42
 
43
  with gr.Blocks() as demo:
 
44
  with gr.Row():
45
  with gr.Column():
46
+ input_image = gr.Image(label="μ—…λ‘œλ“œ 이미지", type="numpy")
47
  filter_choice = gr.Dropdown(
48
  choices=["Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone", "High-Key", "Low-Key", "Haze", "Monochrome"],
49
  value="Soft Glow", label="ν•„ν„° 선택"
50
  )
51
+ intensity_slider = gr.Slider(0.1, 2.0, value=1.0, label="ν•„ν„° 강도")
52
+ format_dropdown = gr.Dropdown(choices=["JPEG", "PNG", "PDF"], label="μ €μž₯ ν˜•μ‹")
53
+ save_button = gr.Button("λ‹€μš΄λ‘œλ“œ")
54
  with gr.Column():
55
+ output_image = gr.Image(label="κ²°κ³Ό 이미지", type="numpy")
56
+
57
+ input_image.change(process_image, [input_image, filter_choice, intensity_slider], output_image)
58
+ save_button.click(save_image, [output_image, format_dropdown], outputs=gr.File(label="파일 μ €μž₯"))
 
 
59
 
60
  demo.launch()