Skip to content

toqpixmap() doesn't work with PyQt5 #7994

Description

@niklashenning

What did you do?

I tried using .toqpixmap() on a Pillow Image to convert it to a QPixmap in a PyQt5 application

What did you expect to happen?

I expected the image to load and show but the program just crashes.

What actually happened?

The program instantly crashes without the debugger even showing anything: Process finished with exit code -1073740791 (0xC0000409)

What are your OS, Python and Pillow versions?

  • OS: Windows 10
  • Python: Python 3.11.3
  • Pillow: 10.1.0

features.pilinfo(supported_formats=False):

--------------------------------------------------------------------
Pillow 10.1.0
Python 3.11.3 (tags/v3.11.3:f3909b8, Apr  4 2023, 23:49:59) [MSC v.1934 64 bit (AMD64)]
--------------------------------------------------------------------
Python modules loaded from C:\Users\NH\AppData\Local\Programs\Python\Python311\Lib\site-packages\PIL
Binary modules loaded from C:\Users\NH\AppData\Local\Programs\Python\Python311\Lib\site-packages\PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 10.1.0
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.13.2
--- LITTLECMS2 support ok, loaded 2.15
--- WEBP support ok, loaded 1.3.2
--- WEBP Transparency support ok
--- WEBPMUX support ok
--- WEBP Animation support ok
--- JPEG support ok, compiled for libjpeg-turbo 3.0.0
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.0
--- ZLIB (PNG/ZIP) support ok, loaded 1.3
--- LIBTIFF support ok, loaded 4.6.0
*** RAQM (Bidirectional Text) support not installed
*** LIBIMAGEQUANT (Quantization method) support not installed
*** XCB (X protocol) support not installed
--------------------------------------------------------------------

Not working with PyQt5:

import sys
from PIL import Image
from PyQt5.QtWidgets import QMainWindow, QLabel, QApplication


# NOT WORKING
class Window(QMainWindow):
    def __init__(self):
        super().__init__(parent=None)

        image = Image.open('test.png')

        self.label = QLabel(self)
        self.label.setPixmap(image.toqpixmap())
        self.label.setFixedSize(64, 64)
        self.label.move(0, 0)


app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())

Same code with PyQt6 works:

import sys
from PIL import Image
from PyQt6.QtWidgets import QMainWindow, QLabel, QApplication


# WORKING
class Window(QMainWindow):
    def __init__(self):
        super().__init__(parent=None)

        image = Image.open('test.png')

        self.label = QLabel(self)
        self.label.setPixmap(image.toqpixmap())
        self.label.setFixedSize(64, 64)
        self.label.move(0, 0)


app = QApplication(sys.argv)
window = Window()
window.show()
app.exec()

I found this workaround to be working for PyQt5, but honestly I'd rather figure out why toqpixmap() isn't working:

import sys
from PIL import Image
from PyQt5 import QtGui
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QMainWindow, QLabel, QApplication


# WORKAROUND
class Window(QMainWindow):
    def __init__(self):
        super().__init__(parent=None)

        image = Image.open('test.png')
        q_image = QImage(image.tobytes('raw', 'RGBA'), image.width,
                         image.height, QtGui.QImage.Format.Format_RGBA8888)

        self.label = QLabel(self)
        self.label.setPixmap(QPixmap.fromImage(q_image))
        self.label.setFixedSize(64, 64)
        self.label.move(0, 0)


app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions