Sangwon78 commited on
Commit
d551d91
ยท
verified ยท
1 Parent(s): 06f230d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -16
app.py CHANGED
@@ -5,7 +5,7 @@ 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":
@@ -27,43 +27,47 @@ def apply_filter(image, filter_name, intensity):
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
 
 
31
  def save_image(image, format):
 
32
  if format == "PDF":
33
  pdf = FPDF()
34
  pdf.add_page()
35
- temp_image = "temp.jpg"
36
  image.save(temp_image)
37
  pdf.image(temp_image, x=10, y=10, w=180)
38
- output = io.BytesIO()
39
- pdf.output(dest='S').encode('latin1')
40
- return output.getvalue()
41
  else:
42
- output = io.BytesIO()
43
- image.save(output, format=format)
44
- output.seek(0)
45
- return output
46
 
47
- # Gradio ์•ฑ ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ •
48
  def process_image(image, filter_name, intensity, output_format):
49
  filtered_image = apply_filter(image, filter_name, intensity)
50
  filtered_image_pil = Image.fromarray(cv2.cvtColor(filtered_image, cv2.COLOR_BGR2RGB))
51
- download_file = save_image(filtered_image_pil, output_format)
52
- return filtered_image_pil, (download_file, f"output.{output_format.lower()}")
 
53
 
 
54
  with gr.Blocks() as demo:
55
  with gr.Row():
56
- gr.Markdown("## ์ด๋ฏธ์ง€ ํ•„ํ„ฐ ๋ณ€ํ™˜ ๋ฐ ๋‹ค์šด๋กœ๋“œ ์•ฑ")
57
-
58
  with gr.Row():
59
- input_image = gr.Image(label="์›๋ณธ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ")
60
  filter_name = gr.Dropdown(
61
  choices=["Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone", "High-Key", "Low-Key", "Haze", "Monochrome"],
62
  label="ํ•„ํ„ฐ ์„ ํƒ"
63
  )
64
  intensity = gr.Slider(0, 1, value=0.5, label="ํ•„ํ„ฐ ๊ฐ•๋„")
65
  output_format = gr.Dropdown(choices=["JPG", "PNG", "PDF"], label="ํŒŒ์ผ ํ˜•์‹")
66
-
67
  with gr.Row():
68
  output_image = gr.Image(label="๊ฒฐ๊ณผ ์ด๋ฏธ์ง€")
69
  download_button = gr.File(label="๋‹ค์šด๋กœ๋“œ")
 
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":
 
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="๋‹ค์šด๋กœ๋“œ")