From 86a64dfd60939a4c549de3c8e43cfc4adc50e341 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/3] Add Qt5 support. --- PIL/ImageQt5.py | 85 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 PIL/ImageQt5.py diff --git a/PIL/ImageQt5.py b/PIL/ImageQt5.py new file mode 100644 index 000000000..69efd18f9 --- /dev/null +++ b/PIL/ImageQt5.py @@ -0,0 +1,85 @@ +# +# The Python Imaging Library. +# $Id$ +# +# a simple Qt image interface. +# +# history: +# 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 +# +# Copyright (c) 2006 by Secret Labs AB +# Copyright (c) 2006 by Fredrik Lundh +# +# See the README file for information on usage and redistribution. +# + +from PIL import Image +from PIL._util import isPath + +from PyQt5.QtGui import QImage, qRgb + +## +# (Internal) Turns an RGB color into a Qt compatible color integer. + +def rgb(r, g, b): + # 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 + +## +# An PIL image wrapper for Qt. This is a subclass of PyQt5's QImage +# class. +# +# @param im A PIL Image object, or a file name (given either as Python +# string or a PyQt string object). + +class ImageQt(QImage): + + def __init__(self, im): + + data = None + colortable = None + + # handle filename, if given instead of image name + if hasattr(im, "toUtf8"): + # FIXME - is this really the best way to do this? + im = unicode(im.toUtf8(), "utf-8") + if isPath(im): + im = Image.open(im) + + if im.mode == "1": + format = QImage.Format_Mono + elif im.mode == "L": + format = QImage.Format_Indexed8 + colortable = [] + for i in range(256): + colortable.append(rgb(i, i, i)) + elif im.mode == "P": + format = QImage.Format_Indexed8 + colortable = [] + palette = im.getpalette() + for i in range(0, len(palette), 3): + colortable.append(rgb(*palette[i:i+3])) + elif im.mode == "RGB": + data = im.tobytes("raw", "BGRX") + format = QImage.Format_RGB32 + elif im.mode == "RGBA": + try: + data = im.tobytes("raw", "BGRA") + except SystemError: + # workaround for earlier versions + r, g, b, a = im.split() + im = Image.merge("RGBA", (b, g, r, a)) + format = QImage.Format_ARGB32 + else: + raise ValueError("unsupported image mode %r" % im.mode) + + # must keep a reference, or Qt will crash! + self.__data = data or im.tobytes() + + QImage.__init__(self, self.__data, im.size[0], im.size[1], format) + + if colortable: + self.setColorTable(colortable) From 6121dd5aa5cd93d1c324067e58b9f985c0ebdef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Ballier?= Date: Tue, 5 Nov 2013 19:14:15 +0100 Subject: [PATCH 2/3] Add Qt5 support. --- PIL/ImageQt5.py | 1 + 1 file changed, 1 insertion(+) diff --git a/PIL/ImageQt5.py b/PIL/ImageQt5.py index 69efd18f9..936e3698f 100644 --- a/PIL/ImageQt5.py +++ b/PIL/ImageQt5.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-05 fl: add support for Qt5 (aurelien.ballier@cyclonit.com) # # Copyright (c) 2006 by Secret Labs AB # Copyright (c) 2006 by Fredrik Lundh From 2a3422335b2495082797cf5d099e053011020934 Mon Sep 17 00:00:00 2001 From: Aurelien Ballier Date: Wed, 13 Nov 2013 12:34:09 +0100 Subject: [PATCH 3/3] Fix PyQt4&5 support. --- PIL/ImageQt.py | 6 +++- PIL/ImageQt5.py | 86 ------------------------------------------------- 2 files changed, 5 insertions(+), 87 deletions(-) delete mode 100644 PIL/ImageQt5.py 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. diff --git a/PIL/ImageQt5.py b/PIL/ImageQt5.py deleted file mode 100644 index 936e3698f..000000000 --- a/PIL/ImageQt5.py +++ /dev/null @@ -1,86 +0,0 @@ -# -# The Python Imaging Library. -# $Id$ -# -# a simple Qt image interface. -# -# history: -# 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-05 fl: add support for Qt5 (aurelien.ballier@cyclonit.com) -# -# Copyright (c) 2006 by Secret Labs AB -# Copyright (c) 2006 by Fredrik Lundh -# -# See the README file for information on usage and redistribution. -# - -from PIL import Image -from PIL._util import isPath - -from PyQt5.QtGui import QImage, qRgb - -## -# (Internal) Turns an RGB color into a Qt compatible color integer. - -def rgb(r, g, b): - # 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 - -## -# An PIL image wrapper for Qt. This is a subclass of PyQt5's QImage -# class. -# -# @param im A PIL Image object, or a file name (given either as Python -# string or a PyQt string object). - -class ImageQt(QImage): - - def __init__(self, im): - - data = None - colortable = None - - # handle filename, if given instead of image name - if hasattr(im, "toUtf8"): - # FIXME - is this really the best way to do this? - im = unicode(im.toUtf8(), "utf-8") - if isPath(im): - im = Image.open(im) - - if im.mode == "1": - format = QImage.Format_Mono - elif im.mode == "L": - format = QImage.Format_Indexed8 - colortable = [] - for i in range(256): - colortable.append(rgb(i, i, i)) - elif im.mode == "P": - format = QImage.Format_Indexed8 - colortable = [] - palette = im.getpalette() - for i in range(0, len(palette), 3): - colortable.append(rgb(*palette[i:i+3])) - elif im.mode == "RGB": - data = im.tobytes("raw", "BGRX") - format = QImage.Format_RGB32 - elif im.mode == "RGBA": - try: - data = im.tobytes("raw", "BGRA") - except SystemError: - # workaround for earlier versions - r, g, b, a = im.split() - im = Image.merge("RGBA", (b, g, r, a)) - format = QImage.Format_ARGB32 - else: - raise ValueError("unsupported image mode %r" % im.mode) - - # must keep a reference, or Qt will crash! - self.__data = data or im.tobytes() - - QImage.__init__(self, self.__data, im.size[0], im.size[1], format) - - if colortable: - self.setColorTable(colortable)