python實戰(zhàn)之PyQt5實現(xiàn)漫畫臉
最終效果

前言
這是最近在學qt這個東西,然后又學會了調用api,然后就想了用pyqt5做一個GUI界面,后期也可以打包分享給其他人使用,所以就最近就寫了一個簡便的gui界面,有點不好看,大家湊合看一下,主要是學思路的哈!
1.PyQt5的安裝。
1.PyQt5庫的安裝。
PyQt5的安裝有兩個辦法。
1:pip install —xxx(庫名)
2:把這三個庫都下載。

?3:配置qt designer

Name:我們自己給這個tool起的名字
Program:填入designer.exe的路徑,每個人路徑不同,找到自己計算機上的 路徑輸進去。一般來說是在site-packages\QtDesigner\designer.exe這里。
Working directory:填入$ProjectFileDir$,表示文件所在的項目路徑
4:配置轉化工具pyuic

Name:自己起的名字
Program:python.exe所在的位置,一般在Python\Python38-32\python.exe這里
Arguements:填入-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
是將.ui文件轉化成.py文件的語句。
2.qt designer 布局的使用。
1:打開這個設計師

2:創(chuàng)建一個窗口

?3:設計界面,用鼠標拖動左邊的控件。

界面設計完成后點左上角的保存就可以,因為我這個就是一個簡單的界面我就沒有設置布局之類的,如果有強迫癥的小伙伴可以去設置布局哦,在csdn中都有資料的哦。
4:ui轉化為py

qrcTopy這個我說一下,因為這個gui界面沒有用到圖片就沒有qrc轉化py,qrc這個就是把圖片py文件才能在使用ui轉化的py,否則就不可以顯示你在qtdesigners 上面設計的圖片。
5:在python上面運行轉化后的py文件

一般轉化過來的py文件里面,類都是沒有初始化的,這個后面有一個坑,就是文件窗口調用的時候,你得初始化繼承qt中的類才能繼續(xù)使用文件窗口那個功能,小伙伴如果有什么不理解,可以等會看完整的代碼就明白了。
if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
6:我們現(xiàn)在運行py文件就可以看見下面這個GUI界面,所以第一步算是完成了。

3.百度智能云api的調用。
接下來我們來調用百度智能云的api? 。
1.首先我們先打開?百度智能云。
2.
?然后我們找到這個圖像特效

?我么可以查看這個調用方法,然后去獲取免費的調用機會。

主要是獲取到這個AK 和SK,調用的時候需要用到這個東西。如果吊用還不是很懂的話,可以仔細看看官方的調用文檔,接下來話不多說,直接上代碼。
import requests, base64
# 這個函數(shù)的操作是為了獲取access_token參數(shù)
def get_access_token():
url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[AK]&client_secret=[SK]'
res = requests.post(url)
res = res.json()
# print(res)
access_token = res['access_token']
return access_token
def img2Cartoon(path):
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
# 二進制方式打開圖片文件
f = open(path, 'rb')
img1 = base64.b64encode(f.read())
params = {"image":img1}
access_token = get_access_token()
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
print(response.json())
# 對響應結果進行處理
if response:
# 打開一個文件
f = open(path, 'wb')
# 獲取動漫頭像
anime = response.json()['image']
# 對返回的頭像進行解碼
anime = base64.b64decode(anime)
# 將頭像寫入文件當中
f.write(anime)
f.close()
if __name__ == '__main__':
img2Cartoon()
?調用成功后,我們的這個第二步就成功了。
4.調控界面的控件。
接下來我們來給界面的布局建立信號和槽
1.界面一共有三個標簽,兩個按鍵,一個輸入表格。

