Python PyQt5干貨滿滿小項目輕松實現(xiàn)高效摳圖去背景
簡介
結(jié)合學(xué)習(xí)的PyQt5,弄點小項目,做次記錄。
此項目是使用了removebg的API,進(jìn)行實現(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è)置窗口標(biāo)題
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和水平布局的容器進(jìn)行垂直布局
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.美化
只對按鍵進(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.信號與槽綁定
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.界面展示

還可以添加程序圖標(biāo)
到此這篇關(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ù)組中最接近與某值的元素操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python 獲取 datax 執(zhí)行結(jié)果保存到數(shù)據(jù)庫的方法
今天小編就為大家分享一篇Python 獲取 datax 執(zhí)行結(jié)果保存到數(shù)據(jù)庫的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
Python線程池模塊ThreadPoolExecutor用法分析
這篇文章主要介紹了Python線程池模塊ThreadPoolExecutor用法,結(jié)合實例形式分析了Python線程池模塊ThreadPoolExecutor的導(dǎo)入與基本使用方法,需要的朋友可以參考下2018-12-12
Python+wxPython實現(xiàn)自動生成PPTX文檔程序
這篇文章主要介紹了如何使用 wxPython 模塊和 python-pptx 模塊來編寫一個程序,用于生成包含首頁、內(nèi)容頁和感謝頁的 PPTX 文檔,感興趣的小伙伴可以學(xué)習(xí)一下2023-08-08

