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 −

pyqt qpixmap example 1

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.

pyqt qpixmap example 2
Advertisements