diff --git a/Tests/test_image_toqimage.py b/Tests/test_image_toqimage.py index 4a318c5c4..3e2aa9dd6 100644 --- a/Tests/test_image_toqimage.py +++ b/Tests/test_image_toqimage.py @@ -1,7 +1,7 @@ from helper import unittest, PillowTestCase, hopper from test_imageqt import PillowQtTestCase -from PIL import ImageQt +from PIL import ImageQt, Image if ImageQt.qt_is_installed: @@ -9,39 +9,62 @@ if ImageQt.qt_is_installed: try: from PyQt5 import QtGui + from PyQt5.QtWidgets import QWidget, QHBoxLayout, QLabel, QApplication + QT_VERSION = 5 except (ImportError, RuntimeError): try: from PyQt4 import QtGui + from PyQt4.QtGui import QWidget, QHBoxLayout, QLabel, QApplication + QT_VERSION = 4 except (ImportError, RuntimeError): from PySide import QtGui + from PySide.QtGui import QWidget, QHBoxLayout, QLabel, QApplication + QT_VERSION = 4 - - class TestToQImage(PillowQtTestCase, PillowTestCase): def test_sanity(self): PillowQtTestCase.setUp(self) - for mode in ('1', 'RGB', 'RGBA', 'L', 'P'): - data = ImageQt.toqimage(hopper(mode)) + for mode in ('RGB', 'RGBA', 'L', 'P', '1'): + src = hopper(mode) + data = ImageQt.toqimage(src) self.assertIsInstance(data, QImage) self.assertFalse(data.isNull()) + # reload directly from the qimage + rt = ImageQt.fromqimage(data) + if mode in ('L', 'P', '1'): + self.assert_image_equal(rt, src.convert('RGB')) + else: + self.assert_image_equal(rt, src) + + if QT_VERSION == 5 and mode == '1': + # this combination appears to not save correctly + # kicks out errors on console: + # libpng warning: Invalid color type/bit depth combination in IHDR + # libpng error: Invalid IHDR data + continue + # Test saving the file tempfile = self.tempfile('temp_{}.png'.format(mode)) data.save(tempfile) + # Check that it actually worked. + reloaded = Image.open(tempfile) + self.assert_image_equal(reloaded, src) + def test_segfault(self): PillowQtTestCase.setUp(self) - app = QtGui.QApplication([]) + app = QApplication([]) ex = Example() if ImageQt.qt_is_installed: - class Example(QtGui.QWidget): + class Example(QWidget): def __init__(self): super(Example, self).__init__() @@ -52,9 +75,9 @@ if ImageQt.qt_is_installed: pixmap1 = QtGui.QPixmap.fromImage(qimage) - hbox = QtGui.QHBoxLayout(self) + hbox = QHBoxLayout(self) - lbl = QtGui.QLabel(self) + lbl = QLabel(self) # Segfault in the problem lbl.setPixmap(pixmap1.copy()) @@ -62,7 +85,7 @@ if ImageQt.qt_is_installed: def main(): - app = QtGui.QApplication(sys.argv) + app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())