Sangwon78 commited on
Commit
428752b
Β·
verified Β·
1 Parent(s): 6a41505

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -90
app.py CHANGED
@@ -1,102 +1,65 @@
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
4
- from fpdf import FPDF
5
- from PIL import Image, UnidentifiedImageError
6
  import io
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- # ν•„ν„° 적용 ν•¨μˆ˜ μ •μ˜
10
- def apply_filter(image, filter_name, intensity):
11
- try:
12
- image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
13
- if filter_name == "Soft Glow":
14
- return cv2.addWeighted(image, 1 - intensity, cv2.GaussianBlur(image, (15, 15), 0), intensity, 0)
15
- elif filter_name == "Portrait Enhancer":
16
- return cv2.detailEnhance(image, sigma_s=10, sigma_r=intensity)
17
- elif filter_name == "Warm Tone":
18
- warm_filter = np.array([1.2, 1.1, 0.9]) * intensity
19
- return cv2.convertScaleAbs(image * warm_filter)
20
- elif filter_name == "Cold Tone":
21
- cold_filter = np.array([0.9, 1.1, 1.2]) * intensity
22
- return cv2.convertScaleAbs(image * cold_filter)
23
- elif filter_name == "High-Key":
24
- return cv2.addWeighted(image, 1 - intensity, 255 - image, intensity, 0)
25
- elif filter_name == "Low-Key":
26
- return cv2.addWeighted(image, 1 - intensity, np.zeros_like(image), intensity, 0)
27
- elif filter_name == "Haze":
28
- return cv2.addWeighted(image, 1 - intensity, cv2.GaussianBlur(image, (21, 21), 0), intensity, 0)
29
- elif filter_name == "Monochrome":
30
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
31
- return cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
32
- return image
33
- except Exception as e:
34
- raise ValueError(f"ν•„ν„° 적용 쀑 였λ₯˜ λ°œμƒ: {str(e)}")
35
 
 
 
 
 
 
 
36
 
37
- # 이미지 μ €μž₯ ν•¨μˆ˜
38
- def save_image(image, output_format):
39
- try:
40
- buffer = io.BytesIO()
41
- if output_format == "PDF":
42
- pdf = FPDF()
43
- pdf.add_page()
44
- temp_image = "temp_image.jpg"
45
- image.save(temp_image)
46
- pdf.image(temp_image, x=10, y=10, w=180)
47
- pdf.output(buffer, "S")
48
- buffer.seek(0)
49
- return buffer.getvalue(), "output.pdf"
50
- else:
51
- image.save(buffer, format=output_format)
52
- buffer.seek(0)
53
- file_name = f"output.{output_format.lower()}"
54
- return buffer.getvalue(), file_name
55
- except Exception as e:
56
- raise ValueError(f"이미지 μ €μž₯ 쀑 였λ₯˜ λ°œμƒ: {str(e)}")
57
-
58
-
59
- # ν•„ν„° 적용 및 λ‹€μš΄λ‘œλ“œ ν•¨μˆ˜
60
- def process_image(image, filter_name, intensity, output_format):
61
- try:
62
- if image is None:
63
- raise ValueError("이미지가 μ—…λ‘œλ“œλ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.")
64
- filtered_image = apply_filter(image, filter_name, intensity)
65
- filtered_image_pil = Image.fromarray(cv2.cvtColor(filtered_image, cv2.COLOR_BGR2RGB))
66
- file_data, file_name = save_image(filtered_image_pil, output_format)
67
- return filtered_image_pil, (file_data, file_name)
68
- except UnidentifiedImageError:
69
- raise ValueError("μ˜¬λ°”λ₯΄μ§€ μ•Šμ€ 이미지 ν˜•μ‹μž…λ‹ˆλ‹€. JPG, PNG ν˜•μ‹μ˜ 이미지λ₯Ό μ—…λ‘œλ“œν•΄μ£Όμ„Έμš”.")
70
- except Exception as e:
71
- raise ValueError(f"이미지 처리 쀑 였λ₯˜ λ°œμƒ: {str(e)}")
72
-
73
-
74
- # Gradio μΈν„°νŽ˜μ΄μŠ€ μ„€μ •
75
  with gr.Blocks() as demo:
 
76
  with gr.Row():
77
- gr.Markdown("## 이미지 ν•„ν„° 및 λ‹€μš΄λ‘œλ“œ μ• ν”Œλ¦¬μΌ€μ΄μ…˜")
78
-
79
- with gr.Row():
80
- input_image = gr.Image(label="이미지 μ—…λ‘œλ“œ", type="pil", tool="editor")
81
- filter_name = gr.Dropdown(
82
- choices=[
83
- "Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone",
84
- "High-Key", "Low-Key", "Haze", "Monochrome"
85
- ],
86
- label="ν•„ν„° 선택",
87
- )
88
- intensity = gr.Slider(0, 1, value=0.5, label="ν•„ν„° 강도")
89
- output_format = gr.Dropdown(choices=["JPG", "PNG", "PDF"], label="파일 ν˜•μ‹ 선택")
90
-
91
- with gr.Row():
92
- output_image = gr.Image(label="κ²°κ³Ό 이미지")
93
- download_button = gr.File(label="κ²°κ³Ό λ‹€μš΄λ‘œλ“œ")
94
-
95
- process_button = gr.Button("ν•„ν„° 적용 및 λ‹€μš΄λ‘œλ“œ")
96
- process_button.click(
97
- process_image,
98
- inputs=[input_image, filter_name, intensity, output_format],
99
- outputs=[output_image, download_button]
100
- )
101
 
102
  demo.launch()
 
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()