1.获取api key
2.安装removebg第三方库:
pip3 install removebg
或者
pip install removebg
3.示例代码
from tkinter import *
from tkinter import filedialog
from PIL import Image
from removebg import RemoveBg
from tkinter.messagebox import showinfo
def openfile():
#这里将API key改为步骤1获得的key
rmbg = RemoveBg('API key', 'error.log')
filename = filedialog.askopenfilename()
rmbg.remove_background_from_img_file(filename)
imagepath = filename+'_no_bg.png'
img = Image.open(imagepath)
img.show()
showinfo('完成生成', f'图片处理完成,路径为: {imagepath}')
#print(filename)
root=Tk()
root.title("抠图小程序")
#窗口大小
root.geometry("300x200")
btn = Button(root, text='打开图片',command=openfile,width=15,height=2) # 绑定方式1: command
btn.place(x=20,y=2,anchor='w')
btn.pack(padx=5, pady=50)
lbl=Label(root,text='-- by 学不会的菜鸟--')
lbl.pack()
root.mainloop()