Spaces:
Running
Running
import gradio as gr | |
import cv2 | |
import numpy as np | |
import os | |
from PIL import Image | |
def upscale_image(input_image, upscale_factor): | |
try: | |
# PIL Image๋ก ๋ณํ | |
if isinstance(input_image, np.ndarray): | |
input_image = Image.fromarray(input_image) | |
# ํ์ฌ ์ด๋ฏธ์ง ํฌ๊ธฐ | |
current_width, current_height = input_image.size | |
# ์๋ก์ด ํฌ๊ธฐ ๊ณ์ฐ | |
new_width = int(current_width * upscale_factor) | |
new_height = int(current_height * upscale_factor) | |
# ์ด๋ฏธ์ง ๋ฆฌ์ฌ์ด์ฆ | |
output_image = input_image.resize((new_width, new_height), Image.Resampling.LANCZOS) | |
# ์์ ํ์ผ ์ ์ฅ์ ์ํ ๋๋ ํ ๋ฆฌ ํ์ธ ๋ฐ ์์ฑ | |
if not os.path.exists("temp"): | |
os.makedirs("temp") | |
# ๊ฒฐ๊ณผ ์ด๋ฏธ์ง ์ ์ฅ | |
output_path = os.path.join("temp", "upscaled_image.png") | |
output_image.save(output_path, "PNG", quality=100) | |
return output_path | |
except Exception as e: | |
print(f"Error in upscale_image: {str(e)}") | |
raise gr.Error("์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค. ๋ค์ ์๋ํด์ฃผ์ธ์.") | |
# ์ธํฐํ์ด์ค ์ค๋ช | |
DESCRIPTION = """ | |
# ์ด๋ฏธ์ง ์ ์ค์ผ์ผ๋ฌ (Image Upscaler) | |
์ด๋ฏธ์ง์ ํฌ๊ธฐ์ ํ์ง์ ํฅ์์์ผ ๋ณด์ธ์! | |
You can increase the size and quality of your images! | |
## ์ฌ์ฉ ๋ฐฉ๋ฒ (How to use): | |
1. ์ด๋ฏธ์ง๋ฅผ ์ ๋ก๋ํ์ธ์ (Upload an image) | |
2. ์ ์ค์ผ์ผ ๋ ๋ฒจ์ ์ ํํ์ธ์ (Select upscale level) | |
3. Submit ๋ฒํผ์ ํด๋ฆญํ์ธ์ (Click submit) | |
""" | |
# Gradio ์ธํฐํ์ด์ค ์ ์ | |
demo = gr.Interface( | |
fn=upscale_image, | |
inputs=[ | |
gr.Image(label="์ ๋ ฅ ์ด๋ฏธ์ง (Input Image)", type="pil"), | |
gr.Radio( | |
label="์ ์ค์ผ์ผ ๋ ๋ฒจ (Upscale Level)", | |
choices=[2, 4, 6, 8], | |
value=2, | |
type="number" | |
) | |
], | |
outputs=gr.File(label="์ ์ค์ผ์ผ๋ ์ด๋ฏธ์ง ๋ค์ด๋ก๋ (Download Upscaled Image)"), | |
title="Image Upscaler", | |
description=DESCRIPTION, | |
allow_flagging="never", | |
examples=[ | |
["example.jpg", 2] # examples ํด๋์ example.jpg ํ์ผ์ ์ถ๊ฐํด์ผ ํฉ๋๋ค | |
] | |
) | |
# ์ธํฐํ์ด์ค ์คํ | |
demo.launch() |