[Python] Do it yourself to implement an automatic matting applet with an interface

I introduced how to use python to cut out the background of the picture, but it also has certain limitations. For example, we have to put the picture to be cut out to the specified path, and then modify the name of the cut out in the program, etc. , These are many constraints.

The following code improves the program. After it runs, we can choose the picture to remove the background, and it has interface windows and buttons. Does it look more professional? ! You can also filter according to different image formats, it looks like a small program, let's take a look~

 

Interface Cutout Program

1. Get a sneak peek

First look at the effect, which is the white window in the picture below 

2. Implementation principle

Depends Tkinter to achieve local window , Tkinter is Python's standard TK GUI toolkit connection port, which can be used in most Unix platforms, the same can be applied to both Windows and Macintosh systems. Subsequent versions of Tk80 can implement native window styles and run well on most platforms.

The code part is not very complicated

Emphasize: The API interface key inside is mentioned in the previous blog. The steps to obtain it are also very simple. You only need to log in and register to find your exclusive API key: https://blog.csdn.net/ weixin_44566432/article/details/107840109

Including the program introduction of the cutout, the following is a good understanding, mainly to set the window properties, and complete the function of selecting and generating pictures.

import os
from tkinter import Tk, Menu, Label, Button
from tkinter.filedialog import askopenfilenames
from tkinter.messagebox import showinfo
from removebg import RemoveBg

rmbg = RemoveBg('你的API接口密钥!!!', 'error.log')#!!!

def remove_bg(img_path):
    rmbg.remove_background_from_img_file(img_path)

IMGPATH = ''

class GUI(object):
    def __init__(self, window):
        self.window = window
        self.window.title('去除图片背景')
        self.window.geometry('300x200')
        menubar = Menu(self.window)

        #定义空菜单
        filemenu = Menu(menubar, tearoff=0)
        filemenu.add_command(label='帮助', command=self.helpme)
        filemenu.add_separator()

        #显示
        self.l = Label(window, text='')
        self.l.pack(padx=5, pady=10) #固定窗口

        #选择照片
        btn1 = Button(window, text='选择照片', width=15, height=2, command=self.get_img)
        btn1.pack()

        #生成图片
        self.send_btn = Button(window, text='去除背景', width=15, height=2, command=self.gen_img)
        self.send_btn.pack()

    def helpme(self):
        showinfo('帮助', '请关注公众号,联系作者')

    #选择图片
    def get_img(self):
        global IMGPATH
        #选择文件
        filenames = askopenfilenames(filetypes=(('jpeg img', '*.jpeg'),
                                                ('jpg img', '*.jpg'), ('png img', "*.png")
                                                ))
        if len(filenames) > 0:
            fnlist = [fn for fn in filenames]
            fnstr = '\n'.join(fnlist)
            self.l.config(text=fnstr)
            IMGPATH = fnlist
        else:
            self.l.config(text='目前没有选择任何图片文件')
    
    #生成图片
    def gen_img(self):
        global IMGPATH
        respathlist = []
        for imgpath in IMGPATH:
            filepath, tempfilename = os.path.split(imgpath)
            filename, extension = os.path.splitext(tempfilename)
            remove_bg(imgpath)
            respathlist.append(imgpath)
        respath = ' '.join(respathlist)
        showinfo('完成生成', f'图片处理完成,路径为: {respath}')

#创建窗口
window = Tk()
GUI(window)
#显示窗口,必须在所有控件后
window.mainloop()

If you have any questions, please leave a message to discuss~

If there is something wrong, please correct me, thank you!

 

Guess you like

Origin blog.csdn.net/weixin_44566432/article/details/107836371
Recommended