Sangwon78 commited on
Commit
7d3fad5
Β·
verified Β·
1 Parent(s): 0a8d396

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -15
app.py CHANGED
@@ -5,7 +5,8 @@ from fpdf import FPDF
5
  from PIL import Image
6
  import io
7
 
8
- # ν•„ν„° 적용 ν•¨μˆ˜
 
9
  def apply_filter(image, filter_name, intensity):
10
  image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
11
  if filter_name == "Soft Glow":
@@ -26,51 +27,57 @@ def apply_filter(image, filter_name, intensity):
26
  return cv2.addWeighted(image, 1 - intensity, cv2.GaussianBlur(image, (21, 21), 0), intensity, 0)
27
  elif filter_name == "Monochrome":
28
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
29
- return cv2.cvtColor(cv2.addWeighted(gray, intensity, gray, 0, 0), cv2.COLOR_GRAY2BGR)
30
  return image
31
 
 
32
  # 이미지 μ €μž₯ ν•¨μˆ˜
33
- def save_image(image, format):
34
  buffer = io.BytesIO()
35
- if format == "PDF":
36
  pdf = FPDF()
37
  pdf.add_page()
38
  temp_image = "temp_image.jpg"
39
  image.save(temp_image)
40
  pdf.image(temp_image, x=10, y=10, w=180)
41
- buffer = io.BytesIO()
42
  pdf.output(buffer, "S")
43
  buffer.seek(0)
 
44
  else:
45
- image.save(buffer, format=format)
46
  buffer.seek(0)
47
- return buffer.getvalue()
 
48
 
49
- # ν•„ν„° 처리 및 κ²°κ³Ό λ°˜ν™˜
 
50
  def process_image(image, filter_name, intensity, output_format):
51
  filtered_image = apply_filter(image, filter_name, intensity)
52
  filtered_image_pil = Image.fromarray(cv2.cvtColor(filtered_image, cv2.COLOR_BGR2RGB))
53
- file_data = save_image(filtered_image_pil, output_format)
54
- file_name = f"output.{output_format.lower()}"
55
  return filtered_image_pil, (file_data, file_name)
56
 
 
57
  # Gradio μΈν„°νŽ˜μ΄μŠ€ μ„€μ •
58
  with gr.Blocks() as demo:
59
  with gr.Row():
60
- gr.Markdown("## 이미지 ν•„ν„° 적용 및 λ‹€μš΄λ‘œλ“œ μ•±")
61
 
62
  with gr.Row():
63
  input_image = gr.Image(label="이미지 μ—…λ‘œλ“œ", type="pil")
64
  filter_name = gr.Dropdown(
65
- choices=["Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone", "High-Key", "Low-Key", "Haze", "Monochrome"],
66
- label="ν•„ν„° 선택"
 
 
 
67
  )
68
  intensity = gr.Slider(0, 1, value=0.5, label="ν•„ν„° 강도")
69
- output_format = gr.Dropdown(choices=["JPG", "PNG", "PDF"], label="파일 ν˜•μ‹")
70
 
71
  with gr.Row():
72
  output_image = gr.Image(label="κ²°κ³Ό 이미지")
73
- download_button = gr.File(label="λ‹€μš΄λ‘œλ“œ")
74
 
75
  process_button = gr.Button("ν•„ν„° 적용 및 λ‹€μš΄λ‘œλ“œ")
76
  process_button.click(
 
5
  from PIL import Image
6
  import io
7
 
8
+
9
+ # ν•„ν„° 적용 ν•¨μˆ˜ μ •μ˜
10
  def apply_filter(image, filter_name, intensity):
11
  image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
12
  if filter_name == "Soft Glow":
 
27
  return cv2.addWeighted(image, 1 - intensity, cv2.GaussianBlur(image, (21, 21), 0), intensity, 0)
28
  elif filter_name == "Monochrome":
29
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
30
+ return cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
31
  return image
32
 
33
+
34
  # 이미지 μ €μž₯ ν•¨μˆ˜
35
+ def save_image(image, output_format):
36
  buffer = io.BytesIO()
37
+ if output_format == "PDF":
38
  pdf = FPDF()
39
  pdf.add_page()
40
  temp_image = "temp_image.jpg"
41
  image.save(temp_image)
42
  pdf.image(temp_image, x=10, y=10, w=180)
 
43
  pdf.output(buffer, "S")
44
  buffer.seek(0)
45
+ return buffer.getvalue(), "output.pdf"
46
  else:
47
+ image.save(buffer, format=output_format)
48
  buffer.seek(0)
49
+ file_name = f"output.{output_format.lower()}"
50
+ return buffer.getvalue(), file_name
51
 
52
+
53
+ # ν•„ν„° 적용 및 λ‹€μš΄λ‘œλ“œ ν•¨μˆ˜
54
  def process_image(image, filter_name, intensity, output_format):
55
  filtered_image = apply_filter(image, filter_name, intensity)
56
  filtered_image_pil = Image.fromarray(cv2.cvtColor(filtered_image, cv2.COLOR_BGR2RGB))
57
+ file_data, file_name = save_image(filtered_image_pil, output_format)
 
58
  return filtered_image_pil, (file_data, file_name)
59
 
60
+
61
  # Gradio μΈν„°νŽ˜μ΄μŠ€ μ„€μ •
62
  with gr.Blocks() as demo:
63
  with gr.Row():
64
+ gr.Markdown("## 이미지 ν•„ν„° 및 λ‹€μš΄λ‘œλ“œ μ• ν”Œλ¦¬μΌ€μ΄μ…˜")
65
 
66
  with gr.Row():
67
  input_image = gr.Image(label="이미지 μ—…λ‘œλ“œ", type="pil")
68
  filter_name = gr.Dropdown(
69
+ choices=[
70
+ "Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone",
71
+ "High-Key", "Low-Key", "Haze", "Monochrome"
72
+ ],
73
+ label="ν•„ν„° 선택",
74
  )
75
  intensity = gr.Slider(0, 1, value=0.5, label="ν•„ν„° 강도")
76
+ output_format = gr.Dropdown(choices=["JPG", "PNG", "PDF"], label="파일 ν˜•μ‹ 선택")
77
 
78
  with gr.Row():
79
  output_image = gr.Image(label="κ²°κ³Ό 이미지")
80
+ download_button = gr.File(label="κ²°κ³Ό λ‹€μš΄λ‘œλ“œ")
81
 
82
  process_button = gr.Button("ν•„ν„° 적용 및 λ‹€μš΄λ‘œλ“œ")
83
  process_button.click(