How Do I Display Images in PyQt6?

Using QLabel and QPixmap to easily add images to your applications
Heads up! You've already completed this tutorial.

Adding images to your Python GUI application is a common requirement, whether you're building an image/photo viewer, or just want to add some decoration to your GUI. Unfortunately, because of how this is done in Qt, it can be a little bit tricky to work out at first.

In this short tutorial, we will look at how you can display an image in a PyQt6 application using QLabel and QPixmap, with both Python code and Qt Designer examples.

Which Widget to Use for Displaying Images in PyQt6?

Since you're wanting to insert an image you might be expecting to use a widget named QImage or similar, but that would make a bit too much sense! QImage is actually Qt's image object type, which is used to store the actual image data for use within your application. The widget you use to display an image is QLabel.

The primary use of QLabel is of course to add labels to a UI, but it also has the ability to display an image — or pixmap — instead, covering the entire area of the widget. Below we'll look at how to use QLabel with QPixmap to display an image in your PyQt6 applications. If you're new to PyQt6, you might want to start with our tutorial on creating your first PyQt6 window.

Displaying Images in PyQt6 Using Qt Designer

First, create a MainWindow object in Qt Designer and add a "Label" to it. You can find Label in Display Widgets at the bottom of the left hand panel. Drag this onto the QMainWindow to add it.

MainWindow with a single QLabel added in Qt Designer MainWindow with a single QLabel added

Next, with the Label selected, look in the right hand QLabel properties panel for the pixmap property (scroll down to the blue region). From the property editor dropdown select "Choose File…" and select an image file to insert.

As you can see, the image is inserted, but the image is kept at its original size, cropped to the boundaries of the QLabel box. You need to resize the QLabel to be able to see the entire image.

In the same controls panel, click to enable scaledContents.

When scaledContents is enabled the image is resized to fit the bounding box of the QLabel widget. This shows the entire image at all times, although it does not respect the aspect ratio of the image if you resize the widget.

You can now save your UI to file (e.g. as mainwindow.ui).

To view the resulting UI, we can use the standard application template below. This loads the .ui file we've created (mainwindow.ui), creates the window and starts up the application. For more on designing UIs with Qt Designer, see our guide to Qt Designer GUI layout.

PyQt6
import sys
from PyQt6 import QtWidgets, uic

app = QtWidgets.QApplication(sys.argv)

window = uic.loadUi("mainwindow.ui")
window.show()
app.exec()

Running the above code will create a window, with the image displayed in the middle.

PyQt6 application showing an image using QLabel and Qt Designer QtDesigner application showing a Cat

Displaying Images Using Python Code with QLabel and QPixmap

Instead of using Qt Designer, you might also want to show an image in your PyQt6 application through code. As before we use a QLabel widget and add a pixmap image to it. This is done by creating a QPixmap object and passing it to the QLabel method .setPixmap(). The full code is shown below.

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

class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = "Image Viewer"
        self.setWindowTitle(self.title)

        label = QLabel(self)
        pixmap = QPixmap('cat.jpg')
        label.setPixmap(pixmap)
        self.setCentralWidget(label)
        self.resize(pixmap.width(), pixmap.height())


app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec())
python
import sys
from PySide6.QtGui import QPixmap
from PySide6.QtWidgets import QMainWindow, QApplication, QLabel

class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = "Image Viewer"
        self.setWindowTitle(self.title)

        label = QLabel(self)
        pixmap = QPixmap('cat.jpg')
        label.setPixmap(pixmap)
        self.setCentralWidget(label)
        self.resize(pixmap.width(), pixmap.height())


app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec())
python
import sys
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QMainWindow, QApplication, QLabel

class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = "Image Viewer"
        self.setWindowTitle(self.title)

        label = QLabel(self)
        pixmap = QPixmap('cat.jpg')
        label.setPixmap(pixmap)
        self.setCentralWidget(label)
        self.resize(pixmap.width(), pixmap.height())


app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
python
import sys
from PySide2.QtGui import QPixmap
from PySide2.QtWidgets import QMainWindow, QApplication, QLabel

class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = "Image Viewer"
        self.setWindowTitle(self.title)

        label = QLabel(self)
        pixmap = QPixmap('cat.jpg')
        label.setPixmap(pixmap)
        self.setCentralWidget(label)
        self.resize(pixmap.width(), pixmap.height())


app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

The block of code below shows the process of creating the QLabel, creating a QPixmap object from our file cat.jpg (passed as a file path), setting this QPixmap onto the QLabel with .setPixmap() and then finally resizing the window to fit the image.

python
label = QLabel(self)
pixmap = QPixmap('cat.jpg')
label.setPixmap(pixmap)
self.setCentralWidget(label)
self.resize(pixmap.width(), pixmap.height())

Launching this code will show a window with the cat photo displayed and the window sized to the size of the image.

PyQt6 QMainWindow displaying an image with QLabel and QPixmap QMainWindow with Cat image displayed

Scaling Images in PyQt6 with setScaledContents

Just as in Qt Designer, you can call .setScaledContents(True) on your QLabel image to enable scaled mode, which resizes the image to fit the available space. This is useful when you want the displayed image to automatically adapt to the size of the label widget.

python
label = QLabel(self)
pixmap = QPixmap('cat.jpg')
label.setPixmap(pixmap)
label.setScaledContents(True)
self.setCentralWidget(label)
self.resize(pixmap.width(), pixmap.height())

Notice that you set the scaled state on the QLabel widget and not the image pixmap itself.

Conclusion

In this quick tutorial we've covered how to display images in your PyQt6 applications using QLabel and QPixmap. We walked through two approaches: using Qt Designer to visually add images to your UI, and writing Python code to programmatically load and display images with QPixmap. Both methods rely on QLabel as the display widget and QPixmap to handle the image data, giving you a straightforward way to enhance your Python GUI applications with images. You also learned how to use setScaledContents to automatically scale images to fit the label widget.

For more advanced image and graphics work, take a look at our tutorial on bitmap graphics in PyQt6 or explore other PyQt6 widgets to build richer interfaces.

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

Create GUI Applications with Python & Qt6 by Martin Fitzpatrick

(PyQt6 Edition) The hands-on guide to making apps with Python — Over 15,000 copies sold!

More info Get the book

John Lim Ji Xiong

How Do I Display Images in PyQt6? was written by John Lim.

John is a developer from Kuala Lumpur, Malaysia who works as a Senior R&D Engineer.