mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-15 18:52:19 +03:00
Fix for issue #1370, Segfault using QImages due to not retaining the data
This commit is contained in:
parent
42d4b23ed7
commit
8582144e0e
|
@ -157,7 +157,6 @@ def _toqclass_helper(im):
|
||||||
else:
|
else:
|
||||||
raise ValueError("unsupported image mode %r" % im.mode)
|
raise ValueError("unsupported image mode %r" % im.mode)
|
||||||
|
|
||||||
# must keep a reference, or Qt will crash!
|
|
||||||
__data = data or align8to32(im.tobytes(), im.size[0], im.mode)
|
__data = data or align8to32(im.tobytes(), im.size[0], im.mode)
|
||||||
return {
|
return {
|
||||||
'data': __data, 'im': im, 'format': format, 'colortable': colortable
|
'data': __data, 'im': im, 'format': format, 'colortable': colortable
|
||||||
|
@ -175,8 +174,13 @@ if qt_is_installed:
|
||||||
string or a PyQt string object).
|
string or a PyQt string object).
|
||||||
"""
|
"""
|
||||||
im_data = _toqclass_helper(im)
|
im_data = _toqclass_helper(im)
|
||||||
|
# must keep a reference, or Qt will crash!
|
||||||
|
# All QImage constructors that take data operate on an existing
|
||||||
|
# buffer, so this buffer has to hang on for the life of the image.
|
||||||
|
# Fixes https://github.com/python-pillow/Pillow/issues/1370
|
||||||
|
self.__data = im_data['data']
|
||||||
QImage.__init__(self,
|
QImage.__init__(self,
|
||||||
im_data['data'], im_data['im'].size[0],
|
self.__data, im_data['im'].size[0],
|
||||||
im_data['im'].size[1], im_data['format'])
|
im_data['im'].size[1], im_data['format'])
|
||||||
if im_data['colortable']:
|
if im_data['colortable']:
|
||||||
self.setColorTable(im_data['colortable'])
|
self.setColorTable(im_data['colortable'])
|
||||||
|
|
|
@ -5,7 +5,17 @@ from PIL import ImageQt
|
||||||
|
|
||||||
|
|
||||||
if ImageQt.qt_is_installed:
|
if ImageQt.qt_is_installed:
|
||||||
from PIL.ImageQt import QImage
|
from PIL.ImageQt import QImage, QPixmap
|
||||||
|
|
||||||
|
try:
|
||||||
|
from PyQt5 import QtGui
|
||||||
|
except (ImportError, RuntimeError):
|
||||||
|
try:
|
||||||
|
from PyQt4 import QtGui
|
||||||
|
except (ImportError, RuntimeError):
|
||||||
|
from PySide import QtGui
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class TestToQImage(PillowQtTestCase, PillowTestCase):
|
class TestToQImage(PillowQtTestCase, PillowTestCase):
|
||||||
|
@ -23,5 +33,41 @@ class TestToQImage(PillowQtTestCase, PillowTestCase):
|
||||||
data.save(tempfile)
|
data.save(tempfile)
|
||||||
|
|
||||||
|
|
||||||
|
def test_segfault(self):
|
||||||
|
PillowQtTestCase.setUp(self)
|
||||||
|
|
||||||
|
app = QtGui.QApplication([])
|
||||||
|
ex = Example()
|
||||||
|
|
||||||
|
|
||||||
|
if ImageQt.qt_is_installed:
|
||||||
|
class Example(QtGui.QWidget):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super(Example, self).__init__()
|
||||||
|
|
||||||
|
img = hopper().resize((1000,1000))
|
||||||
|
|
||||||
|
qimage = ImageQt.ImageQt(img)
|
||||||
|
|
||||||
|
pixmap1 = QtGui.QPixmap.fromImage(qimage)
|
||||||
|
|
||||||
|
hbox = QtGui.QHBoxLayout(self)
|
||||||
|
|
||||||
|
lbl = QtGui.QLabel(self)
|
||||||
|
# Segfault in the problem
|
||||||
|
lbl.setPixmap(pixmap1.copy())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
app = QtGui.QApplication(sys.argv)
|
||||||
|
ex = Example()
|
||||||
|
sys.exit(app.exec_())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user