|
import gradio as gr |
|
from PIL import Image |
|
|
|
def change_background_to_white(image): |
|
try: |
|
|
|
rgba_image = image.convert('RGBA') |
|
except Exception as e: |
|
return None, f"์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}" |
|
|
|
|
|
if rgba_image.width > 3000 or rgba_image.height > 3000: |
|
return None, "๋งค์ฐ ํฐ ์ด๋ฏธ์ง๋ ์ฒ๋ฆฌ ์๊ฐ์ด ๊ธธ์ด์ง ์ ์์ต๋๋ค. ๊ฐ๋ฅํ๋ค๋ฉด ์ด๋ฏธ์ง ํฌ๊ธฐ๋ฅผ ์ค์ฌ์ฃผ์ธ์." |
|
|
|
|
|
white_bg = Image.new('RGBA', rgba_image.size, (255, 255, 255, 255)) |
|
|
|
|
|
pixels = rgba_image.getdata() |
|
|
|
|
|
new_pixels = [] |
|
|
|
|
|
for pixel in pixels: |
|
if pixel[3] < 255: |
|
new_pixels.append((255, 255, 255, 255)) |
|
else: |
|
new_pixels.append(pixel) |
|
|
|
|
|
white_bg.putdata(new_pixels) |
|
|
|
|
|
final_image = white_bg.convert('RGB') |
|
|
|
return final_image, "์ด๋ฏธ์ง ์ฒ๋ฆฌ๊ฐ ์๋ฃ๋์์ต๋๋ค." |
|
|
|
with gr.Blocks() as app: |
|
gr.Markdown("## ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์์ ํฐ์์ผ๋ก ๋ณ๊ฒฝํ๊ธฐ") |
|
with gr.Row(): |
|
input_image = gr.Image(type="pil", label="์ด๋ฏธ์ง ์
๋ก๋") |
|
output_image = gr.Image(label="๋ฐฐ๊ฒฝ์ ๋ณ๊ฒฝ๋ ์ด๋ฏธ์ง") |
|
output_text = gr.Textbox(label="์ฒ๋ฆฌ ๊ฒฐ๊ณผ") |
|
input_image.change(fn=change_background_to_white, inputs=input_image, outputs=[output_image, output_text]) |
|
|
|
if __name__ == "__main__": |
|
app.launch() |