亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python PyQt5干貨滿滿小項目輕松實現(xiàn)高效摳圖去背景

 更新時間:2021年11月09日 11:40:40   作者:不俠居  
PyQt5以一套Python模塊的形式來實現(xiàn)功能。它包含了超過620個類,600個方法和函數(shù)。本篇文章手把手帶你用PyQt5輕松實現(xiàn)圖片扣除背景,大家可以在過程中查缺補漏,提升水平

簡介

結(jié)合學(xué)習(xí)的PyQt5,弄點小項目,做次記錄。
此項目是使用了removebg的API,進行實現(xiàn)摳圖功能,將人物的背景扣去。將次功能封裝到桌面上。

1.獲取API

先打開removebg的網(wǎng)站

在這里插入圖片描述

點擊上面的工具和API

在這里插入圖片描述

再點擊API Docs

在這里插入圖片描述

最后點擊Get API Key,當(dāng)然要先登錄

在這里插入圖片描述

在這里插入圖片描述

2.API使用方法

在API Docs 下面有使用方法

在這里插入圖片描述

3.可視化桌面制作

def ui_init(self):
        self.setWindowTitle('摳圖') # 設(shè)置窗口標題
        self.resize(610,500) # 設(shè)置窗口大小

        self.button = QPushButton('選擇圖片')

		'''兩個放置圖片的Qlable'''
        self.before_lable = QLabel()
        self.before_lable.setToolTip('原來的圖片') # 設(shè)置提示信息
        self.before_lable.resize(300,400)
        self.before_lable.setScaledContents(True) # 設(shè)置圖片自適應(yīng)窗口大小
        self.before_lable.setFrameShape(QFrame.Panel|QFrame.Plain)
        self.after_lable = QLabel()
        self.after_lable.setToolTip('處理后的圖片') # 設(shè)置提示信息	
        self.after_lable.resize(300,400)	
        self.after_lable.setScaledContents(True) # 設(shè)置圖片自適應(yīng)窗口大小
        self.after_lable.setFrameShape(QFrame.Panel|QFrame.Plain)
		
		'''一條線'''
        self.frame = QFrame()
        self.frame.setFrameShape(QFrame.VLine|QFrame.Plain)

		'''窗口布局'''
        self.h_layout = QHBoxLayout()
        self.v_layout = QVBoxLayout()
        self.h_layout.addWidget(self.before_lable)
        self.h_layout.addWidget(self.frame)
        self.h_layout.addWidget(self.after_lable)
        self.v_layout.addWidget(self.button)
        self.v_layout.addLayout(self.h_layout)
        self.widget = QWidget()
        self.setCentralWidget(self.widget)
        self.widget.setLayout(self.v_layout)

使用setToolTip方法設(shè)置提示信息

self.before_lable.setToolTip('原來的圖片') # 設(shè)置提示信息

使用setScaledContents方法設(shè)置在before_lable上的圖片自適應(yīng)

self.before_lable.setScaledContents(True)

使用setFrameShape方法設(shè)置QLable的形狀,因為QFrame是QLable的基類,所以可以使用QFrame的方法

self.before_lable.setFrameShape(QFrame.Panel|QFrame.Plain)

樣式組合表

在這里插入圖片描述

窗口布局是由:
兩個QLable和一個QFrame水平布局
在有Qpushbutton和水平布局的容器進行垂直布局

4.邏輯實現(xiàn)

def file(self):
        fname,a = QFileDialog.getOpenFileName(self,'打開文件','.','圖像文件(*.jpg *.png)') # 用來選擇文件
        if fname:
            self.before_lable.setPixmap(QPixmap(fname)) # 將原來的圖片顯示在before_lable控件上

			'''API調(diào)用'''
            response = requests.post(
            'https://api.remove.bg/v1.0/removebg',
            files={'image_file': open(fname, 'rb')},
            data={'size': 'auto'},
            headers={'X-Api-Key': '你們自己的API Key'},
            )
            if response.status_code == requests.codes.ok:
                with open('no-bg.png', 'wb') as f:
                    f.write(response.content)
                try:
                    self.after_lable.setPixmap(QPixmap('./no-bg.png'))
                except FileNotFoundError:
                    pass 

用QFileDialog.getOpenFileName,選擇本地文件。此方法返回兩個值,第一個值才是我們需要的。

fname,a = QFileDialog.getOpenFileName(self,'打開文件','.','圖像文件(*.jpg *.png)') # 用來選擇文件

5.美化

只對按鍵進行了美化

def qss_init(self):
        qss = '''
        QPushButton{
                border-radius: 6px;
                border: none;
                height: 25px;
                color: white;
                background: rgb(57, 58, 60);
            }

        QPushButton:enabled:hover{
                background: rgb(230, 39, 39);
            }
        QPushButton:enabled:pressed{
                background: rgb(255, 0, 0);
            }
            '''
        self.setStyleSheet(qss) # 加載樣式

6.信號與槽綁定

def connect_init(self):
        self.button.clicked.connect(self.file) 

7.全部代碼