?查看圖片我們是直接打開窗口選擇文件然后在輸入表格上面顯示,就是文件窗口我們如果要使用的話,我們得給類初始化,并繼承qt中的類,才能正常使用其中的qt調用窗口函數(shù)。
class Ui_MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(Ui_MainWindow, self).__init__()
然后我們分別給按鍵調用函數(shù)功能。
self.pushButton.clicked.connect(self.xians)
self.pushButton_2.clicked.connect(self.zh)
可能還有一些小伙伴沒有看明白,我們直接上源碼吧
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'six.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
import os,sys
from PyQt5.QtWidgets import *
class Ui_MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(Ui_MainWindow, self).__init__()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1062, 652)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(40, 10, 471, 451))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(570, 10, 431, 451))
self.label_2.setObjectName("label_2")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(150, 530, 151, 41))
self.pushButton.setStyleSheet("font: 16pt \"仿宋\";")
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(740, 530, 151, 41))
self.pushButton_2.setStyleSheet("font: 16pt \"仿宋\";")
self.pushButton_2.setObjectName("pushButton_2")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(350, 200, 110, 17))
self.widget.setObjectName("widget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.widget1 = QtWidgets.QWidget(self.centralwidget)
self.widget1.setGeometry(QtCore.QRect(340, 490, 371, 41))
self.widget1.setObjectName("widget1")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.widget1)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_3 = QtWidgets.QLabel(self.widget1)
self.label_3.setStyleSheet("font: 12pt \"隸書\";")
self.label_3.setObjectName("label_3")
self.horizontalLayout_2.addWidget(self.label_3)
self.lineEdit = QtWidgets.QLineEdit(self.widget1)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout_2.addWidget(self.lineEdit)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1062, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "漫畫臉生成"))
self.label.setText(_translate("MainWindow", "原圖"))
self.label_2.setText(_translate("MainWindow", "漫畫臉"))
self.pushButton.setText(_translate("MainWindow", "查看原圖:"))
self.pushButton_2.setText(_translate("MainWindow", "生成漫畫臉"))
self.label_3.setText(_translate("MainWindow", "圖片地址:"))
self.pushButton.clicked.connect(self.xians)
self.pushButton_2.clicked.connect(self.zh)
def xians(self):
self.cwd = os.getcwd()
print(type(self.cwd))
fileName_choose = QFileDialog.getOpenFileName(self, "文件打開",
# 起始路徑
self.cwd,
"All Files (*);;Text Files (*.txt)") # 設置文件擴展名過濾,用雙分號間隔
if fileName_choose[0] == "":
print("\n取消選擇")
return
self.lineEdit.setText(fileName_choose[0])
if os.path.isfile(self.lineEdit.text()) == True:
png = QtGui.QPixmap(self.lineEdit.text())
self.label.setPixmap(png)
self.label.setScaledContents(True)
else:
self.messageDialog()
pass
def messageDialog(self):
mag_box = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, '警告', '文件異常!')
mag_box.exec_()
def zh(self):
import apics #這個就是調用api接口那個函數(shù)
path = self.lineEdit.text()
path = path.replace('\\', '\\\\')
apics.img2Cartoon(path)
png = QtGui.QPixmap(self.lineEdit.text())
self.label_2.setPixmap(png)
self.label_2.setScaledContents(True)
pass
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
5.最終成果

6.總結
到此這篇關于python實戰(zhàn)之PyQt5實現(xiàn)漫畫臉的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python尾遞歸優(yōu)化實現(xiàn)代碼及原理詳解
這篇文章主要介紹了Python尾遞歸優(yōu)化實現(xiàn)代碼及原理詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-10-10
Windows8下安裝Python的BeautifulSoup
這篇文章主要介紹了Windows8下安裝Python的BeautifulSoup,本文著重講解安裝中出現(xiàn)的錯誤和解決方法,需要的朋友可以參考下2015-01-01
python實現(xiàn)搜索指定目錄下文件及文件內搜索指定關鍵詞的方法
這篇文章主要介紹了python實現(xiàn)搜索指定目錄下文件及文件內搜索指定關鍵詞的方法,可實現(xiàn)針對文件夾及文件內關鍵詞的搜索功能,需要的朋友可以參考下2015-06-06

