Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use PyQt5 to make cartoon face GUI interface in Python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use PyQt5 to make comic face GUI interface in Python. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Installation of 1.PyQt5. Installation of the 1.PyQt5 library.

There are two ways to install PyQt5.

Pip install-xxx (library name)

Download all three libraries.

Configure qt designer

Name: we named this tool ourselves.

Program: fill in the path of designer.exe. Each person's path is different. Find the path on your computer and enter it. Generally speaking, it is here in site-packages\ QtDesigner\ designer.exe.

Working directory: enter $ProjectFileDir$, to indicate the project path where the file is located

Configure the conversion tool pyuic

Name: name yourself.

The location where Program:python.exe is located, usually at Python\ Python38-32\ python.exe.

Arguements: enter-m PyQt5.uic.pyuic $FileName$-o $FileNameWithoutExtension$.py

Is a statement that converts a .ui file into a .py file.

The use of 2.qt designer layout. 1: open the designer

2: create a window

3: design the interface and drag the control on the left with the mouse.

After the completion of the interface design, you can save the upper left corner of the interface, because this is a simple interface, I did not set the layout and so on, if you have obsessive-compulsive disorder, you can set the layout, oh, there are materials in csdn.

Convert 4:ui to py

QrcTopy this I say, because this gui interface does not use pictures, there is no qrc to convert py,qrc this is the picture py files can be converted in the use of ui py, otherwise you can not display the pictures you designed on qtdesigners.

5: run the converted py file on python

Generally converted to the py file, the classes are not initialized, there is a pit behind this, that is, when the file window is called, you have to initialize and inherit the class in qt before you can continue to use the function of the file window. If you don't understand anything, you can read the complete code later.

If _ _ name__ = ='_ main__': app = QtWidgets.QApplication (sys.argv) MainWindow = QtWidgets.QMainWindow () ui = Ui_MainWindow () ui.setupUi (MainWindow) MainWindow.show () sys.exit (app.exec_ ()) 6: run the py file

Now that we run the py file, we can see the following GUI interface, so the first step is done.

3. Baidu Intelligent Cloud api call.

Next, let's call the api of Baidu Smart Cloud.

1. First of all, let's open Baidu Smart Cloud.

two。

And then we found this image special effect.

We can look at this calling method and get a free call opportunity.

The main thing is to get the AK and SK, which you need to use when calling. If you don't know how to use it, you can take a closer look at the official call documentation, and then go straight to the code without saying much.

Import requests Base64 # this function operates to get the access_token parameter 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" # binary mode to open the picture file 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 ()) # process the response result if response: # Open a file f = open (path) 'wb') # get the animation avatar anime = response.json () [' image'] # decode the returned avatar anime = base64.b64decode (anime) # write the avatar to the file f.write (anime) f.close () if _ _ name__ ='_ main__': img2Cartoon ()

After the call is successful, our second step is successful.

4. Controls that regulate the interface.

Next, let's set up signals and slots for the layout of the interface.

1. The interface has a total of three tags, two buttons and one input form.

To view the picture, we directly open the window to select the file and display it on the input form, that is, the file window if we want to use it, we have to initialize the class and inherit the class in qt in order to use the qt to call the window function normally.

Class Ui_MainWindow (QtWidgets.QMainWindow): def _ init__ (self): super (Ui_MainWindow, self). _ _ init__ ()

Then we call the function to the keystroke separately.

Self.pushButton.clicked.connect (self.xians) self.pushButton_2.clicked.connect (self.zh)

There may still be some friends who don't understand. Let's go directly to the source code.

#-*-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, QtWidgetsimport os,sysfrom 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 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) 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\" imitating Song\ " ") 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\ "imitating Song\" ") 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\" official script\ " 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") "comic face generation") self.label.setText (_ translate ("MainWindow", "original")) self.label_2.setText (_ translate ("MainWindow", "comic face")) self.pushButton.setText (_ translate ("MainWindow", "View original:") self.pushButton_2.setText (_ translate ("MainWindow") ) self.label_3.setText (_ translate ("MainWindow", "picture address:")) 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) "File Open", # start path self.cwd, "All Files (*) Text Files (* .txt) ") # set file extension filtering Double semicolon interval if fileName_choose [0] = "": print ("\ ndeselect") 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) 'warning', 'file exception!') Mag_box.exec_ () def zh (self): import apics # this is the function path = self.lineEdit.text () path = path.replace ('\\') that calls the api interface. 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. Final result

On "how to use PyQt5 in Python to make comic face GUI interface" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report