In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the "Python how to load and display pictures from the GUI interface" related knowledge, editor through the actual case to show you the process of operation, the method of operation is simple and fast, practical, I hope that this "Python how to load and display pictures from the GUI interface" article can help you solve the problem.
[sample code]
# coding=utf-8# @ Auther: "Pengge thieves are excellent" # @ Date: 2019 @ Software: PyCharm
From PyQt5 import QtCore, QtGui, QtWidgetsimport sysimport requests
Class Ui_Dialog (QtWidgets.QWidget): def _ init__ (self): super (Ui_Dialog, self). _ _ init__ ()
Def setupUi (self, Dialog): Dialog.setObjectName ("Dialog") Dialog.resize (561,431) self.pushButton = QtWidgets.QPushButton (Dialog) self.pushButton.setGeometry (QtCore.QRect (70,60,75,23) self.pushButton.setObjectName ("pushButton") self.label = QtWidgets.QLabel (Dialog) self.label.setGeometry (QtCore.QRect (100,110,341) 231) self.label.setObjectName ("label")
Self.retranslateUi (Dialog) QtCore.QMetaObject.connectSlotsByName (Dialog)
Def retranslateUi (self, Dialog): _ translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle (_ translate ("Dialog", "Dialog")) self.pushButton.setText (_ translate ("Dialog", "Open Picture")) self.label.setText (_ translate ("Dialog") "Show picture") self.pushButton.clicked.connect (self.show_urlimage) # load picture from local computer def show_localimage (self): imgName, imgType = QtWidgets.QFileDialog.getOpenFileName (self, "Open picture", "", "," *. Jpg " ; *. Png All Files (*) ") pix = QtGui.QPixmap (imgName) jpg = pix.scaled (self.label.width () Self.label.height () self.label.setPixmap (jpg) # load pictures directly from the website def show_urlimage (self): url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1566126255397&di=e6038a03911121946883ce68aad6474e&imgtype=0&src=http%3A%2F%2Fnews.mydrivers.com%2FImg%2F20120227%2F2012022709173765.jpg" r = requests.get (url) print (r.content) ) with open ("1.jpg" "wb") as f: f.write (r.content) pix = QtGui.QPixmap () pix.loadFromData (r.content) jpg = pix.scaled (self.label.width (), self.label.height ()) self.label.setPixmap (jpg)
If _ _ name__ = = "_ main__": app = QtWidgets.QApplication (sys.argv) main = QtWidgets.QDialog () mainwindow = Ui_Dialog () mainwindow.setupUi (main) main.show () sys.exit (app.exec ())
[effect]
1. Load locally:
2. Load pictures on the website
[knowledge points]
1. What controls are used to display pictures in pyqt5? In addition, what is the process of loading the code that displays the picture?
A: the sample code uses the label control, and it seems possible to see Graphics components in other blogs. In addition, the code flow of image loading is as follows (take the local image as an example):
Where pix.scaled () is used to resize the image to accommodate the size setting of the Label component.
The difference between the loading method of local images and network images:
1. How to get it: the network picture accesses the picture through request.get, and the local picture calls the QFileDailog class to load the picture.
2. QPixmap loading data: because QPixmap () usually loads the image name directly, it took me a long time to load the network image. I didn't know how to solve the error report of QtGui.QPixmap (r.content) (content is byte). Later, I saw that the circumvention method is:
Pix.loadFromData (r.content)
2. Pit encountered:
(1) if the gui interface made by Qt Designer is converted into a py script, the default Ui_Dialog class is object, which does not clearly indicate the inherited parent class, resulting in an error in the following program. So you need to declare the parent class, where either QWidget (base class) or QtWidgets.QDialog (dialog class) can
(2) when the network image was first loaded, it was written like this:
Pix = QtGui.QPixmap () .loadFromData (r.content), but found that pix returned True instead of the class after the image was loaded. Therefore, the loading of binary picture data needs to be written on a separate line.
(3) because I got the picture through Baidu scenery. Therefore, the URL address of the picture must be able to open the picture directly, not other messy web elements. The solution is to right-click the picture and copy the picture address.
3. Extended knowledge (not yet in-depth understanding):
(1) how to write image data and save it:
With open ("1.jpg", "wb") as f:
F.write (r.content)
Note that if it is "w", it will report an error.
(2) display pictures with PIL library:
From PIL import Image
F = Image.open ('1.jpg')
F.show ()
F.save ("new.jpg")
(3) convert images from numpy library into an array:
Import numpy as np
Res = np.array (Image.open ("1.jpg")) about "how Python loads and displays pictures from the GUI interface" ends here. Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
Rpm-ivh lrzsz-0.12.20-27.1.el6.x86_64.rpm installation upload / download kit
© 2024 shulou.com SLNews company. All rights reserved.