Merge pull request #1217 from radarhere/qt-convert

Add functions to convert: Image <-> QImage; Image <-> QPixmap
This commit is contained in:
wiredfool 2015-06-22 11:32:55 -07:00
commit cea766e980
7 changed files with 322 additions and 82 deletions

View File

@ -1936,6 +1936,20 @@ class Image(object):
im = self.im.effect_spread(distance) im = self.im.effect_spread(distance)
return self._new(im) return self._new(im)
def toqimage(self):
"""Returns a QImage copy of this image"""
from PIL import ImageQt
if not ImageQt.qt_is_installed:
raise ImportError("Qt bindings are not installed")
return ImageQt.toqimage(self)
def toqpixmap(self):
"""Returns a QPixmap copy of this image"""
from PIL import ImageQt
if not ImageQt.qt_is_installed:
raise ImportError("Qt bindings are not installed")
return ImageQt.toqpixmap(self)
# -------------------------------------------------------------------- # --------------------------------------------------------------------
# Lazy operations # Lazy operations
@ -2185,6 +2199,22 @@ def fromarray(obj, mode=None):
return frombuffer(mode, size, obj, "raw", rawmode, 0, 1) return frombuffer(mode, size, obj, "raw", rawmode, 0, 1)
def fromqimage(im):
"""Creates an image instance from a QImage image"""
from PIL import ImageQt
if not ImageQt.qt_is_installed:
raise ImportError("Qt bindings are not installed")
return ImageQt.fromqimage(im)
def fromqpixmap(im):
"""Creates an image instance from a QPixmap image"""
from PIL import ImageQt
if not ImageQt.qt_is_installed:
raise ImportError("Qt bindings are not installed")
return ImageQt.fromqpixmap(im)
_fromarray_typemap = { _fromarray_typemap = {
# (shape, typestr) => mode, rawmode # (shape, typestr) => mode, rawmode
# first two members of shape are set to one # first two members of shape are set to one

View File

@ -16,43 +16,72 @@
# See the README file for information on usage and redistribution. # See the README file for information on usage and redistribution.
# #
from PIL import Image import PIL
from PIL._util import isPath from PIL._util import isPath
import sys from io import BytesIO
if 'PyQt4.QtGui' not in sys.modules: qt_is_installed = True
qt_version = None
try: try:
from PyQt5.QtGui import QImage, qRgba from PyQt5.QtGui import QImage, qRgba, QPixmap
except: from PyQt5.QtCore import QBuffer, QIODevice
qt_version = '5'
except ImportError:
try: try:
from PyQt4.QtGui import QImage, qRgba from PyQt4.QtGui import QImage, qRgba, QPixmap
except: from PyQt4.QtCore import QBuffer, QIODevice
from PySide.QtGui import QImage, qRgba qt_version = '4'
except ImportError:
else: #PyQt4 is used try:
from PyQt4.QtGui import QImage, qRgba from PySide.QtGui import QImage, qRgba, QPixmap
from PySide.QtCore import QBuffer, QIODevice
## qt_version = 'side'
# (Internal) Turns an RGB color into a Qt compatible color integer. except ImportError:
qt_is_installed = False
def rgb(r, g, b, a=255): def rgb(r, g, b, a=255):
"""(Internal) Turns an RGB color into a Qt compatible color integer."""
# use qRgb to pack the colors, and then turn the resulting long # use qRgb to pack the colors, and then turn the resulting long
# into a negative integer with the same bitpattern. # into a negative integer with the same bitpattern.
return (qRgba(r, g, b, a) & 0xffffffff) return (qRgba(r, g, b, a) & 0xffffffff)
## # :param im A PIL Image object, or a file name
# An PIL image wrapper for Qt. This is a subclass of PyQt4's QImage # (given either as Python string or a PyQt string object)
# 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 fromqimage(im):
buffer = QBuffer()
buffer.open(QIODevice.ReadWrite)
im.save(buffer, 'ppm')
def __init__(self, im): b = BytesIO()
try:
b.write(buffer.data())
except TypeError:
# workaround for Python 2
b.write(str(buffer.data()))
buffer.close()
b.seek(0)
return PIL.Image.open(b)
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 data = None
colortable = None colortable = None
@ -64,7 +93,7 @@ class ImageQt(QImage):
else: else:
im = str(im.toUtf8(), "utf-8") im = str(im.toUtf8(), "utf-8")
if isPath(im): if isPath(im):
im = Image.open(im) im = PIL.Image.open(im)
if im.mode == "1": if im.mode == "1":
format = QImage.Format_Mono format = QImage.Format_Mono
@ -88,15 +117,48 @@ class ImageQt(QImage):
except SystemError: except SystemError:
# workaround for earlier versions # workaround for earlier versions
r, g, b, a = im.split() r, g, b, a = im.split()
im = Image.merge("RGBA", (b, g, r, a)) im = PIL.Image.merge("RGBA", (b, g, r, a))
format = QImage.Format_ARGB32 format = QImage.Format_ARGB32
else: else:
raise ValueError("unsupported image mode %r" % im.mode) raise ValueError("unsupported image mode %r" % im.mode)
# must keep a reference, or Qt will crash! # must keep a reference, or Qt will crash!
self.__data = data or im.tobytes() __data = data or im.tobytes()
return {
'data': __data, 'im': im, 'format': format, 'colortable': colortable
}
QImage.__init__(self, self.__data, im.size[0], im.size[1], format) ##
# An PIL image wrapper for Qt. This is a subclass of PyQt's QImage
# class.
#
# @param im A PIL Image object, or a file name (given either as Python
# string or a PyQt string object).
if colortable: if qt_is_installed:
self.setColorTable(colortable) class ImageQt(QImage):
def __init__(self, im):
im_data = _toqclass_helper(im)
QImage.__init__(self,
im_data['data'], im_data['im'].size[0],
im_data['im'].size[1], im_data['format'])
if im_data['colortable']:
self.setColorTable(im_data['colortable'])
def toqimage(im):
return ImageQt(im)
def toqpixmap(im):
# # This doesn't work. For now using a dumb approach.
# 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')
qimage = toqimage(im)
return QPixmap.fromImage(qimage)

View File

@ -0,0 +1,33 @@
from helper import unittest, PillowTestCase, hopper
from test_imageqt import PillowQtTestCase
from PIL import ImageQt
class TestFromQImage(PillowQtTestCase, PillowTestCase):
def roundtrip(self, expected):
result = ImageQt.fromqimage(expected.toqimage())
# Qt saves all images as rgb
self.assert_image_equal(result, expected.convert('RGB'))
def test_sanity_1(self):
self.roundtrip(hopper('1'))
def test_sanity_rgb(self):
self.roundtrip(hopper('RGB'))
def test_sanity_rgba(self):
self.roundtrip(hopper('RGBA'))
def test_sanity_l(self):
self.roundtrip(hopper('L'))
def test_sanity_p(self):
self.roundtrip(hopper('P'))
if __name__ == '__main__':
unittest.main()
# End of file

View File

@ -0,0 +1,34 @@
from helper import unittest, PillowTestCase, hopper
from test_imageqt import PillowQtTestCase, PillowQPixmapTestCase
from PIL import ImageQt
class TestFromQPixmap(PillowQPixmapTestCase, PillowTestCase):
def roundtrip(self, expected):
PillowQtTestCase.setUp(self)
result = ImageQt.fromqpixmap(ImageQt.toqpixmap(expected))
# Qt saves all pixmaps as rgb
self.assert_image_equal(result, expected.convert('RGB'))
def test_sanity_1(self):
self.roundtrip(hopper('1'))
def test_sanity_rgb(self):
self.roundtrip(hopper('RGB'))
def test_sanity_rgba(self):
self.roundtrip(hopper('RGBA'))
def test_sanity_l(self):
self.roundtrip(hopper('L'))
def test_sanity_p(self):
self.roundtrip(hopper('P'))
if __name__ == '__main__':
unittest.main()
# End of file

View File

@ -0,0 +1,29 @@
from helper import unittest, PillowTestCase, hopper
from test_imageqt import PillowQtTestCase
from PIL import ImageQt
if ImageQt.qt_is_installed:
from PIL.ImageQt import QImage
class TestToQImage(PillowQtTestCase, PillowTestCase):
def test_sanity(self):
PillowQtTestCase.setUp(self)
for mode in ('1', 'RGB', 'RGBA', 'L', 'P'):
data = ImageQt.toqimage(hopper(mode))
self.assertTrue(isinstance(data, QImage))
self.assertFalse(data.isNull())
# Test saving the file
tempfile = self.tempfile('temp_{}.png'.format(mode))
data.save(tempfile)
if __name__ == '__main__':
unittest.main()
# End of file

View File

@ -0,0 +1,29 @@
from helper import unittest, PillowTestCase, hopper
from test_imageqt import PillowQtTestCase, PillowQPixmapTestCase
from PIL import ImageQt
if ImageQt.qt_is_installed:
from PIL.ImageQt import QPixmap
class TestToQPixmap(PillowQPixmapTestCase, PillowTestCase):
def test_sanity(self):
PillowQtTestCase.setUp(self)
for mode in ('1', 'RGB', 'RGBA', 'L', 'P'):
data = ImageQt.toqpixmap(hopper(mode))
self.assertTrue(isinstance(data, QPixmap))
self.assertFalse(data.isNull())
# Test saving the file
tempfile = self.tempfile('temp_{}.png'.format(mode))
data.save(tempfile)
if __name__ == '__main__':
unittest.main()
# End of file

View File

@ -1,38 +1,61 @@
from helper import unittest, PillowTestCase, hopper from helper import unittest, PillowTestCase, hopper
try:
from PIL import ImageQt from PIL import ImageQt
from PyQt5.QtGui import QImage, qRgb, qRgba
except:
try: if ImageQt.qt_is_installed:
from PyQt4.QtGui import QImage, qRgb, qRgba from PIL.ImageQt import qRgba
except:
try: def skip_if_qt_is_not_installed(_):
from PySide.QtGui import QImage, qRgb, qRgba pass
except: else:
# Will be skipped in setUp def skip_if_qt_is_not_installed(test_case):
test_case.skipTest('Qt bindings are not installed')
class PillowQtTestCase(object):
def setUp(self):
skip_if_qt_is_not_installed(self)
def tearDown(self):
pass pass
class TestImageQt(PillowTestCase): class PillowQPixmapTestCase(PillowQtTestCase):
def setUp(self): def setUp(self):
PillowQtTestCase.setUp(self)
try: try:
from PyQt5.QtGui import QImage, qRgb, qRgba if ImageQt.qt_version == '5':
from PyQt5.QtGui import QGuiApplication
elif ImageQt.qt_version == '4':
from PyQt4.QtGui import QGuiApplication
elif ImageQt.qt_version == 'side':
from PySide.QtGui import QGuiApplication
except ImportError: except ImportError:
try: self.skipTest('QGuiApplication not installed')
from PyQt4.QtGui import QImage, qRgb, qRgba
except ImportError: self.app = QGuiApplication([])
try:
from PySide.QtGui import QImage, qRgb, qRgba def tearDown(self):
except ImportError: PillowQtTestCase.tearDown(self)
self.skipTest('PyQt4 or 5 or PySide not installed') self.app.quit()
class TestImageQt(PillowQtTestCase, PillowTestCase):
def test_rgb(self): def test_rgb(self):
# from https://qt-project.org/doc/qt-4.8/qcolor.html # from https://doc.qt.io/qt-4.8/qcolor.html
# typedef QRgb # typedef QRgb
# An ARGB quadruplet on the format #AARRGGBB, # An ARGB quadruplet on the format #AARRGGBB,
# equivalent to an unsigned int. # equivalent to an unsigned int.
if ImageQt.qt_version == '5':
from PyQt5.QtGui import qRgb
elif ImageQt.qt_version == '4':
from PyQt4.QtGui import qRgb
elif ImageQt.qt_version == 'side':
from PySide.QtGui import qRgb
self.assertEqual(qRgb(0, 0, 0), qRgba(0, 0, 0, 255)) self.assertEqual(qRgb(0, 0, 0), qRgba(0, 0, 0, 255))