import sys 
from PyQt5.QtWidgets import QApplication, QPushButton, QStatusBar, QWidget, QFileDialog, QLabel, QHBoxLayout, QVBoxLayout, QFrame, QMainWindow
from PyQt5.QtGui import QPixmap
import requests
class removebg(QMainWindow):
    def __init__(self):
        super(removebg,self).__init__()
        self.ui_init()
        self.qss_init()
        self.connect_init()

    def ui_init(self):
        self.setWindowTitle('摳圖')
        self.resize(610,500)

        self.button = QPushButton('選擇圖片')
        self.before_lable = QLabel()
        self.before_lable.setToolTip('原來的圖片')
        self.before_lable.resize(300,400)
        self.before_lable.setScaledContents(True) # 設(shè)置圖片自適應(yīng)窗口大小
        self.before_lable.setFrameShape(QFrame.Panel|QFrame.Plain)
        self.after_lable = QLabel()
        self.after_lable.setToolTip('處理后的圖片')	
        self.after_lable.resize(300,400)	
        self.after_lable.setScaledContents(True) # 設(shè)置圖片自適應(yīng)窗口大小
        self.after_lable.setFrameShape(QFrame.Panel|QFrame.Plain)


        self.frame = QFrame()
        self.frame.setFrameShape(QFrame.VLine|QFrame.Plain)
        self.h_layout = QHBoxLayout()
        self.v_layout = QVBoxLayout()
        self.h_layout.addWidget(self.before_lable)
        self.h_layout.addWidget(self.frame)
        self.h_layout.addWidget(self.after_lable)
        self.v_layout.addWidget(self.button)
        self.v_layout.addLayout(self.h_layout)
        self.widget = QWidget()
        self.setCentralWidget(self.widget)
        self.widget.setLayout(self.v_layout)

    def file(self):
        fname,a = QFileDialog.getOpenFileName(self,'打開文件','.','圖像文件(*.jpg *.png)')
        if fname:
            self.before_lable.setPixmap(QPixmap(fname))


            response = requests.post(
            'https://api.remove.bg/v1.0/removebg',
            files={'image_file': open(fname, 'rb')},
            data={'size': 'auto'},
            headers={'X-Api-Key': '7Uuo8dhdTHwSXUdjhKZP7h9c'},
            )
            if response.status_code == requests.codes.ok:
                with open('no-bg.png', 'wb') as f:
                    f.write(response.content)
                try:
                    self.after_lable.setPixmap(QPixmap('./no-bg.png'))
                except FileNotFoundError:
                    pass 
    
    def connect_init(self):
        self.button.clicked.connect(self.file)        

    def qss_init(self):
        qss = '''
        QPushButton{
                border-radius: 6px;
                border: none;
                height: 25px;
                color: white;
                background: rgb(57, 58, 60);
            }

        QPushButton:enabled:hover{
                background: rgb(230, 39, 39);
            }
        QPushButton:enabled:pressed{
                background: rgb(255, 0, 0);
            }
            '''
        self.setStyleSheet(qss)



if __name__ == '__main__':
    app = QApplication(sys.argv)
    dispaly = removebg()
    dispaly.show()
    sys.exit(app.exec_())

8.界面展示

在這里插入圖片描述

還可以添加程序圖標

到此這篇關(guān)于Python PyQt5干貨滿滿小項目輕松實現(xiàn)高效摳圖去背景的文章就介紹到這了,更多相關(guān)Python PyQt5 摳圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 在python3中實現(xiàn)查找數(shù)組中最接近與某值的元素操作

    在python3中實現(xiàn)查找數(shù)組中最接近與某值的元素操作

    今天小編就為大家分享一篇在python3中實現(xiàn)查找數(shù)組中最接近與某值的元素操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Flask數(shù)據(jù)庫遷移簡單介紹

    Flask數(shù)據(jù)庫遷移簡單介紹

    這篇文章主要為大家詳細介紹了Flask數(shù)據(jù)庫遷移簡單工作,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • python?如何去除字符串中指定字符

    python?如何去除字符串中指定字符

    python中的strip()可以去除頭尾指定字符,只能刪除頭尾指定字符,想要去除中間字符,可以使用replace()函數(shù),本文結(jié)合示例代碼給大家介紹的非常詳細,需要的朋友參考下吧
    2022-12-12
  • 講解Python中運算符使用時的優(yōu)先級

    講解Python中運算符使用時的優(yōu)先級

    這篇文章主要介紹了講解Python中運算符使用時的優(yōu)先級,是Python學(xué)習(xí)當(dāng)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-05-05
  • 在numpy矩陣中令小于0的元素改為0的實例

    在numpy矩陣中令小于0的元素改為0的實例

    今天小編就為大家分享一篇在numpy矩陣中令小于0的元素改為0的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python 獲取 datax 執(zhí)行結(jié)果保存到數(shù)據(jù)庫的方法

    Python 獲取 datax 執(zhí)行結(jié)果保存到數(shù)據(jù)庫的方法

    今天小編就為大家分享一篇Python 獲取 datax 執(zhí)行結(jié)果保存到數(shù)據(jù)庫的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python加速器numba使用詳解

    python加速器numba使用詳解

    本文主要介紹了python加速器numba使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • Python線程池模塊ThreadPoolExecutor用法分析

    Python線程池模塊ThreadPoolExecutor用法分析

    這篇文章主要介紹了Python線程池模塊ThreadPoolExecutor用法,結(jié)合實例形式分析了Python線程池模塊ThreadPoolExecutor的導(dǎo)入與基本使用方法,需要的朋友可以參考下
    2018-12-12
  • Python+wxPython實現(xiàn)自動生成PPTX文檔程序

    Python+wxPython實現(xiàn)自動生成PPTX文檔程序

    這篇文章主要介紹了如何使用 wxPython 模塊和 python-pptx 模塊來編寫一個程序,用于生成包含首頁、內(nèi)容頁和感謝頁的 PPTX 文檔,感興趣的小伙伴可以學(xué)習(xí)一下
    2023-08-08
  • Python正則簡單實例分析

    Python正則簡單實例分析

    這篇文章主要介紹了Python正則簡單實例,具體分析了Python針對字符串的簡單正則匹配測試中遇到的問題與相關(guān)注意事項,需要的朋友可以參考下
    2017-03-03

最新評論