From 938323bd2be80513ac975668973eb957ac0dde4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Ballier?= Date: Mon, 4 Nov 2013 12:44:41 +0100 Subject: [PATCH 1/6] Add Qt5 support. --- PIL/ImageQt.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/PIL/ImageQt.py b/PIL/ImageQt.py index d8b838d25..b0c477d03 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,7 +19,10 @@ from PIL import Image from PIL._util import isPath -from PyQt4.QtGui import QImage, qRgb +try: + from PyQt5.QtGui import QImage, qRgb +except: + from PyQt4.QtGui import QImage, qRgb ## # (Internal) Turns an RGB color into a Qt compatible color integer. From 29fb4523d5301fd1d1b2d5f5fc8e839d3404790f Mon Sep 17 00:00:00 2001 From: wiredfool Date: Wed, 20 Nov 2013 22:19:28 -0800 Subject: [PATCH 2/6] tests for imageqt4/5 --- Tests/test_imageqt.py | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/Tests/test_imageqt.py b/Tests/test_imageqt.py index 8d6ac9f3c..36e673a90 100644 --- a/Tests/test_imageqt.py +++ b/Tests/test_imageqt.py @@ -1,9 +1,39 @@ 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) + print val + 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'): + print ( "Testing mode %s" % mode) + assert_no_exception(lambda: ImageQt.ImageQt(lena(mode))) From dbecc1cb70264af1333d229fd521731ec2da1f7c Mon Sep 17 00:00:00 2001 From: wiredfool Date: Wed, 20 Nov 2013 22:20:13 -0800 Subject: [PATCH 3/6] Fix for failing ImageQt with mode 'L' image --- PIL/ImageQt.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/PIL/ImageQt.py b/PIL/ImageQt.py index b0c477d03..ca8b14b5c 100644 --- a/PIL/ImageQt.py +++ b/PIL/ImageQt.py @@ -20,17 +20,17 @@ from PIL import Image from PIL._util import isPath try: - from PyQt5.QtGui import QImage, qRgb + from PyQt5.QtGui import QImage, qRgba except: - from PyQt4.QtGui import QImage, qRgb + 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 From 8550570187b007a0dd748199526149f6f9a228bb Mon Sep 17 00:00:00 2001 From: wiredfool Date: Wed, 20 Nov 2013 22:23:26 -0800 Subject: [PATCH 4/6] Qt5 Documentation --- docs/reference/ImageQt.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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. From b0b7c1acb5cacff5c7a48608c6d02ef0d5ec7a32 Mon Sep 17 00:00:00 2001 From: wiredfool Date: Wed, 20 Nov 2013 22:43:39 -0800 Subject: [PATCH 5/6] removed debugging prints --- Tests/test_imageqt.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Tests/test_imageqt.py b/Tests/test_imageqt.py index 36e673a90..73d1f4b1c 100644 --- a/Tests/test_imageqt.py +++ b/Tests/test_imageqt.py @@ -21,7 +21,6 @@ def test_rgb(): def checkrgb(r,g,b): val = ImageQt.rgb(r,g,b) - print val val = val % 2**24 # drop the alpha assert_equal(val >> 16, r) assert_equal(((val >> 8 ) % 2**8), g) @@ -35,5 +34,4 @@ def test_rgb(): def test_image(): for mode in ('1', 'RGB', 'RGBA', 'L', 'P'): - print ( "Testing mode %s" % mode) assert_no_exception(lambda: ImageQt.ImageQt(lena(mode))) From 661e3108f2e9c0b672a94c231ade207f6babdc7a Mon Sep 17 00:00:00 2001 From: wiredfool Date: Wed, 20 Nov 2013 22:44:00 -0800 Subject: [PATCH 6/6] testing pyqt4 on travis --- .travis.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 88118b1b2..bafcd7d14 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" +install: "sudo apt-get -qq install libfreetype6-dev liblcms2-dev libwebp-dev python-qt4" script: - python setup.py clean