2020-03-28 04:51:28 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2019-07-06 23:40:53 +03:00
|
|
|
from PIL import Image, ImageQt
|
|
|
|
|
2020-03-27 12:30:00 +03:00
|
|
|
from .helper import assert_image_equal, hopper
|
|
|
|
|
2020-03-28 04:51:28 +03:00
|
|
|
pytestmark = pytest.mark.skipif(
|
|
|
|
not ImageQt.qt_is_installed, reason="Qt bindings are not installed"
|
|
|
|
)
|
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
|
2020-08-07 13:28:33 +03:00
|
|
|
from PyQt5.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget
|
2016-10-27 23:57:11 +03:00
|
|
|
except (ImportError, RuntimeError):
|
2019-09-26 21:38:19 +03:00
|
|
|
from PySide2 import QtGui
|
2020-08-07 13:28:33 +03:00
|
|
|
from PySide2.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget
|
2017-04-20 14:14:23 +03:00
|
|
|
|
|
|
|
|
2020-03-27 12:30:00 +03:00
|
|
|
def test_sanity(tmp_path):
|
|
|
|
for mode in ("RGB", "RGBA", "L", "P", "1"):
|
|
|
|
src = hopper(mode)
|
|
|
|
data = ImageQt.toqimage(src)
|
|
|
|
|
|
|
|
assert isinstance(data, QImage)
|
|
|
|
assert not data.isNull()
|
|
|
|
|
|
|
|
# reload directly from the qimage
|
|
|
|
rt = ImageQt.fromqimage(data)
|
|
|
|
if mode in ("L", "P", "1"):
|
|
|
|
assert_image_equal(rt, src.convert("RGB"))
|
|
|
|
else:
|
|
|
|
assert_image_equal(rt, src)
|
|
|
|
|
|
|
|
if mode == "1":
|
|
|
|
# BW appears to not save correctly on QT4 and QT5
|
|
|
|
# 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 = str(tmp_path / "temp_{}.png".format(mode))
|
|
|
|
data.save(tempfile)
|
|
|
|
|
|
|
|
# Check that it actually worked.
|
|
|
|
with Image.open(tempfile) as reloaded:
|
|
|
|
assert_image_equal(reloaded, src)
|
|
|
|
|
|
|
|
|
|
|
|
def test_segfault():
|
|
|
|
app = QApplication([])
|
|
|
|
ex = Example()
|
|
|
|
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())
|