How do I display an Image in PyQt5?

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

Adding images to your PyQt5 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 insert an external image into your PyQt5 application layout, using both Python code and Qt Designer.

Which widget to use to display an image in PyQt5?

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 and QPixmap to display an image in your PyQt5 applications.

Display an image in PyQt5 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.

Bring Your PyQt/PySide Application to Market — Specialized launch support for scientific and engineering software built using Python & Qt.

Find out More

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 PyQt5 application template below. This loads the .ui file we've created (mainwindow.ui), creates the window and starts up the application. If you're new to building UIs with Qt Designer, see our guide to getting started with Qt Designer.

PyQt5
import sys
from PyQt5 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.

PyQt5 Qt Designer application showing an image with QLabel Qt Designer application showing a Cat image in a QLabel

Display an image using QLabel and QPixmap in Python code

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

PyQt5
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_())

The block of code below shows the step-by-step process of displaying an image with QLabel and QPixmap: 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. If you want to learn more about the other basic widgets available in PyQt5, check out our widgets tutorial.

PyQt5 QMainWindow with image displayed using QLabel and QPixmap QMainWindow with Cat image displayed using QLabel and QPixmap

Scaling images in PyQt5 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.

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 PyQt5 applications using QLabel and QPixmap — both from Qt Designer and directly from Python code. By combining QLabel.setPixmap() with QPixmap, you can easily load and show image files such as JPG and PNG in your PyQt5 GUI windows. You also learned how to scale images to fit your widget using setScaledContents, giving you full control over how images appear in your PyQt5 application.

For more advanced image handling, you may want to explore bitmap graphics and drawing with QPainter or learn how to build your own custom widgets for more specialized image display needs.

The complete guide to packaging Python GUI applications with PyInstaller.
Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak
Martin Fitzpatrick

How do I display an Image in PyQt5? was written by Martin Fitzpatrick with contributions from John Lim.

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt. Martin founded PythonGUIs to provide easy to follow GUI programming tutorials to the Python community. He has written a number of popular Python books on the subject.