How do I display an Image in PyQt5/PySide2?
Tags: python, pyqt
Published: 2020-05-24 13:35:13

The QPixmap class is used to display the image inside a PyQt/Pyside window, either as a QPainterDevice object or loaded into a control, usually a label or button. 

QPixmap can read image file types such as BMP, GIF, JPG, etc. If you want to <a href="https://pythonpyqt.com/pyqt-gif/">display animated GIFs</a>, you can use QMovie. PyQt can also <a href="https://pythonprogramminglanguage.com/pyqt5-video-widget/">display videos</a>.

QPixmap is one of the widgets used to process images. It is optimized for displaying images on the screen. In our code example, we will use QPixmap to display the image on the window.

If you are new to PyQt, I suggest <a href="https://gumroad.com/l/pysqtsamples">this course/book</a>.

## PyQt show image

The code below loads an image using a QPixmap. It attaches the QPixmap to a QLabel. The label is added to an <a href="https://pythonpyqt.com/pyqt-box-layout/">QHBoxLayout</a> and the QHboxLayout to the window.

```python
from PyQt5.Qt import (QWidget, QHBoxLayout, QLabel, QApplication)                                                                          
from PyQt5.QtGui import QPixmap                                                                                                            
import sys                                                                                                                                 
                                                                                                                                           
class Example(QWidget):                                                                                                                    
    def __init__(self):                                                                                                                    
        super().__init__()                                                                                                                 
        self.initUI()                                                                                                                      
                                                                                                                                           
    def initUI(self):                                                                                                                      
        hbox = QHBoxLayout(self)                                                                                                           
        pixmap = QPixmap("cat.jpg")                                                                                                        
                                                                                                                                           
        lbl = QLabel(self)                                                                                                                 
        lbl.setPixmap(pixmap)                                                                                                              
                                                                                                                                           
        hbox.addWidget(lbl)                                                                                                                
        self.setLayout(hbox)                                                                                                               
                                                                                                                                           
        self.move(300, 200)                                                                                                                
        self.setWindowTitle('Image with PyQt')                                                                                             
        self.show()                                                                                                                        
                                                                                                                                           
if __name__ == '__main__':                                                                                                                 
                                                                                                                                           
    app = QApplication(sys.argv)                                                                                                           
    ex = Example()                                                                                                                         
    sys.exit(app.exec_())      
```

![qpixmap show image](https://dev-to-uploads.s3.amazonaws.com/i/sj0031u8ufvqkfmo3prb.png)

In our example, we display the image on the window.

```python
pixmap = QPixmap("cat.png")
```

We create a QPixmap object. It takes the name of the file as an argument.

```python
lbl = QLabel(self)
lbl.setPixmap(pixmap)
```

We put the pixmap into the QLabel widget. 
Alternatively you can use <a href="https://pythonbasics.org/qt-designer-python/">designer</a>.

## PySide2 show image

The PySide2 version is similar. Both the PyQt and PySide toolkits are actively maintained, and by now more or less equal in features and quality. There are only few, rather unimportant differences.

```python
import sys                                                                                                                                 
from PySide2.QtGui import QPixmap                                                                                                          
from PySide2.QtWidgets import QMainWindow, QApplication, QLabel                                                                            
                                                                                                                                           
class MainWindow(QMainWindow):                                                                                                             
                                                                                                                                           
    def __init__(self):                                                                                                                    
        super(MainWindow, self).__init__()                                                                                                 
        self.title = "Load image in PySide2"                                                                                               
        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_())  
```

**Reference**
* <a href="https://gumroad.com/l/pysqtsamples">Course: Create Desktop Apps with PyQt5</a>
* <a href="https://pythonspot.com/gui/">More on Python PyQt</a>