Python+Qt相片更換背景顏色窗體程序的步驟詳解
程序示例精選
Python+Qt相片更換背景顏色窗體程序
如需安裝運(yùn)行環(huán)境或遠(yuǎn)程調(diào)試,由專業(yè)技術(shù)人員遠(yuǎn)程協(xié)助!
前言
QT+Python是非常經(jīng)典的窗體編程組合,功能完善,可視化界面美觀易維護(hù),這篇博客針對(duì)相片更換背景顏色方面編寫代碼,代碼整潔,規(guī)則,易讀,對(duì)學(xué)習(xí)與使用Python有較好的幫助。
一、所需工具軟件
1. Python3.6以上
2. Pycharm代碼編輯器
3. PyQT, OpenCV庫(kù)
二、使用步驟
1.引入庫(kù)
代碼如下(示例):
# -*- coding: utf-8 -*- from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5 import QtWidgets from PyQt5.QtWidgets import * import cv2 as cv import numpy as np import os
2.導(dǎo)入相片
代碼如下(示例):
def on_loadImage_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
self.file_name, self.file_type = QFileDialog.getOpenFileName(self,"打開文件","D:/","All Files (*);")
if (img.shape[2] == 3):#bgr到rgb轉(zhuǎn)換的顯示
qimg = cv.cvtColor(img, cv.COLOR_BGR2RGB)
qimg = QImage(qimg.data, qimg.shape[1], qimg.shape[0], qimg.shape[1]*qimg.shape[2],QImage.Format_RGB888)
self.before.setPixmap(QPixmap.fromImage(qimg))
else:
qimg = QImage(img.data, img.shape[1], img.shape[0],QImage.Format_Grayscale8)
self.before.setPixmap(QPixmap.fromImage(qimg))
該處使用的url網(wǎng)絡(luò)請(qǐng)求的數(shù)據(jù)。
3.顏色選擇:
代碼如下(示例):
def on_colorchoose_currentIndexChanged(self, p0):
current = self.colorchoose.currentText()
if self.target:
if current=="紅色":
self.color =[0, 0, 255]
self.newname= '_red'
elif current=="白色":
self.color =[255, 255, 255]
self.newname= '_white'
elif current=="藍(lán)色":
self.color =[255, 0, 0]
self.newname= '_blue'
elif current=="綠色":
self.color =[0, 255, 0]
self.newname= '_green'
elif current=="黃色":
self.color=[0, 255, 255]
self.newname= '_yellow'
elif current=="紫色":
self.color=[255, 0, 255]
self.newname= '_violet'
elif current=="灰色":
self.color=[96, 96, 96]
self.newname= '_gray'
@pyqtSlot()
def on_change_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
if self.red.text()!="":
self.newname='_define'
print(self.color)
self.target = False
img =cv.imread(self.file_name, -1)
if (img is None):
print(1)
self.textBrowser.setText("打開圖片失敗,請(qǐng)重新加載")
else:
# 準(zhǔn)備數(shù)據(jù)
wide = img.shape[1]
height = img.shape[0]
dims = img.shape[2]
sampleCount = wide * height
clusterCount = 4
points = np.zeros([sampleCount, dims], np.float32)
centers = np.zeros([clusterCount, 1], np.float32)
index = 0
for i in range(height):
for j in range(wide):
index = i * wide + j
for n in range(dims):
points[index][n] = img[i][j][n]
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 10, 0.1)
ret, label, center = cv.kmeans(points, clusterCount, None, criteria, 10, cv.KMEANS_PP_CENTERS)
mask = np.zeros((img.shape[0], img.shape[1]), np.uint8)
index = wide * 2 + 4
img2 = img.copy()
cindex = label[index]
for i in range(height):
for j in range(wide):
index = i * wide + j
label1 = label[index]
if cindex == label1:
for n in range(dims):
img2[i][j][n] = 0;
mask[i][j] = 0;
else:
mask[i][j] = 255
structuting = cv.getStructuringElement(cv.MORPH_RECT, (13, 13))
mask =cv.dilate(mask,structuting)
mask = cv.erode(mask, structuting)
structuting2 = cv.getStructuringElement(cv.MORPH_RECT, (9, 9))
mask = cv.erode(mask, structuting2)
cv.waitKey(10)
img2 = cv.GaussianBlur(img2, (3, 3), 0, 0)
if (img2.shape[2] == 3):
qimg = cv.cvtColor(img2, cv.COLOR_BGR2RGB)
qimg = QImage(qimg.data, qimg.shape[1], qimg.shape[0], qimg.shape[1] * qimg.shape[2],
QImage.Format_RGB888)
self.after.setPixmap(QPixmap.fromImage(qimg))
else:
qimg = QImage(img.data, img.shape[1], img.shape[0], QImage.Format_Grayscale8)
self.after.setPixmap(QPixmap.fromImage(qimg))
name=fathername+"/"+name+self.newname+type
print(name)
cv.imwrite(name,img2)
write = "the image has been saved in:"+name
self.textBrowser.setText(write)
4.運(yùn)行結(jié)果如下:

三、在線協(xié)助:
如需安裝運(yùn)行環(huán)境或遠(yuǎn)程調(diào)試,可以邀請(qǐng)專業(yè)人士!
到此這篇關(guān)于Python+Qt相片更換背景顏色窗體程序的文章就介紹到這了,更多相關(guān)Python+Qt更換背景顏色內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Python對(duì)Syslog信息進(jìn)行分析并繪圖的實(shí)現(xiàn)
這篇文章主要介紹了使用Python對(duì)Syslog信息進(jìn)行分析并繪圖的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Python?PDF轉(zhuǎn)化wolrd代碼的寫法小結(jié)
將PDF文件轉(zhuǎn)換為Word文檔的過(guò)程通常需要使用一些外部庫(kù)來(lái)實(shí)現(xiàn),因?yàn)镻ython本身并不直接支持這種轉(zhuǎn)換,這篇文章主要介紹了Python?PDF轉(zhuǎn)化wolrd代碼的寫法小結(jié),需要的朋友可以參考下2024-06-06
關(guān)于TensorBoard的使用以及遇到的坑記錄
這篇文章主要介紹了關(guān)于TensorBoard的使用以及遇到的坑記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
python密碼學(xué)文件解密實(shí)現(xiàn)教程
這篇文章主要為大家介紹了python密碼學(xué)文件解密實(shí)現(xiàn)教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Python使用FastApi發(fā)送Post請(qǐng)求的基本步驟
FastAPI 是一個(gè)現(xiàn)代、快速(高性能)的 Web 框架,用于構(gòu)建 API,它基于 Python 3.6 及以上版本,在 FastAPI 中發(fā)送 POST 請(qǐng)求,通常是指創(chuàng)建一個(gè)接口來(lái)接收客戶端發(fā)送的 POST 請(qǐng)求,以下是使用 FastAPI 處理 POST 請(qǐng)求的基本步驟,需要的朋友可以參考下2024-09-09

