0

I'm trying to load "jpg" picture on the QLabel in PySide6. So I used this:

mypic = QPixmap()
mypic.load("./test.jpg")
self.ui.mylabel.setPixmap(QPixmap(mypic))

However, I found there is empty in mylabel. When I debug in this program, I found that my pic is null (even after it loads the image file).

So I wonder if the jpg is not automatically supported in PySide6.

As I checked the documention of PySide6, it illustrates that the default supporting image formats include "jpg".

enter image description here

As I print the supportedImageFormats using:

print(QtGui.QImageReader.supportedImageFormats())

It returns with this:

[PySide6.QtCore.QByteArray(b'bmp'),  PySide6.QtCore.QByteArray(b'pbm'),  PySide6.QtCore.QByteArray(b'pgm'),  PySide6.QtCore.QByteArray(b'png'),  PySide6.QtCore.QByteArray(b'ppm'),  PySide6.QtCore.QByteArray(b'xbm'),  PySide6.QtCore.QByteArray(b'xpm')] 

I found it does not include the "jpg" format by default!

Then I searched a lot of solutions, I found that this can solve my problem: add one line of code to introduce the "imageformats" plugins in PySide6 folder.

app.addLibraryPath(os.path.join(os.path.dirname(QtCore.__file__), "plugins"))

Here I printed the os.path.dirname(QtCore.__file__), and found it was my PySide6 folder.

I checked the plugins folder, and found the inner imageformats folder, and found these dlls:

enter image description here

It seems that the jpg format is supported here. So when I add the plugin path, it can solve my problem. But it still confuses me that why I should add the plugin path (The official documentation implies that this format is supported by default!).

I wonder if there is a more convincing solution because adding one line of code in my program every time seems clumsy. Or if I left out something. I sincerely want to get your help. Thank you!

  • I am unable to reproduce your situation... jpeg load fine without adding any additional plugins for me. When you debug does the pixmap properly load the jpg? – Alexander Nov 15 '22 at 07:04
  • Sorry, when I debug, the pixmap is NULL all along. So I wonder if there is anything wrong in the installation of PySide6. I use the `pip install PySide6` instruction to install the package in my anaconda base environment, and set the enviroment variables **QT_QPA_PLATFORM_PLUGIN_PATH**. I think I didn't make mistakes in these steps. I reinstall the PySide6 but find it still fails to load the jpg. Thank you for sharing your situation! – Zzz212zzZ Nov 15 '22 at 12:52
  • @Zzz212zzZ Have seen [this](https://stackoverflow.com/q/56491169/984421)? – ekhumoro Nov 15 '22 at 13:54

0 Answers0