Pillow/Tests/test_imageqt.py

72 lines
1.9 KiB
Python
Raw Normal View History

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
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
def skip_if_qt_is_not_installed(test_case):
2019-06-13 18:54:46 +03:00
test_case.skipTest("Qt bindings are not installed")
2014-06-10 13:10:47 +04:00
2015-06-19 08:36:23 +03:00
class PillowQtTestCase(object):
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
self.assertEqual(qRgb(0, 0, 0), qRgba(0, 0, 0, 255))
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
2014-06-10 13:10:47 +04:00
self.assertEqual(val >> 16, r)
2019-06-13 18:54:46 +03:00
self.assertEqual(((val >> 8) % 2 ** 8), g)
self.assertEqual(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))