Home ยป PyQt6 QPixmap Widget Tutorial

PyQt6 QPixmap Widget Tutorial

Java SE 11 Developer (Upgrade) [1Z0-817]
Oracle Java Certification
Java SE 11 Programmer I [1Z0-815] Practice Tests
1 Year Subscription
Java SE 11 Programmer II [1Z0-816] Practice Tests
Spring Framework Basics Video Course

The QPixmap class in PyQt6 is primarily used for handling and displaying images. It provides powerful tools to load, manipulate, and display images efficiently. QPixmap is optimized for use with widgets like QLabel and for rendering images on a GUI.

This tutorial explains the basics of QPixmap, demonstrates its features, and includes code examples to help you integrate images into your PyQt6 applications.

1. Basics of QPixmap

Key Features:

  • Load images from files or memory.
  • Supports many formats (e.g., JPEG, PNG, BMP, GIF).
  • Allows scaling, cropping, and transformations.
  • Works seamlessly with QLabel and QPainter.

Importing QPixmap

from PyQt6.QtGui import QPixmap
from PyQt6.QtWidgets import QLabel, QApplication, QMainWindow

2. Example 1: Displaying an Image

This example demonstrates how to load and display an image using QPixmap and QLabel.

Code:

import sys
from PyQt6.QtGui import QPixmap
from PyQt6.QtWidgets import QApplication, QLabel, QMainWindow

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Display Image")

        # Load the image
        pixmap = QPixmap("path/to/your/image.jpg")  # Replace with your image path

        # Create a QLabel to display the QPixmap
        label = QLabel(self)
        label.setPixmap(pixmap)

        # Set the QLabel as the central widget
        self.setCentralWidget(label)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())

Output:

  • A window displaying the specified image.

3. Example 2: Resizing an Image

You can scale a QPixmap to fit within a specific size using the scaled method.

Code:

import sys
from PyQt6.QtGui import QPixmap
from PyQt6.QtWidgets import QApplication, QLabel, QMainWindow
from PyQt6.QtCore import Qt

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Resize Image")

        # Load and resize the image
        pixmap = QPixmap("path/to/your/image.jpg").scaled(400, 300, Qt.AspectRatioMode.KeepAspectRatio)

        # Display the resized QPixmap
        label = QLabel(self)
        label.setPixmap(pixmap)

        self.setCentralWidget(label)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())

4. Example 3: Cropping an Image

Use the copy method to crop a section of the image.

Code:

import sys
from PyQt6.QtGui import QPixmap
from PyQt6.QtWidgets import QApplication, QLabel, QMainWindow

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Crop Image")

        # Load the image
        pixmap = QPixmap("path/to/your/image.jpg")

        # Crop the image (x, y, width, height)
        cropped_pixmap = pixmap.copy(50, 50, 200, 200)

        # Display the cropped QPixmap
        label = QLabel(self)
        label.setPixmap(cropped_pixmap)

        self.setCentralWidget(label)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())

5. Example 4: Drawing on an Image

You can use QPainter to draw shapes or text on a QPixmap.

Code:

import sys
from PyQt6.QtGui import QPixmap, QPainter, QPen, QColor
from PyQt6.QtWidgets import QApplication, QLabel, QMainWindow
from PyQt6.QtCore import Qt

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Draw on Image")

        # Load the image
        pixmap = QPixmap("path/to/your/image.jpg")

        # Create a QPainter object
        painter = QPainter(pixmap)

        # Draw a rectangle
        pen = QPen(QColor("red"))
        pen.setWidth(5)
        painter.setPen(pen)
        painter.drawRect(50, 50, 100, 100)

        # Draw text
        painter.setPen(QColor("blue"))
        painter.drawText(60, 80, "Hello PyQt6!")

        painter.end()

        # Display the modified QPixmap
        label = QLabel(self)
        label.setPixmap(pixmap)

        self.setCentralWidget(label)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())

6. Example 5: Saving an Image

You can save a QPixmap to a file using the save method.

Code:

import sys
from PyQt6.QtGui import QPixmap
from PyQt6.QtWidgets import QApplication, QLabel, QMainWindow

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Save Image")

        # Load the image
        pixmap = QPixmap("path/to/your/image.jpg")

        # Save the image in another format
        pixmap.save("output_image.png", "PNG")

        # Display the saved image
        label = QLabel(self)
        label.setPixmap(pixmap)

        self.setCentralWidget(label)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())

7. Example 6: Handling Transparency

You can create transparent images or apply alpha channels to QPixmap.

Code:

import sys
from PyQt6.QtGui import QPixmap, QColor, QPainter
from PyQt6.QtWidgets import QApplication, QLabel, QMainWindow

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Transparent Image")

        # Create a transparent QPixmap
        pixmap = QPixmap(400, 300)
        pixmap.fill(Qt.GlobalColor.transparent)

        # Draw on the transparent QPixmap
        painter = QPainter(pixmap)
        painter.setBrush(QColor(255, 0, 0, 128))  # Semi-transparent red
        painter.drawRect(50, 50, 300, 200)
        painter.end()

        # Display the transparent QPixmap
        label = QLabel(self)
        label.setPixmap(pixmap)

        self.setCentralWidget(label)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())

8. Example 7: Using QPixmap with QGraphicsView

You can integrate QPixmap into a QGraphicsScene and display it in a QGraphicsView.

Code:

import sys
from PyQt6.QtGui import QPixmap
from PyQt6.QtWidgets import QApplication, QGraphicsView, QGraphicsScene, QMainWindow

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("QPixmap in QGraphicsView")

        # Create a QGraphicsScene
        scene = QGraphicsScene()

        # Load the QPixmap and add it to the scene
        pixmap = QPixmap("path/to/your/image.jpg")
        scene.addPixmap(pixmap)

        # Display the scene in a QGraphicsView
        view = QGraphicsView(scene)
        self.setCentralWidget(view)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())

Summary of QPixmap Features:

  1. Loading Images: Load images from files or memory.
  2. Manipulation: Resize, crop, and draw on images.
  3. Transparency: Handle alpha channels for transparent images.
  4. Integration: Use with QLabel, QPainter, or QGraphicsView.
  5. Saving: Export images in various formats.

These examples demonstrate the versatility of QPixmap for creating and manipulating images in PyQt6 applications.

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More