Python PyQt5干貨滿滿小項(xiàng)目輕松實(shí)現(xiàn)高效摳圖去背景
簡(jiǎn)介
結(jié)合學(xué)習(xí)的PyQt5,弄點(diǎn)小項(xiàng)目,做次記錄。
此項(xiàng)目是使用了removebg的API,進(jìn)行實(shí)現(xiàn)摳圖功能,將人物的背景扣去。將次功能封裝到桌面上。
1.獲取API
先打開(kāi)removebg的網(wǎng)站
點(diǎn)擊上面的工具和API
再點(diǎn)擊API Docs
最后點(diǎn)擊Get API Key,當(dāng)然要先登錄
2.API使用方法
在API Docs 下面有使用方法
3.可視化桌面制作
def ui_init(self): self.setWindowTitle('摳圖') # 設(shè)置窗口標(biāo)題 self.resize(610,500) # 設(shè)置窗口大小 self.button = QPushButton('選擇圖片') '''兩個(gè)放置圖片的Qlable''' self.before_lable = QLabel() self.before_lable.setToolTip('原來(lái)的圖片') # 設(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('原來(lái)的圖片') # 設(shè)置提示信息
使用setScaledContents方法設(shè)置在before_lable上的圖片自適應(yīng)
self.before_lable.setScaledContents(True)
使用setFrameShape方法設(shè)置QLable的形狀,因?yàn)镼Frame是QLable的基類(lèi),所以可以使用QFrame的方法
self.before_lable.setFrameShape(QFrame.Panel|QFrame.Plain)
樣式組合表
窗口布局是由:
兩個(gè)QLable和一個(gè)QFrame水平布局
在有Qpushbutton和水平布局的容器進(jìn)行垂直布局
4.邏輯實(shí)現(xiàn)
def file(self): fname,a = QFileDialog.getOpenFileName(self,'打開(kāi)文件','.','圖像文件(*.jpg *.png)') # 用來(lái)選擇文件 if fname: self.before_lable.setPixmap(QPixmap(fname)) # 將原來(lái)的圖片顯示在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,選擇本地文件。此方法返回兩個(gè)值,第一個(gè)值才是我們需要的。
fname,a = QFileDialog.getOpenFileName(self,'打開(kāi)文件','.','圖像文件(*.jpg *.png)') # 用來(lái)選擇文件
5.美化
只對(duì)按鍵進(jìn)行了美化
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.信號(hào)與槽綁定
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('原來(lái)的圖片') 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,'打開(kāi)文件','.','圖像文件(*.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.界面展示
還可以添加程序圖標(biāo)
到此這篇關(guān)于Python PyQt5干貨滿滿小項(xiàng)目輕松實(shí)現(xiàn)高效摳圖去背景的文章就介紹到這了,更多相關(guān)Python PyQt5 摳圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在python3中實(shí)現(xiàn)查找數(shù)組中最接近與某值的元素操作
今天小編就為大家分享一篇在python3中實(shí)現(xiàn)查找數(shù)組中最接近與某值的元素操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02Flask數(shù)據(jù)庫(kù)遷移簡(jiǎn)單介紹
這篇文章主要為大家詳細(xì)介紹了Flask數(shù)據(jù)庫(kù)遷移簡(jiǎn)單工作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10講解Python中運(yùn)算符使用時(shí)的優(yōu)先級(jí)
這篇文章主要介紹了講解Python中運(yùn)算符使用時(shí)的優(yōu)先級(jí),是Python學(xué)習(xí)當(dāng)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-05-05Python 獲取 datax 執(zhí)行結(jié)果保存到數(shù)據(jù)庫(kù)的方法
今天小編就為大家分享一篇Python 獲取 datax 執(zhí)行結(jié)果保存到數(shù)據(jù)庫(kù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07Python線程池模塊ThreadPoolExecutor用法分析
這篇文章主要介紹了Python線程池模塊ThreadPoolExecutor用法,結(jié)合實(shí)例形式分析了Python線程池模塊ThreadPoolExecutor的導(dǎo)入與基本使用方法,需要的朋友可以參考下2018-12-12Python+wxPython實(shí)現(xiàn)自動(dòng)生成PPTX文檔程序
這篇文章主要介紹了如何使用 wxPython 模塊和 python-pptx 模塊來(lái)編寫(xiě)一個(gè)程序,用于生成包含首頁(yè)、內(nèi)容頁(yè)和感謝頁(yè)的 PPTX 文檔,感興趣的小伙伴可以學(xué)習(xí)一下2023-08-08