栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python --去除人像背景(抠图)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python --去除人像背景(抠图)

注册

https://www.remove.bg/zh/api#api-reference

实操
import time
import requests
import win32ui
import pyautogui
import PySimpleGUI as sg
import os

a = pyautogui.confirm(text='是否现在开始去除人像背景', title='小毅欧巴', buttons=['开始', '关闭'])
if a == '关闭':
    exit()

dlg = win32ui.CreateFileDialog(1)  # 参数 1 表示打开文件对话框
dlg.SetOFNInitialDir('C:')  # 设置打开文件对话框中的初始显示目录
dlg.DoModal()
filename = dlg.GetPathName()
file, ext = os.path.splitext(filename)
if ext not in ('.jpg', '.JPG', '.PNG', '.png'):
    pyautogui.alert(text='文件仅支持(jpg, png)', title='提示', button='关闭')
    exit()

pyautogui.alert(text=filename, title='确认路径', button='我已确认')

key = 'c6vLHWNS7fDFAvoMXk3uo4mB'
# rmbg = RemoveBg(key, "error.log")
# rmbg.remove_background_from_img_file(filename)
new_file_name = os.path.join(os.path.join(os.path.expanduser('~'), "Desktop"), 'no-bg.png')
with open(filename, 'rb') as f:
    response = requests.post(
        'https://api.remove.bg/v1.0/removebg',
        files={'image_file': f},
        data={
            'size': "regular",
            'bg_color': None
        },
        headers={'X-Api-Key': key})
    with open(new_file_name, 'wb') as removed_bg_file:
        removed_bg_file.write(response.content)

mylist = [1, 2, 3, 4, 5, 6, 7, 8]
for i, item in enumerate(mylist):
    sg.one_line_progress_meter('AI', i + 1, len(mylist), '正在智能清除背景')
    time.sleep(0.1)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/740541.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号