Pillow/Tests/test_imageqt.py

73 lines
1.9 KiB
Python
Raw Normal View History

2020-03-27 11:50:51 +03:00
import pytest
from PIL import ImageQt
from .helper import PillowTestCase, hopper
if ImageQt.qt_is_installed:
2015-06-19 08:35:56 +03:00
from PIL.ImageQt import qRgba
2020-03-27 11:50:51 +03:00
def skip_if_qt_is_not_installed():
pass
2019-06-13 18:54:46 +03:00
else:
2019-06-13 18:54:46 +03:00
2020-03-27 11:50:51 +03:00
def skip_if_qt_is_not_installed():
return pytest.mark.skip(reason="Qt bindings are not installed")
2014-06-10 13:10:47 +04:00
class PillowQtTestCase:
2014-06-10 13:10:47 +04:00
def setUp(self):
skip_if_qt_is_not_installed(self)
def tearDown(self):
pass
2015-06-19 08:36:23 +03:00
class PillowQPixmapTestCase(PillowQtTestCase):
def setUp(self):
super().setUp()
2015-06-19 08:35:56 +03:00
try:
2019-06-13 18:54:46 +03:00
if ImageQt.qt_version == "5":
2015-06-19 08:35:56 +03:00
from PyQt5.QtGui import QGuiApplication
2019-06-13 18:54:46 +03:00
elif ImageQt.qt_version == "side2":
2018-08-03 16:07:19 +03:00
from PySide2.QtGui import QGuiApplication
2015-06-19 08:35:56 +03:00
except ImportError:
2019-06-13 18:54:46 +03:00
self.skipTest("QGuiApplication not installed")
2015-06-19 08:36:23 +03:00
self.app = QGuiApplication([])
def tearDown(self):
super().tearDown()
self.app.quit()
class TestImageQt(PillowQtTestCase, PillowTestCase):
2014-06-10 13:10:47 +04:00
def test_rgb(self):
2018-06-23 03:58:41 +03:00
# from https://doc.qt.io/archives/qt-4.8/qcolor.html
2014-06-10 13:10:47 +04:00
# typedef QRgb
# An ARGB quadruplet on the format #AARRGGBB,
# equivalent to an unsigned int.
2019-06-13 18:54:46 +03:00
if ImageQt.qt_version == "5":
2015-06-19 08:35:56 +03:00
from PyQt5.QtGui import qRgb
2019-06-13 18:54:46 +03:00
elif ImageQt.qt_version == "side2":
2018-08-03 16:07:19 +03:00
from PySide2.QtGui import qRgb
2014-06-10 13:10:47 +04:00
assert qRgb(0, 0, 0) == qRgba(0, 0, 0, 255)
2014-06-10 13:10:47 +04:00
def checkrgb(r, g, b):
val = ImageQt.rgb(r, g, b)
2019-06-13 18:54:46 +03:00
val = val % 2 ** 24 # drop the alpha
assert val >> 16 == r
assert ((val >> 8) % 2 ** 8) == g
assert val % 2 ** 8 == b
2014-06-10 13:10:47 +04:00
checkrgb(0, 0, 0)
checkrgb(255, 0, 0)
checkrgb(0, 255, 0)
checkrgb(0, 0, 255)
2015-06-19 11:23:17 +03:00
def test_image(self):
2019-06-13 18:54:46 +03:00
for mode in ("1", "RGB", "RGBA", "L", "P"):
2015-06-19 11:23:17 +03:00
ImageQt.ImageQt(hopper(mode))