Update app.py
Browse files
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(
|
| 30 |
return image
|
| 31 |
|
|
|
|
| 32 |
# μ΄λ―Έμ§ μ μ₯ ν¨μ
|
| 33 |
-
def save_image(image,
|
| 34 |
buffer = io.BytesIO()
|
| 35 |
-
if
|
| 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=
|
| 46 |
buffer.seek(0)
|
| 47 |
-
|
|
|
|
| 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=[
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
| 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(
|