Further fixes

This commit is contained in:
Andrew Murray 2015-06-21 16:31:51 +10:00
parent c1b1f184b8
commit b553ad7a70
4 changed files with 26 additions and 14 deletions

View File

@ -1937,12 +1937,14 @@ class Image(object):
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")
@ -2199,6 +2201,7 @@ def fromarray(obj, mode=None):
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")
@ -2206,6 +2209,7 @@ def 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")

View File

@ -18,6 +18,7 @@
import PIL
from PIL._util import isPath
from io import BytesIO
qt_is_installed = True
qt_version = None
@ -38,8 +39,6 @@ except ImportError:
except ImportError:
qt_is_installed = False
from io import BytesIO
def rgb(r, g, b, a=255):
"""(Internal) Turns an RGB color into a Qt compatible color integer."""
@ -55,15 +54,17 @@ def fromqimage(im):
buffer = QBuffer()
buffer.open(QIODevice.ReadWrite)
im.save(buffer, 'ppm')
bytes_io = BytesIO()
b = BytesIO()
try:
bytes_io.write(buffer.data())
b.write(buffer.data())
except TypeError:
# workaround for Python 2
bytes_io.write(str(buffer.data()))
b.write(str(buffer.data()))
buffer.close()
bytes_io.seek(0)
return PIL.Image.open(bytes_io)
b.seek(0)
return PIL.Image.open(b)
def fromqpixmap(im):
@ -128,7 +129,7 @@ def _toqclass_helper(im):
}
##
# An PIL image wrapper for Qt. This is a subclass of PyQt4's QImage
# 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
@ -151,13 +152,13 @@ def toqimage(im):
def toqpixmap(im):
# This doesn't work. For now using a dumb approach.
# # 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)
qimage.save('/tmp/hopper_{}_qpixmap_qimage.png'.format(im.mode))
return QPixmap.fromImage(qimage)

View File

@ -14,10 +14,14 @@ class TestToQImage(PillowQtTestCase, PillowTestCase):
PillowQtTestCase.setUp(self)
for mode in ('1', 'RGB', 'RGBA', 'L', 'P'):
data = ImageQt.toqimage(hopper(mode))
data.save('/tmp/hopper_{}_qimage.png'.format(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()

View File

@ -11,14 +11,17 @@ class TestToQPixmap(PillowQPixmapTestCase, PillowTestCase):
def test_sanity(self):
PillowQtTestCase.setUp(self)
QPixmap('Tests/images/hopper.ppm').save(
'/tmp/hopper_RGB_qpixmap_file.png')
for mode in ('1', 'RGB', 'RGBA', 'L', 'P'):
data = ImageQt.toqpixmap(hopper(mode))
data.save('/tmp/hopper_{}_qpixmap.png'.format(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()