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.
|
|
|
|
#
|
|
|
|
|
2014-09-15 22:24:56 +04:00
|
|
|
import PIL
|
2013-06-30 15:04:42 +04:00
|
|
|
from PIL._util import isPath
|
2015-06-21 09:31:51 +03:00
|
|
|
from io import BytesIO
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-09-15 22:24:56 +04:00
|
|
|
qt_is_installed = True
|
2015-06-19 08:35:56 +03:00
|
|
|
qt_version = None
|
2014-09-15 22:24:56 +04:00
|
|
|
try:
|
2015-06-19 08:35:56 +03:00
|
|
|
from PyQt5.QtGui import QImage, qRgba, QPixmap
|
2015-05-07 13:26:26 +03:00
|
|
|
from PyQt5.QtCore import QBuffer, QIODevice
|
2015-06-19 08:35:56 +03:00
|
|
|
qt_version = '5'
|
2014-09-15 22:24:56 +04:00
|
|
|
except ImportError:
|
2015-05-07 13:26:26 +03:00
|
|
|
try:
|
2015-06-19 08:35:56 +03:00
|
|
|
from PyQt4.QtGui import QImage, qRgba, QPixmap
|
2015-05-07 13:26:26 +03:00
|
|
|
from PyQt4.QtCore import QBuffer, QIODevice
|
2015-06-19 08:35:56 +03:00
|
|
|
qt_version = '4'
|
2015-05-07 13:26:26 +03:00
|
|
|
except ImportError:
|
|
|
|
try:
|
2015-06-19 08:35:56 +03:00
|
|
|
from PySide.QtGui import QImage, qRgba, QPixmap
|
2015-05-07 13:26:26 +03:00
|
|
|
from PySide.QtCore import QBuffer, QIODevice
|
2015-06-19 08:35:56 +03:00
|
|
|
qt_version = 'side'
|
2015-05-07 13:26:26 +03:00
|
|
|
except ImportError:
|
|
|
|
qt_is_installed = False
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-04-24 02:26:52 +03:00
|
|
|
|
2013-11-21 10:20:13 +04:00
|
|
|
def rgb(r, g, b, a=255):
|
2014-09-15 22:24:56 +04:00
|
|
|
"""(Internal) Turns an RGB color into a Qt compatible color integer."""
|
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
|
|
|
|
|
|
|
|
2015-06-19 08:36:23 +03:00
|
|
|
# :param im A PIL Image object, or a file name
|
|
|
|
# (given either as Python string or a PyQt string object)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-09-15 22:24:56 +04:00
|
|
|
def fromqimage(im):
|
|
|
|
buffer = QBuffer()
|
|
|
|
buffer.open(QIODevice.ReadWrite)
|
|
|
|
im.save(buffer, 'ppm')
|
2015-06-21 09:31:51 +03:00
|
|
|
|
|
|
|
b = BytesIO()
|
2014-09-15 22:24:56 +04:00
|
|
|
try:
|
2015-06-21 09:31:51 +03:00
|
|
|
b.write(buffer.data())
|
2014-09-15 22:24:56 +04:00
|
|
|
except TypeError:
|
|
|
|
# workaround for Python 2
|
2015-06-21 09:31:51 +03:00
|
|
|
b.write(str(buffer.data()))
|
2014-09-15 22:24:56 +04:00
|
|
|
buffer.close()
|
2015-06-21 09:31:51 +03:00
|
|
|
b.seek(0)
|
|
|
|
|
|
|
|
return PIL.Image.open(b)
|
2014-09-15 22:24:56 +04:00
|
|
|
|
|
|
|
|
|
|
|
def fromqpixmap(im):
|
|
|
|
return fromqimage(im)
|
|
|
|
# buffer = QBuffer()
|
|
|
|
# buffer.open(QIODevice.ReadWrite)
|
|
|
|
# # im.save(buffer)
|
|
|
|
# # What if png doesn't support some image features like animation?
|
|
|
|
# im.save(buffer, 'ppm')
|
|
|
|
# bytes_io = BytesIO()
|
|
|
|
# bytes_io.write(buffer.data())
|
|
|
|
# buffer.close()
|
|
|
|
# bytes_io.seek(0)
|
|
|
|
# return PIL.Image.open(bytes_io)
|
|
|
|
|
|
|
|
|
|
|
|
def _toqclass_helper(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?
|
|
|
|
if str is bytes:
|
|
|
|
im = unicode(im.toUtf8(), "utf-8")
|
|
|
|
else:
|
|
|
|
im = str(im.toUtf8(), "utf-8")
|
|
|
|
if isPath(im):
|
|
|
|
im = PIL.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 = PIL.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!
|
|
|
|
__data = data or im.tobytes()
|
|
|
|
return {
|
|
|
|
'data': __data, 'im': im, 'format': format, 'colortable': colortable
|
|
|
|
}
|
|
|
|
|
2015-05-07 12:55:52 +03:00
|
|
|
##
|
2015-06-21 09:31:51 +03:00
|
|
|
# An PIL image wrapper for Qt. This is a subclass of PyQt's QImage
|
2015-05-07 12:55:52 +03:00
|
|
|
# class.
|
|
|
|
#
|
|
|
|
# @param im A PIL Image object, or a file name (given either as Python
|
|
|
|
# string or a PyQt string object).
|
|
|
|
|
2015-05-07 13:26:26 +03:00
|
|
|
if qt_is_installed:
|
|
|
|
class ImageQt(QImage):
|
|
|
|
|
|
|
|
def __init__(self, im):
|
|
|
|
im_data = _toqclass_helper(im)
|
|
|
|
QImage.__init__(self,
|
2015-06-19 08:36:23 +03:00
|
|
|
im_data['data'], im_data['im'].size[0],
|
|
|
|
im_data['im'].size[1], im_data['format'])
|
2015-05-07 13:26:26 +03:00
|
|
|
if im_data['colortable']:
|
2015-06-19 08:35:56 +03:00
|
|
|
self.setColorTable(im_data['colortable'])
|
|
|
|
|
|
|
|
|
|
|
|
def toqimage(im):
|
|
|
|
return ImageQt(im)
|
|
|
|
|
|
|
|
|
|
|
|
def toqpixmap(im):
|
2015-06-21 09:31:51 +03:00
|
|
|
# # This doesn't work. For now using a dumb approach.
|
2015-06-19 08:35:56 +03:00
|
|
|
# im_data = _toqclass_helper(im)
|
|
|
|
# result = QPixmap(im_data['im'].size[0], im_data['im'].size[1])
|
|
|
|
# result.loadFromData(im_data['data'])
|
|
|
|
# Fix some strange bug that causes
|
|
|
|
if im.mode == 'RGB':
|
|
|
|
im = im.convert('RGBA')
|
2015-06-21 09:31:51 +03:00
|
|
|
|
2015-06-19 08:35:56 +03:00
|
|
|
qimage = toqimage(im)
|
2015-06-19 08:36:23 +03:00
|
|
|
return QPixmap.fromImage(qimage)
|