diff --git a/.travis.yml b/.travis.yml index fdc920ac2..1eecc9c13 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,12 +1,16 @@ language: python +# for python-qt4 +virtualenv: + system_site_packages: true + python: - 2.6 - 2.7 - 3.2 - 3.3 -install: "sudo apt-get -qq install libfreetype6-dev liblcms2-dev libwebp-dev ghostscript" +install: "sudo apt-get -qq install libfreetype6-dev liblcms2-dev libwebp-dev python-qt4 ghostscript"" script: - python setup.py clean diff --git a/PIL/ImageQt.py b/PIL/ImageQt.py index d8b838d25..ca8b14b5c 100644 --- a/PIL/ImageQt.py +++ b/PIL/ImageQt.py @@ -8,6 +8,7 @@ # 2006-06-03 fl: created # 2006-06-04 fl: inherit from QImage instead of wrapping it # 2006-06-05 fl: removed toimage helper; move string support to ImageQt +# 2013-11-13 fl: add support for Qt5 (aurelien.ballier@cyclonit.com) # # Copyright (c) 2006 by Secret Labs AB # Copyright (c) 2006 by Fredrik Lundh @@ -18,15 +19,18 @@ from PIL import Image from PIL._util import isPath -from PyQt4.QtGui import QImage, qRgb +try: + from PyQt5.QtGui import QImage, qRgba +except: + from PyQt4.QtGui import QImage, qRgba ## # (Internal) Turns an RGB color into a Qt compatible color integer. -def rgb(r, g, b): +def rgb(r, g, b, a=255): # use qRgb to pack the colors, and then turn the resulting long # into a negative integer with the same bitpattern. - return (qRgb(r, g, b) & 0xffffff) - 0x1000000 + return (qRgba(r, g, b, a) & 0xffffffff) ## # An PIL image wrapper for Qt. This is a subclass of PyQt4's QImage diff --git a/Tests/test_imageqt.py b/Tests/test_imageqt.py index 8d6ac9f3c..73d1f4b1c 100644 --- a/Tests/test_imageqt.py +++ b/Tests/test_imageqt.py @@ -1,9 +1,37 @@ from tester import * from PIL import Image -try: - from PIL import ImageQt -except ImportError as v: - skip(v) -success() +try: + from PyQt5.QtGui import QImage, qRgb, qRgba +except: + try: + from PyQt4.QtGui import QImage, qRgb, qRgba + except: + skip('PyQT4 or 5 not installed') + +from PIL import ImageQt + +def test_rgb(): + # from https://qt-project.org/doc/qt-4.8/qcolor.html + # typedef QRgb + # An ARGB quadruplet on the format #AARRGGBB, equivalent to an unsigned int. + + assert_equal(qRgb(0,0,0), qRgba(0,0,0,255)) + + def checkrgb(r,g,b): + val = ImageQt.rgb(r,g,b) + val = val % 2**24 # drop the alpha + assert_equal(val >> 16, r) + assert_equal(((val >> 8 ) % 2**8), g) + assert_equal(val % 2**8, b) + + checkrgb(0,0,0) + checkrgb(255,0,0) + checkrgb(0,255,0) + checkrgb(0,0,255) + + +def test_image(): + for mode in ('1', 'RGB', 'RGBA', 'L', 'P'): + assert_no_exception(lambda: ImageQt.ImageQt(lena(mode))) diff --git a/docs/reference/ImageQt.rst b/docs/reference/ImageQt.rst index 2f5cccf45..e63fd99fe 100644 --- a/docs/reference/ImageQt.rst +++ b/docs/reference/ImageQt.rst @@ -4,7 +4,7 @@ :py:mod:`ImageQt` Module ======================== -The :py:mod:`ImageQt` module contains support for creating PyQt4 QImage objects +The :py:mod:`ImageQt` module contains support for creating PyQt4 or PyQt5 QImage objects from PIL images. .. versionadded:: 1.1.6 @@ -14,7 +14,7 @@ from PIL images. Creates an :py:class:`~PIL.ImageQt.ImageQt` object from a PIL :py:class:`~PIL.Image.Image` object. This class is a subclass of QtGui.QImage, which means that you can pass the resulting objects directly - to PyQt4 API functions and methods. + to PyQt4/5 API functions and methods. This operation is currently supported for mode 1, L, P, RGB, and RGBA images. To handle other modes, you need to convert the image first.