Image_Upscaler / app.py
jpjp9292's picture
Update app.py
088c8d3 verified
import gradio as gr
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="value" # 'number' ์—์„œ 'value'๋กœ ๋ณ€๊ฒฝ
)
],
outputs=gr.File(label="์—…์Šค์ผ€์ผ๋œ ์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ (Download Upscaled Image)"),
title="Image Upscaler",
description=DESCRIPTION,
allow_flagging="never"
)
# ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰
demo.launch()