Update app.py
Browse files
app.py
CHANGED
|
@@ -8,54 +8,63 @@ import io
|
|
| 8 |
|
| 9 |
# νν° μ μ© ν¨μ μ μ
|
| 10 |
def apply_filter(image, filter_name, intensity):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
# μ΄λ―Έμ§ μ μ₯ ν¨μ
|
| 35 |
def save_image(image, output_format):
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
|
| 53 |
# νν° μ μ© λ° λ€μ΄λ‘λ ν¨μ
|
| 54 |
def process_image(image, filter_name, intensity, output_format):
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
|
| 61 |
# Gradio μΈν°νμ΄μ€ μ€μ
|
|
|
|
| 8 |
|
| 9 |
# νν° μ μ© ν¨μ μ μ
|
| 10 |
def apply_filter(image, filter_name, intensity):
|
| 11 |
+
try:
|
| 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 |
+
filtered_image = apply_filter(image, filter_name, intensity)
|
| 63 |
+
filtered_image_pil = Image.fromarray(cv2.cvtColor(filtered_image, cv2.COLOR_BGR2RGB))
|
| 64 |
+
file_data, file_name = save_image(filtered_image_pil, output_format)
|
| 65 |
+
return filtered_image_pil, (file_data, file_name)
|
| 66 |
+
except Exception as e:
|
| 67 |
+
raise ValueError(f"μ΄λ―Έμ§ μ²λ¦¬ μ€ μ€λ₯ λ°μ: {str(e)}")
|
| 68 |
|
| 69 |
|
| 70 |
# Gradio μΈν°νμ΄μ€ μ€μ
|