
- PyQt - Home
- PyQt - Introduction
- PyQt - Environment
- PyQt - Hello World
- PyQt - Major Classes
- PyQt - Using Qt Designer
- PyQt - Meta Objects
- PyQt Signals & Slots
- PyQt - Signals and Slots
- PyQt - Support and Signals
- PyQt - Unbound and Bound Signals
- PyQt - New Signals with PyQtSignal
- PyQt - Connecting, Disconnecting, & Emitting Signals
- PyQt - Slot decorator
- PyQt - Slot Connection
- PyQt Layouts
- PyQt - Layout Management
- PyQt - QBoxLayout
- PyQt - QGridLayout
- PyQt - QFormLayout
- PyQt - QHBoxLayout
- PyQt - QVBoxLayout
- PyQt - QStackedLayout
- PyQt - QGraphicsGridLayout
- PyQt - QGraphicsAnchorLayout
- PyQt - QGraphicsLayout
- PyQt - QGraphicsLinearLayout
- PyQt Basic Widgets
- PyQt - Basic Widgets
- PyQt - Qlabel Widget
- PyQt - QLineEdit Widget
- PyQt - QPushButton Widget
- PyQt - QRadioButton Widget
- PyQt - QCheckBox Widget
- PyQt - QComboBox Widget
- PyQt - QSpinBox Widget
- PyQt - QMessageBox
- PyQt - QDialogButtonBox Widget
- PyQt - QFontComboBox Widget
- PyQt - QDoubleSpinBox Widget
- PyQt - QToolBox Widget
- PyQt - QMenuBar, QMenu & Qaction Widgets
- PyQt - QToolTip
- PyQt - QInputDialog Widget
- PyQt - QFontDialog Widget
- PyQt - QDialog Widget
- PyQt - QFileDialog Widget
- PyQt - QTab Widget
- PyQt - QSplitter Widget
- PyQt - QDock Widget
- PyQt - QStatusBar Widget
- PyQt - QTabBar
- PyQt - QList Widget
- PyQt - QScrollBar Widget
- PyQt - QProgressBar
- PyQt - QCalendar Widget
- PyQt - QMessageBox Widget
- PyQt - QPlainTextEdit
- PyQt - QDateEdit
- PyQt - QDateTimeEdit
- PyQt - QTimeEdit
- PyQt - QTextEdit
- PyQt - QTextBrowser
- PyQt - QScrollArea
- PyQt - Drag and Drop
- PyQt - Multiple Document Interface
- PyQt - QDialog Class
- PyQt Views
- PyQt - QColumnView
- PyQt - QTableView
- PyQt Drawing API
- PyQt - Drawing API
- PyQt - Drawing a Line
- PyQt - Drawing a Rectangle
- PyQt - Drawing a Triangle
- PyQt - Drawing a Circle
- PyQt - Drawing a Ellipse
- PyQt - Drawing a Polygon
- PyQt - Geometric Transformation
- PyQt - Drawing Effect
- PyQt Groups
- PyQt - QButtonGroup
- PyQt - QGroupBox
- PyQt Effects
- PyQt - Effects
- PyQt - Opacity Effect
- PyQt - QGraphicsBlur Effect
- PyQt - QGraphicsColorize Effect
- PyQt - QGraphicsDropShadow Effect
- PyQt Events
- PyQt - Event Handling
- PyQt - Drag & Drop Events
- PyQt - File Open Event
- PyQt - Action Event
- PyQt - Hide Event
- PyQt - Resize Event
- PyQt Database
- PyQt - Database Handling
- PyQt Essentials
- PyQt - BrushStyle Constants
- PyQt - QClipboard
- PyQt - QPixmap Class
- PyQt Useful Resources
- PyQt - Quick Guide
- PyQt - Useful Resources
- PyQt - Discussion
PyQt - QPixmap Class
In PyQt the QPixmap class is created to efficiently manage images. It acts as a storage unit, for off screen image display. QPixmap plays a important role in showcasing images within PyQt applications. It Offers flexibility, with different image formats to cater to various needs.
QPixmap class provides an off-screen representation of an image. It can be used as a QPaintDevice object or can be loaded into another widget, typically a label or button.
Qt API has another similar class QImage, which is optimized for I/O and other pixel manipulations. Pixmap, on the other hand, is optimized for showing it on screen. Both formats are interconvertible.
The types of image files that can be read into a QPixmap object are as follows −
BMP | Windows Bitmap |
GIF | Graphic Interchange Format (optional) |
JPG | Joint Photographic Experts Group |
JPEG | Joint Photographic Experts Group |
PNG | Portable Network Graphics |
PBM | Portable Bitmap |
PGM | Portable Graymap |
PPM | Portable Pixmap |
XBM | X11 Bitmap |
XPM | X11 Pixmap |
Methods in QPixmap
Following methods are useful in handling QPixmap object −
Sr.No. | Methods & Description |
---|---|
1 |
copy() Copies pixmap data from a QRect object |
2 |
fromImage() Converts QImage object into QPixmap |
3 |
grabWidget() Creates a pixmap from the given widget |
4 |
grabWindow() Create pixmap of data in a window |
5 |
Load() Loads an image file as pixmap |
6 |
save() Saves the QPixmap object as a file |
7 |
toImage Converts a QPixmap to QImage |
The most common use of QPixmap is to display image on a label/button.
Features and functionality
Loading Images
One of the features of QPixmap involves loading images. You have the option to load images from files by utilizing the load() function. This function provides an false outcome to show if the image was loaded successfully.
pixmap = QPixmap() loaded = pixmap.load('image.jpeg')
Displaying Images
After loading an image into a QPixmap object, you can display it within PyQt windows using QLabel. Set the pixmap property of QLabel to the QPixmap object.
label = QLabel(self) label.setPixmap(pixmap)
Handling Image Paths
PyQt5 applications often require users to select images dynamically. QPixmap supports loading images using file paths. You can obtain the file path using QFileDialog.getOpenFileName() and then load the image using QPixmap.
image = QFileDialog.getOpenFileName(None, 'OpenFile', '', "Image file(*.png)") imagePath = image[0] pixmap = QPixmap(imagePath)
Example 1: Displaying Image in a Window
This example creates a PyQt5 window and displays an image (image.jpeg) within it using a QLabel. The image is loaded into a QPixmap object and set as the pixmap property of the QLabel.
import sys import PyQt6 from PyQt6.QtWidgets import QApplication, QWidget, QLabel from PyQt6.QtGui import QPixmap class App(QWidget): def __init__(self): super().__init__() self.title = 'PyQt5 image - pythonspot.com' self.left = 10 self.top = 50 self.width = 640 self.height = 480 self.initUI() def initUI(self): self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) # Create widget label = QLabel(self) pixmap = QPixmap('python_logo.png') label.setPixmap(pixmap) self.resize(pixmap.width(), pixmap.height()) self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = App() sys.exit(app.exec())
The above code produces the following output −

Example 2: Loading Image Dynamically
import sys from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QFileDialog from PyQt6.QtGui import QPixmap class App(QWidget): def __init__(self): super().__init__() self.title = 'Dynamic Image Loading' self.initUI() def initUI(self): self.setWindowTitle(self.title) self.setGeometry(100, 100, 640, 480) # Create widget label = QLabel(self) image_path, _ = QFileDialog.getOpenFileName(self, 'Open Image', '', "Image files (*.jpg *.png)") if image_path: pixmap = QPixmap(image_path) label.setPixmap(pixmap) self.resize(pixmap.width(), pixmap.height()) else: label.setText('No image selected.') self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = App() sys.exit(app.exec())
The above code opens the file system to select the image file you want to open.
