Python实现图片背景移除工具
上传一张图片,实现其背景移除的功能。
代码示例:
import streamlit as st
from rembg import remove
from PIL import Image
from io import BytesIO
import base64
st.set_page_config(layout="wide", page_title="图片背景移除")
st.write("## 从图片移除背景")
st.write(
":dog: 请上传一张图片,去除背景后图片可在侧边栏下载. :grin:"
)
st.sidebar.write("## 上传和下载 :gear:")
# Download the fixed image
def convert_image(img):
buf = BytesIO()
img.save(buf, fORMat="PNG")
byte_im = buf.getvalue()
return byte_im
def fix_image(upload):
image = Image.open(upload)
col1.write("原图片 :camera:")
col1.image(image)
fixed = remove(image)
col2.write("处理后 :wrench:")
col2.image(fixed)
st.sidebar.markdown("\n")
st.sidebar.download_button("下载图片", convert_image(fixed), "fixed.png", "image/png")
col1, col2 = st.columns(2)
my_upload = st.sidebar.file_uploader("上传图片", type=["png", "jpg", "jpeg"])
if my_upload is not None:
fix_image(upload=my_upload)
else:
fix_image("./zebra.jpg")
执行
streamlit run bg_remove.py
效果图
到此这篇关于python实现图片背景移除工具的文章就介绍到这了,更多相关Python图片背景移除内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关文章