A QPixmap can be used to show an image in a PyQT window. QPixmap() can load an image, as parameter it has the filename. To show the image, add the QPixmap to a QLabel.

QPixmap supports all the major image formats: BMP,GIF,JPG,JPEG,PNG,PBM,PGM,PPM,XBM and XPM.

Related Course: Create GUI Apps with Python PyQt5

PyQT image

QPixmap

Start by creating a QPixmap and a QLabel. Then you can combine them like this:

1
2
3
self.im = QPixmap("./image.jpg")
self.label = QLabel()
self.label.setPixmap(self.im)

Then add the whole thing to a layout, like a QGridLayout

1
2
3
self.grid = QGridLayout()
self.grid.addWidget(self.label,1,1)
self.setLayout(self.grid)

image

Example

Copy and paste the code below to load an image from your local computer. The image will be shown in a grid layout.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QGridLayout, QWidget
from PyQt5.QtGui import QPixmap

class Example(QWidget):

def __init__(self):
super().__init__()

self.im = QPixmap("./image.jpg")
self.label = QLabel()
self.label.setPixmap(self.im)

self.grid = QGridLayout()
self.grid.addWidget(self.label,1,1)
self.setLayout(self.grid)

self.setGeometry(50,50,320,200)
self.setWindowTitle("PyQT show image")
self.show()

if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

If you are new to Python PyQt, then I highly recommend this book.

Download Examples