In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to deal with the problem of QtGui.QImage picture display in PyQt5. The article is very detailed and has a certain reference value. Interested friends must finish reading it!
Preface
PyQt is one of the GUI programming solutions in Python language environment. In addition, PyGTK and wxPython are also commonly used. As the Python extension of Qt language, PyQt can be used to develop interface applications conveniently and quickly.
QtGui.QImage picture display in PyQt5
Import os.path as ospimport cv2from qtpy import QtCorefrom qtpy import QtWidgetsfrom qtpy import QtGui# TracelessLe Note: code example Cannot execute class MainWindow (QtWidgets.QMainWindow): def _ init__ (self): self.image = QtWidgets.QLabel () def show_img (self, img_path): self.image.clear () img_np = cv2.imread (img_path) width = img_np.shape [1] height = img_np.shape [0] img_rgb_data = cv2.cvtColor (img_np) Cv2.COLOR_BGR2RGB) # label_data = QtGui.QImage (img_rgb_data.data, width, height, QtGui.QImage.Format_RGB888) # some pictures may have problems with label_data = QtGui.QImage (img_rgb_data.data, width, height, width*3, QtGui.QImage.Format_RGB888) # the correct way to display RGB images self.image.setPixmap (QtGui.QPixmap.fromImage (label_data))
Analysis of the problem of picture display
As mentioned in the above code, the correct way to display a RGB three-channel picture is to use:
Label_data = QtGui.QImage (img_rgb_data.data, width, height, width*3, QtGui.QImage.Format_RGB888)
When you enter the QtGui.QImage class, you can see that there are many supported image data formats:
Supported image formats can also be found on the QT website:
There are also many ways to initialize images:
From the point of view of the mode of use, it is mainly divided into three categories:
(1) incoming image size or width, as well as image format (QImage.Format)
(2) incoming image data, image size or width, and image format
(3) incoming image path, image format
Usually for images that have been read and processed using OpenCV, we use method (2) to initialize.
For commonly used RGB three-channel images, the image format at initialization is generally set to QtGui.QImage.Format_RGB888.
When using it, the correct way to use it is:
Def _ _ init__ (self, data: bytes, width: int, height: int, bytesPerLine: int, format: 'QImage.Format')
If you use the
Def _ _ init__ (self, data: bytes, width: int, height: int, format: 'QImage.Format')
Method initialization, you may encounter some display problems. For example, the picture oblique display, the picture black and white bar interval display, the picture load directly crashes and so on.
Comparing the two methods, it is found that the problem lies in the parameter bytesPerLine: int. This parameter specifies the interval (bytesPerLine) of the bytes input data data on the data arrangement. If it is not specified, for some input data, QtGui.QImage cannot properly infer the data arrangement format, then the display exception in the above will occur.
The above is all the contents of the article "how to deal with the problem of QtGui.QImage Picture display in PyQt5". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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
© 2024 shulou.com SLNews company. All rights reserved.