2019-07-06 23:40:53 +03:00
|
|
|
from PIL import Image, ImageQt
|
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
from .helper import PillowTestCase, assert_image_equal, hopper
|
2019-01-13 20:00:12 +03:00
|
|
|
from .test_imageqt import PillowQtTestCase
|
2014-09-15 22:24:56 +04:00
|
|
|
|
|
|
|
if ImageQt.qt_is_installed:
|
2017-02-19 13:04:18 +03:00
|
|
|
from PIL.ImageQt import QImage
|
2016-10-27 23:57:11 +03:00
|
|
|
|
|
|
|
try:
|
|
|
|
from PyQt5 import QtGui
|
2017-02-17 20:03:49 +03:00
|
|
|
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QLabel, QApplication
|
2016-10-27 23:57:11 +03:00
|
|
|
except (ImportError, RuntimeError):
|
2019-09-26 21:38:19 +03:00
|
|
|
from PySide2 import QtGui
|
|
|
|
from PySide2.QtWidgets import QWidget, QHBoxLayout, QLabel, QApplication
|
2017-04-20 14:14:23 +03:00
|
|
|
|
|
|
|
|
2014-09-15 22:24:56 +04:00
|
|
|
class TestToQImage(PillowQtTestCase, PillowTestCase):
|
|
|
|
def test_sanity(self):
|
2019-06-13 18:54:46 +03:00
|
|
|
for mode in ("RGB", "RGBA", "L", "P", "1"):
|
2017-02-17 20:03:49 +03:00
|
|
|
src = hopper(mode)
|
|
|
|
data = ImageQt.toqimage(src)
|
2015-06-21 09:31:51 +03:00
|
|
|
|
2015-12-08 12:31:02 +03:00
|
|
|
self.assertIsInstance(data, QImage)
|
2014-09-15 22:24:56 +04:00
|
|
|
self.assertFalse(data.isNull())
|
|
|
|
|
2017-02-17 20:03:49 +03:00
|
|
|
# reload directly from the qimage
|
|
|
|
rt = ImageQt.fromqimage(data)
|
2019-06-13 18:54:46 +03:00
|
|
|
if mode in ("L", "P", "1"):
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(rt, src.convert("RGB"))
|
2017-02-17 20:03:49 +03:00
|
|
|
else:
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(rt, src)
|
2017-02-17 20:03:49 +03:00
|
|
|
|
2019-06-13 18:54:46 +03:00
|
|
|
if mode == "1":
|
2017-02-20 23:28:13 +03:00
|
|
|
# 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
|
2019-06-13 18:54:46 +03:00
|
|
|
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.
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(tempfile) as reloaded:
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(reloaded, src)
|
2017-02-17 20:03:49 +03:00
|
|
|
|
2016-10-27 23:57:11 +03:00
|
|
|
def test_segfault(self):
|
2017-02-17 20:03:49 +03:00
|
|
|
app = QApplication([])
|
2016-10-27 23:57:11 +03:00
|
|
|
ex = Example()
|
2018-10-02 11:55:28 +03:00
|
|
|
assert app # Silence warning
|
|
|
|
assert ex # Silence warning
|
2016-10-27 23:57:11 +03:00
|
|
|
|
|
|
|
|
|
|
|
if ImageQt.qt_is_installed:
|
|
|
|
|
2019-06-13 18:54:46 +03:00
|
|
|
class Example(QWidget):
|
2016-10-27 23:57:11 +03:00
|
|
|
def __init__(self):
|
2019-09-30 17:56:31 +03:00
|
|
|
super().__init__()
|
2016-10-27 23:57:11 +03:00
|
|
|
|
2017-02-19 13:04:18 +03:00
|
|
|
img = hopper().resize((1000, 1000))
|
2016-10-27 23:57:11 +03:00
|
|
|
|
|
|
|
qimage = ImageQt.ImageQt(img)
|
|
|
|
|
|
|
|
pixmap1 = QtGui.QPixmap.fromImage(qimage)
|
|
|
|
|
2018-10-18 21:49:14 +03:00
|
|
|
QHBoxLayout(self) # hbox
|
2016-10-27 23:57:11 +03:00
|
|
|
|
2017-02-17 20:03:49 +03:00
|
|
|
lbl = QLabel(self)
|
2016-10-27 23:57:11 +03:00
|
|
|
# Segfault in the problem
|
|
|
|
lbl.setPixmap(pixmap1.copy())
|