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_titl开发者_Js入门e="图片背景移除") st.write("## 从图片移除背景") st.write( ":dog: 请上传一张图片,去除背景后图片可在侧边栏下载. :grinjs:" ) 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
效果图
到此这篇关于pythonphp实现图片背景移除工具的文章就介绍到这了,更多相关Python图片背景移除内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
精彩评论