python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.32k stars 2.23k forks source link

Detach PyQt6 QPixmap instance before returning #8509

Closed radarhere closed 3 weeks ago

radarhere commented 3 weeks ago

Alternative to #8507

The user in that PR reported seeing corrupt output from ImageQt.toqpixmap() on Windows.

https://github.com/python-pillow/Pillow/blob/9a4b3e05d643c92f050df5713e48225a9f36497e/src/PIL/ImageQt.py#L214-L216

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.

codev8services commented 3 weeks ago

This resolved the issue #8507. RGB and RGBA work correctly.