Detach PyQt6 QPixmap instance before returning by radarhere · Pull Request #8509 · python-pillow/Pillow
Alternative to #8507
The user in that PR reported seeing corrupt output from ImageQt.toqpixmap() on Windows.
| def toqpixmap(im: Image.Image | str | QByteArray) -> QPixmap: | |
| qimage = toqimage(im) | |
| return getattr(QPixmap, "fromImage")(qimage) |
Testing, I found I was able to use GitHub Actions to reproduce the matter.
Testing further, I found that it was only happening when qimage was inside the function - if I unwrapped the function so that qimage wouldn't be garbage collected, the problem disappeared. I created a reproduction just using PyQt6 and asked about this at https://stackoverflow.com/questions/79133259/corrupted-display-from-qpixmap-fromimage. The response I received was
This is caused by Qt's implicit data sharing. The QImage will be garbage-collected when wrap returns, which may mean some of the shared data is no longer accesible. To avoid these problems, call pixmap.detach() to enforce a copy-on-write.
This fix allows us to test PyQt6 in GitHub Actions with CPython, which would otherwise fail.