Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,74 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
# νν° ν¨μ μ μ
|
| 7 |
-
def apply_filter(image,
|
| 8 |
-
image = np.array(image)
|
| 9 |
-
if
|
| 10 |
-
return cv2.addWeighted(image, 1
|
| 11 |
-
elif
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
return cv2.
|
| 23 |
-
elif
|
| 24 |
-
return cv2.
|
| 25 |
-
elif
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
else:
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
def process_image(image,
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
-
# Gradio μΈν°νμ΄μ€ ꡬμ±
|
| 43 |
with gr.Blocks() as demo:
|
| 44 |
-
gr.Markdown("### λ€μν μ¬μ§ νν° μ μ©")
|
| 45 |
-
with gr.Row():
|
| 46 |
-
image_input = gr.Image(label="μ
λ‘λ μ΄λ―Έμ§", type="pil")
|
| 47 |
-
filter_type = gr.Dropdown(
|
| 48 |
-
label="νν° μ ν",
|
| 49 |
-
choices=[
|
| 50 |
-
"Soft Glow", "Portrait Enhancer", "Warm Tone",
|
| 51 |
-
"Cold Tone", "High-Key", "Low-Key", "Haze", "Monochrome"
|
| 52 |
-
],
|
| 53 |
-
value="Soft Glow"
|
| 54 |
-
)
|
| 55 |
-
intensity = gr.Slider(label="νν° κ°λ", minimum=0, maximum=100, value=50)
|
| 56 |
with gr.Row():
|
| 57 |
-
|
| 58 |
-
|
| 59 |
with gr.Row():
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
choices=["
|
| 63 |
-
|
| 64 |
)
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
process_image,
|
| 69 |
-
inputs=[
|
| 70 |
-
outputs=[
|
| 71 |
)
|
| 72 |
|
| 73 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
+
from fpdf import FPDF
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
# νν° ν¨μ μ μ
|
| 8 |
+
def apply_filter(image, filter_name, intensity):
|
| 9 |
+
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 10 |
+
if filter_name == "Soft Glow":
|
| 11 |
+
return cv2.addWeighted(image, 1 - intensity, cv2.GaussianBlur(image, (15, 15), 0), intensity, 0)
|
| 12 |
+
elif filter_name == "Portrait Enhancer":
|
| 13 |
+
return cv2.detailEnhance(image, sigma_s=10, sigma_r=intensity)
|
| 14 |
+
elif filter_name == "Warm Tone":
|
| 15 |
+
warm_filter = np.array([1.2, 1.1, 0.9]) * intensity
|
| 16 |
+
return cv2.convertScaleAbs(image * warm_filter)
|
| 17 |
+
elif filter_name == "Cold Tone":
|
| 18 |
+
cold_filter = np.array([0.9, 1.1, 1.2]) * intensity
|
| 19 |
+
return cv2.convertScaleAbs(image * cold_filter)
|
| 20 |
+
elif filter_name == "High-Key":
|
| 21 |
+
return cv2.addWeighted(image, 1 - intensity, 255 - image, intensity, 0)
|
| 22 |
+
elif filter_name == "Low-Key":
|
| 23 |
+
return cv2.addWeighted(image, 1 - intensity, np.zeros_like(image), intensity, 0)
|
| 24 |
+
elif filter_name == "Haze":
|
| 25 |
+
return cv2.addWeighted(image, 1 - intensity, cv2.GaussianBlur(image, (21, 21), 0), intensity, 0)
|
| 26 |
+
elif filter_name == "Monochrome":
|
| 27 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 28 |
+
return cv2.cvtColor(cv2.addWeighted(gray, intensity, gray, 0, 0), cv2.COLOR_GRAY2BGR)
|
| 29 |
+
|
| 30 |
+
def save_image(image, format):
|
| 31 |
+
if format == "PDF":
|
| 32 |
+
pdf = FPDF()
|
| 33 |
+
pdf.add_page()
|
| 34 |
+
temp_image = "temp.jpg"
|
| 35 |
+
image.save(temp_image)
|
| 36 |
+
pdf.image(temp_image, x=10, y=10, w=180)
|
| 37 |
+
return pdf.output(dest='S').encode('latin1')
|
| 38 |
else:
|
| 39 |
+
image_bytes = io.BytesIO()
|
| 40 |
+
image.save(image_bytes, format=format)
|
| 41 |
+
return image_bytes.getvalue()
|
| 42 |
|
| 43 |
+
# Gradio μ± μΈν°νμ΄μ€ μ€μ
|
| 44 |
+
def process_image(image, filter_name, intensity, output_format):
|
| 45 |
+
filtered_image = apply_filter(image, filter_name, intensity)
|
| 46 |
+
filtered_image_pil = Image.fromarray(cv2.cvtColor(filtered_image, cv2.COLOR_BGR2RGB))
|
| 47 |
+
download_file = save_image(filtered_image_pil, output_format)
|
| 48 |
+
return filtered_image_pil, download_file
|
| 49 |
|
|
|
|
| 50 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
with gr.Row():
|
| 52 |
+
gr.Markdown("## μ΄λ―Έμ§ νν° λ³ν μ±")
|
| 53 |
+
|
| 54 |
with gr.Row():
|
| 55 |
+
input_image = gr.Image(label="μλ³Έ μ΄λ―Έμ§ μ
λ‘λ")
|
| 56 |
+
filter_name = gr.Dropdown(
|
| 57 |
+
choices=["Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone", "High-Key", "Low-Key", "Haze", "Monochrome"],
|
| 58 |
+
label="νν° μ ν"
|
| 59 |
)
|
| 60 |
+
intensity = gr.Slider(0, 1, value=0.5, label="νν° κ°λ")
|
| 61 |
+
output_format = gr.Dropdown(choices=["JPG", "PNG", "PDF"], label="νμΌ νμ")
|
| 62 |
+
|
| 63 |
+
with gr.Row():
|
| 64 |
+
output_image = gr.Image(label="κ²°κ³Ό μ΄λ―Έμ§")
|
| 65 |
+
download_button = gr.File(label="λ€μ΄λ‘λ")
|
| 66 |
+
|
| 67 |
+
process_button = gr.Button("νν° μ μ© λ° λ€μ΄λ‘λ")
|
| 68 |
+
process_button.click(
|
| 69 |
process_image,
|
| 70 |
+
inputs=[input_image, filter_name, intensity, output_format],
|
| 71 |
+
outputs=[output_image, download_button]
|
| 72 |
)
|
| 73 |
|
| 74 |
demo.launch()
|