Pillow/Tests/test_qt_image_toqimage.py

90 lines
2.7 KiB
Python
Raw Normal View History

from helper import unittest, PillowTestCase, hopper
from test_imageqt import PillowQtTestCase
2017-02-17 20:03:49 +03:00
from PIL import ImageQt, Image
if ImageQt.qt_is_installed:
2017-02-19 13:04:18 +03:00
from PIL.ImageQt import QImage
try:
from PyQt5 import QtGui
2017-02-17 20:03:49 +03:00
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QLabel, QApplication
QT_VERSION = 5
except (ImportError, RuntimeError):
try:
from PyQt4 import QtGui
2017-02-17 20:03:49 +03:00
from PyQt4.QtGui import QWidget, QHBoxLayout, QLabel, QApplication
QT_VERSION = 4
except (ImportError, RuntimeError):
from PySide import QtGui
2017-02-17 20:03:49 +03:00
from PySide.QtGui import QWidget, QHBoxLayout, QLabel, QApplication
QT_VERSION = 4
2017-04-20 14:14:23 +03:00
class TestToQImage(PillowQtTestCase, PillowTestCase):
def test_sanity(self):
2017-02-17 20:03:49 +03:00
for mode in ('RGB', 'RGBA', 'L', 'P', '1'):
src = hopper(mode)
data = ImageQt.toqimage(src)
2015-06-21 09:31:51 +03:00
self.assertIsInstance(data, QImage)
self.assertFalse(data.isNull())
2017-02-17 20:03:49 +03:00
# 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 mode == '1':
# BW appears to not save correctly on QT4 and QT5
2017-02-17 20:03:49 +03:00
# kicks out errors on console:
2018-06-24 15:32:25 +03:00
# libpng warning: Invalid color type/bit depth combination
# in IHDR
# libpng error: Invalid IHDR data
2017-02-17 20:03:49 +03:00
continue
2017-04-20 14:14:23 +03:00
2015-06-21 09:31:51 +03:00
# Test saving the file
tempfile = self.tempfile('temp_{}.png'.format(mode))
2015-06-21 09:31:51 +03:00
data.save(tempfile)
2017-04-20 14:14:23 +03:00
# Check that it actually worked.
2017-02-17 20:03:49 +03:00
reloaded = Image.open(tempfile)
# Gray images appear to come back in palette mode.
# They're roughly equivalent
if QT_VERSION == 4 and mode == 'L':
src = src.convert('P')
2017-02-17 20:03:49 +03:00
self.assert_image_equal(reloaded, src)
def test_segfault(self):
2017-02-17 20:03:49 +03:00
app = QApplication([])
ex = Example()
2017-02-19 13:04:18 +03:00
assert(app) # Silence warning
assert(ex) # Silence warning
if ImageQt.qt_is_installed:
2017-02-17 20:03:49 +03:00
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
2017-02-19 13:04:18 +03:00
img = hopper().resize((1000, 1000))
qimage = ImageQt.ImageQt(img)
pixmap1 = QtGui.QPixmap.fromImage(qimage)
2017-02-17 20:03:49 +03:00
hbox = QHBoxLayout(self)
2017-02-17 20:03:49 +03:00
lbl = QLabel(self)
# Segfault in the problem
lbl.setPixmap(pixmap1.copy())
if __name__ == '__main__':
unittest.main()