removing_bg / app.py
meebox's picture
share the app.
56c4cd8
Raw
History Blame Contribute Delete
1.25 kB
from rembg import remove # 匯入移除背景的函式
from PIL import Image # 匯入 pillow 影像操作模組
import gradio as gr
def remove_bg(img, x_off, y_off, scale):
x = int(x_off * img.width / 100) # 計算橫向位移像數
y = int(y_off * img.height / 100) # 計算縱向位移像數
width = img.width - x # 計算實際寬度
height = img.height - y # 計算實際高度
size = min(width, height) # 取寬高較短者
img = img.crop((x, y, x + size, y + size)) # 切割正方形區域
if scale < 100:
size = int(size * scale / 100)# 依照指定比例縮放
img = img.resize((size, size))
img_nb = remove(img) # 移除背景
img.save('img.png') # 儲存影像檔
img_nb.save('img_nb.png') # 儲存去背影像檔
return (img, img_nb)
no_bg_if = gr.Interface(
fn=remove_bg,
inputs=[
gr.Image(label='輸入影像', type='pil'),
gr.Slider(label='橫向切割位移百分比'),
gr.Slider(label='縱向切割位移百分比'),
gr.Slider(label='縮放比例', value=100, step=5)],
outputs=[gr.Gallery(label='處理後影像')]
)
no_bg_if.launch(share=True)