Update app.py
Browse files
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 = "
|
| 36 |
image.save(temp_image)
|
| 37 |
pdf.image(temp_image, x=10, y=10, w=180)
|
| 38 |
-
|
| 39 |
-
pdf.output(
|
| 40 |
-
|
| 41 |
else:
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
return output
|
| 46 |
|
| 47 |
-
#
|
| 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 |
-
|
| 52 |
-
|
|
|
|
| 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="๋ค์ด๋ก๋")
|