Image file that can be read by QPixmap of PyQt5

2021/04/02 categories:PyQt5| tags:PyQt5|Python|QPixmap|

v I created a program to check the format of the image file that can be read by QPixmap.

Reading image files with QPixmap

QPixmap constructor is as follows, and if you pass the file name to the first argument of the QPixmap constructor, the image file will be displayed. It can be read and converted to QPixmap.

QPixmap::QPixmap(const QString &fileName, const char *format = nullptr, Qt::ImageConversionFlags flags = Qt::AutoColor)

If the file does not exist or the format is unknown, QPixmap will be null, and isNull () will determine if it is null. I can do it.

Try loading various image files

Reference seems to support the following formats.

Format Description Qt’s support
BMP Windows Bitmap Read/write
GIF Graphic Interchange Format (optional) Read
JPG Joint Photographic Experts Group Read/write
JPEG Joint Photographic Experts Group Read/write
PNG Portable Network Graphics Read/write
PBM Portable Bitmap Read
PGM Portable Graymap Read
PPM Portable Pixmap Read/write
XBM X11 Bitmap Read/write
XPM X11 Pixmap Read/write

Try loading various image files

I converted the format with Paint and Inkscape and read the following file.

Using the source code described below, I tried a test to display an image with GUI. The format that could not be read by QPixmap is displayed as “The file does not exist or is of an unknown format”, and the format that can be read is the program that displays the image.

How to check supported formats

Further reading the reference, QImageReader.supportedImageFormats describes how to get the supported formats.

from PyQt5 import QtGui

for byteArray in QtGui.QImageReader().supportedImageFormats():
    print( bytes(byteArray).decode() )

When I ran the above code, I got the following results:

bmp
cur
gif
icns
ico
jpeg
jpg
pbm
pgm
png
ppm
svg
svgz
tga
tif
tiff
wbmp
webp
xbm
xpm

Source code

The code used to load various image files is as follows.

# -*- coding: utf-8 -*-
import sys
from pathlib import Path
from PyQt5 import QtWidgets, QtCore, QtGui

class Widget(QtWidgets.QWidget):
    def __init__(self):
        super(Widget, self).__init__()

        self.model = QtGui.QStandardItemModel()

        self.list = QtWidgets.QListView(self)
        self.list.setModel(self.model)
        self.list.setMaximumWidth(150)
        self.list.clicked.connect(self.listClicked)

        self.scene = QtWidgets.QGraphicsScene()
        self.view = QtWidgets.QGraphicsView(self)
        self.view.setScene(self.scene)

        self.setLayout( QtWidgets.QHBoxLayout() )
        self.layout().addWidget(self.list)
        self.layout().addWidget(self.view)

    def appendRow(self, data, text):
        item = QtGui.QStandardItem(text)
        item.setData(data)
        self.model.appendRow(item)

    def listClicked(self, clickedIndex):
        path = str( self.model.itemFromIndex(clickedIndex).data().absolute() )
        pixmap = QtGui.QPixmap(path)
        self.scene.clear()
        if pixmap.isNull():
            textItem = QtWidgets.QGraphicsTextItem('The file does not exist \nor is of an unknown format')
            self.scene.addItem(textItem)
            self.scene.setSceneRect( textItem.boundingRect() )
        else:
            self.scene.addPixmap(pixmap)
            self.scene.setSceneRect( QtCore.QRectF(pixmap.rect()) )
        self.view.fitInView(self.scene.items()[0], QtCore.Qt.KeepAspectRatio)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    for path in Path().glob('test1.*'):
        w.appendRow(path, path.name)
    w.show()
    app.exec()

Share post

Related Posts

コメント