Update app.py
Browse files
app.py
CHANGED
|
@@ -1,102 +1,65 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
-
from
|
| 5 |
-
from PIL import Image, UnidentifiedImageError
|
| 6 |
import io
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 13 |
-
if filter_name == "Soft Glow":
|
| 14 |
-
return cv2.addWeighted(image, 1 - intensity, cv2.GaussianBlur(image, (15, 15), 0), intensity, 0)
|
| 15 |
-
elif filter_name == "Portrait Enhancer":
|
| 16 |
-
return cv2.detailEnhance(image, sigma_s=10, sigma_r=intensity)
|
| 17 |
-
elif filter_name == "Warm Tone":
|
| 18 |
-
warm_filter = np.array([1.2, 1.1, 0.9]) * intensity
|
| 19 |
-
return cv2.convertScaleAbs(image * warm_filter)
|
| 20 |
-
elif filter_name == "Cold Tone":
|
| 21 |
-
cold_filter = np.array([0.9, 1.1, 1.2]) * intensity
|
| 22 |
-
return cv2.convertScaleAbs(image * cold_filter)
|
| 23 |
-
elif filter_name == "High-Key":
|
| 24 |
-
return cv2.addWeighted(image, 1 - intensity, 255 - image, intensity, 0)
|
| 25 |
-
elif filter_name == "Low-Key":
|
| 26 |
-
return cv2.addWeighted(image, 1 - intensity, np.zeros_like(image), intensity, 0)
|
| 27 |
-
elif filter_name == "Haze":
|
| 28 |
-
return cv2.addWeighted(image, 1 - intensity, cv2.GaussianBlur(image, (21, 21), 0), intensity, 0)
|
| 29 |
-
elif filter_name == "Monochrome":
|
| 30 |
-
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 31 |
-
return cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
|
| 32 |
-
return image
|
| 33 |
-
except Exception as e:
|
| 34 |
-
raise ValueError(f"νν° μ μ© μ€ μ€λ₯ λ°μ: {str(e)}")
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
# μ΄λ―Έμ§ μ μ₯ ν¨μ
|
| 38 |
-
def save_image(image, output_format):
|
| 39 |
-
try:
|
| 40 |
-
buffer = io.BytesIO()
|
| 41 |
-
if output_format == "PDF":
|
| 42 |
-
pdf = FPDF()
|
| 43 |
-
pdf.add_page()
|
| 44 |
-
temp_image = "temp_image.jpg"
|
| 45 |
-
image.save(temp_image)
|
| 46 |
-
pdf.image(temp_image, x=10, y=10, w=180)
|
| 47 |
-
pdf.output(buffer, "S")
|
| 48 |
-
buffer.seek(0)
|
| 49 |
-
return buffer.getvalue(), "output.pdf"
|
| 50 |
-
else:
|
| 51 |
-
image.save(buffer, format=output_format)
|
| 52 |
-
buffer.seek(0)
|
| 53 |
-
file_name = f"output.{output_format.lower()}"
|
| 54 |
-
return buffer.getvalue(), file_name
|
| 55 |
-
except Exception as e:
|
| 56 |
-
raise ValueError(f"μ΄λ―Έμ§ μ μ₯ μ€ μ€λ₯ λ°μ: {str(e)}")
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
# νν° μ μ© λ° λ€μ΄λ‘λ ν¨μ
|
| 60 |
-
def process_image(image, filter_name, intensity, output_format):
|
| 61 |
-
try:
|
| 62 |
-
if image is None:
|
| 63 |
-
raise ValueError("μ΄λ―Έμ§κ° μ
λ‘λλμ§ μμμ΅λλ€.")
|
| 64 |
-
filtered_image = apply_filter(image, filter_name, intensity)
|
| 65 |
-
filtered_image_pil = Image.fromarray(cv2.cvtColor(filtered_image, cv2.COLOR_BGR2RGB))
|
| 66 |
-
file_data, file_name = save_image(filtered_image_pil, output_format)
|
| 67 |
-
return filtered_image_pil, (file_data, file_name)
|
| 68 |
-
except UnidentifiedImageError:
|
| 69 |
-
raise ValueError("μ¬λ°λ₯΄μ§ μμ μ΄λ―Έμ§ νμμ
λλ€. JPG, PNG νμμ μ΄λ―Έμ§λ₯Ό μ
λ‘λν΄μ£ΌμΈμ.")
|
| 70 |
-
except Exception as e:
|
| 71 |
-
raise ValueError(f"μ΄λ―Έμ§ μ²λ¦¬ μ€ μ€λ₯ λ°μ: {str(e)}")
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
# Gradio μΈν°νμ΄μ€ μ€μ
|
| 75 |
with gr.Blocks() as demo:
|
|
|
|
| 76 |
with gr.Row():
|
| 77 |
-
gr.
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
label="
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
output_image = gr.Image(label="κ²°κ³Ό μ΄λ―Έμ§")
|
| 93 |
-
download_button = gr.File(label="κ²°κ³Ό λ€μ΄λ‘λ")
|
| 94 |
-
|
| 95 |
-
process_button = gr.Button("νν° μ μ© λ° λ€μ΄λ‘λ")
|
| 96 |
-
process_button.click(
|
| 97 |
-
process_image,
|
| 98 |
-
inputs=[input_image, filter_name, intensity, output_format],
|
| 99 |
-
outputs=[output_image, download_button]
|
| 100 |
-
)
|
| 101 |
|
| 102 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
+
from PIL import Image, ImageEnhance
|
|
|
|
| 5 |
import io
|
| 6 |
|
| 7 |
+
def apply_filter(image, filter_type, intensity):
|
| 8 |
+
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 9 |
+
if filter_type == "Soft Glow":
|
| 10 |
+
result = cv2.GaussianBlur(image, (0, 0), intensity * 2)
|
| 11 |
+
result = cv2.addWeighted(image, 1, result, 0.5, 0)
|
| 12 |
+
elif filter_type == "Portrait Enhancer":
|
| 13 |
+
result = cv2.detailEnhance(image, sigma_s=10, sigma_r=0.15 * intensity)
|
| 14 |
+
elif filter_type == "Warm Tone":
|
| 15 |
+
warm_filter = np.array([[[1.1, 0.95, 0.8]]])
|
| 16 |
+
result = cv2.transform(image, warm_filter) * intensity
|
| 17 |
+
elif filter_type == "Cold Tone":
|
| 18 |
+
cold_filter = np.array([[[0.9, 1.1, 1.1]]])
|
| 19 |
+
result = cv2.transform(image, cold_filter) * intensity
|
| 20 |
+
elif filter_type == "High-Key":
|
| 21 |
+
result = cv2.add(image, np.array([intensity * 50.0]))
|
| 22 |
+
elif filter_type == "Low-Key":
|
| 23 |
+
result = cv2.add(image, np.array([-intensity * 50.0]))
|
| 24 |
+
elif filter_type == "Haze":
|
| 25 |
+
haze = cv2.GaussianBlur(image, (15, 15), intensity)
|
| 26 |
+
result = cv2.addWeighted(image, 0.5, haze, 0.5, 0)
|
| 27 |
+
elif filter_type == "Monochrome":
|
| 28 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 29 |
+
result = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
|
| 30 |
+
else:
|
| 31 |
+
result = image
|
| 32 |
+
|
| 33 |
+
return cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
|
| 34 |
|
| 35 |
+
def process_image(image, filter_type, intensity):
|
| 36 |
+
filtered_image = apply_filter(image, filter_type, intensity)
|
| 37 |
+
return filtered_image, filtered_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
def save_image(image, format_choice):
|
| 40 |
+
image = Image.fromarray(image)
|
| 41 |
+
buf = io.BytesIO()
|
| 42 |
+
image.save(buf, format=format_choice)
|
| 43 |
+
buf.seek(0)
|
| 44 |
+
return buf
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
with gr.Blocks() as demo:
|
| 47 |
+
gr.Markdown("# μ΄λ―Έμ§ νν° μ μ© λ° λ€μ΄λ‘λ")
|
| 48 |
with gr.Row():
|
| 49 |
+
with gr.Column():
|
| 50 |
+
input_image = gr.Image(label="μ΄λ―Έμ§ μ
λ‘λ", type="pil")
|
| 51 |
+
filter_choice = gr.Dropdown(
|
| 52 |
+
choices=["Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone", "High-Key", "Low-Key", "Haze", "Monochrome"],
|
| 53 |
+
value="Soft Glow", label="νν° μ ν"
|
| 54 |
+
)
|
| 55 |
+
intensity_slider = gr.Slider(0.1, 2.0, value=1.0, step=0.1, label="νν° κ°λ")
|
| 56 |
+
format_choice = gr.Dropdown(choices=["JPG", "PNG", "PDF"], value="JPG", label="λ€μ΄λ‘λ νμ")
|
| 57 |
+
with gr.Column():
|
| 58 |
+
comparison_slider = gr.Image(label="λ³κ²½ μ /ν λΉκ΅", type="numpy", interactive=True)
|
| 59 |
+
download_button = gr.Button("λ€μ΄λ‘λ")
|
| 60 |
+
processed_image = gr.Image(label="νν° μ μ© κ²°κ³Ό", type="numpy")
|
| 61 |
+
|
| 62 |
+
input_image.change(process_image, [input_image, filter_choice, intensity_slider], [comparison_slider, processed_image])
|
| 63 |
+
download_button.click(save_image, [processed_image, format_choice], "νμΌ λ€μ΄λ‘λ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
demo.launch()
|