2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# 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-13 15:34:09 +04:00
|
|
|
# 2013-11-13 fl: add support for Qt5 (aurelien.ballier@cyclonit.com)
|
2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# Copyright (c) 2006 by Secret Labs AB
|
|
|
|
# Copyright (c) 2006 by Fredrik Lundh
|
|
|
|
#
|
|
|
|
# See the README file for information on usage and redistribution.
|
|
|
|
#
|
|
|
|
|
2013-03-07 20:20:28 +04:00
|
|
|
from PIL import Image
|
2013-06-30 15:04:42 +04:00
|
|
|
from PIL._util import isPath
|
2014-11-11 20:00:00 +03:00
|
|
|
import sys
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-11-11 20:00:00 +03:00
|
|
|
if 'PyQt4.QtGui' not in sys.modules:
|
2014-11-21 01:08:58 +03:00
|
|
|
try:
|
2014-11-27 22:43:45 +03:00
|
|
|
from PyQt5.QtGui import QImage, qRgba
|
2014-11-21 01:08:58 +03:00
|
|
|
except:
|
2014-11-27 22:43:45 +03:00
|
|
|
try:
|
|
|
|
from PyQt4.QtGui import QImage, qRgba
|
|
|
|
except:
|
|
|
|
from PySide.QtGui import QImage, qRgba
|
2014-11-21 01:08:58 +03:00
|
|
|
|
2015-04-24 02:26:52 +03:00
|
|
|
else: #PyQt4 is used
|
2014-11-27 22:43:45 +03:00
|
|
|
from PyQt4.QtGui import QImage, qRgba
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
##
|
|
|
|
# (Internal) Turns an RGB color into a Qt compatible color integer.
|
|
|
|
|
2015-04-24 02:26:52 +03:00
|
|
|
|
2013-11-21 10:20:13 +04:00
|
|
|
def rgb(r, g, b, a=255):
|
2010-07-31 06:52:47 +04:00
|
|
|
# use qRgb to pack the colors, and then turn the resulting long
|
|
|
|
# into a negative integer with the same bitpattern.
|
2013-11-21 10:20:13 +04:00
|
|
|
return (qRgba(r, g, b, a) & 0xffffffff)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# An PIL image wrapper for Qt. This is a subclass of PyQt4'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?
|
2015-05-07 14:47:08 +03:00
|
|
|
if str is bytes:
|
|
|
|
im = unicode(im.toUtf8(), "utf-8")
|
|
|
|
else:
|
|
|
|
im = str(im.toUtf8(), "utf-8")
|
2013-06-30 15:04:42 +04:00
|
|
|
if isPath(im):
|
2010-07-31 06:52:47 +04:00
|
|
|
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":
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
data = im.tobytes("raw", "BGRX")
|
2010-07-31 06:52:47 +04:00
|
|
|
format = QImage.Format_RGB32
|
|
|
|
elif im.mode == "RGBA":
|
|
|
|
try:
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
data = im.tobytes("raw", "BGRA")
|
2010-07-31 06:52:47 +04:00
|
|
|
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!
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
self.__data = data or im.tobytes()
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
QImage.__init__(self, self.__data, im.size[0], im.size[1], format)
|
|
|
|
|
|
|
|
if colortable:
|
|
|
|
self.setColorTable(colortable)
|