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) return self._new(im)
def toqimage(self): def toqimage(self):
"""Returns a QImage copy of this image"""
from PIL import ImageQt from PIL import ImageQt
if not ImageQt.qt_is_installed: if not ImageQt.qt_is_installed:
raise ImportError("Qt bindings are not installed") raise ImportError("Qt bindings are not installed")
return ImageQt.toqimage(self) return ImageQt.toqimage(self)
def toqpixmap(self): def toqpixmap(self):
"""Returns a QPixmap copy of this image"""
from PIL import ImageQt from PIL import ImageQt
if not ImageQt.qt_is_installed: if not ImageQt.qt_is_installed:
raise ImportError("Qt bindings are not installed") raise ImportError("Qt bindings are not installed")
@ -2199,6 +2201,7 @@ def fromarray(obj, mode=None):
def fromqimage(im): def fromqimage(im):
"""Creates an image instance from a QImage image"""
from PIL import ImageQt from PIL import ImageQt
if not ImageQt.qt_is_installed: if not ImageQt.qt_is_installed:
raise ImportError("Qt bindings are not installed") raise ImportError("Qt bindings are not installed")
@ -2206,6 +2209,7 @@ def fromqimage(im):
def fromqpixmap(im): def fromqpixmap(im):
"""Creates an image instance from a QPixmap image"""
from PIL import ImageQt from PIL import ImageQt
if not ImageQt.qt_is_installed: if not ImageQt.qt_is_installed:
raise ImportError("Qt bindings are not installed") raise ImportError("Qt bindings are not installed")

View File

@ -18,6 +18,7 @@
import PIL import PIL
from PIL._util import isPath from PIL._util import isPath
from io import BytesIO
qt_is_installed = True qt_is_installed = True
qt_version = None qt_version = None
@ -38,8 +39,6 @@ except ImportError:
except ImportError: except ImportError:
qt_is_installed = False qt_is_installed = False
from io import BytesIO
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.""" """(Internal) Turns an RGB color into a Qt compatible color integer."""
@ -55,15 +54,17 @@ def fromqimage(im):
buffer = QBuffer() buffer = QBuffer()
buffer.open(QIODevice.ReadWrite) buffer.open(QIODevice.ReadWrite)
im.save(buffer, 'ppm') im.save(buffer, 'ppm')
bytes_io = BytesIO()
b = BytesIO()
try: try:
bytes_io.write(buffer.data()) b.write(buffer.data())
except TypeError: except TypeError:
# workaround for Python 2 # workaround for Python 2
bytes_io.write(str(buffer.data())) b.write(str(buffer.data()))
buffer.close() buffer.close()
bytes_io.seek(0) b.seek(0)
return PIL.Image.open(bytes_io)
return PIL.Image.open(b)
def fromqpixmap(im): 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. # class.
# #
# @param im A PIL Image object, or a file name (given either as Python # @param im A PIL Image object, or a file name (given either as Python
@ -151,13 +152,13 @@ def toqimage(im):
def toqpixmap(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) # im_data = _toqclass_helper(im)
# result = QPixmap(im_data['im'].size[0], im_data['im'].size[1]) # result = QPixmap(im_data['im'].size[0], im_data['im'].size[1])
# result.loadFromData(im_data['data']) # result.loadFromData(im_data['data'])
# Fix some strange bug that causes # Fix some strange bug that causes
if im.mode == 'RGB': if im.mode == 'RGB':
im = im.convert('RGBA') im = im.convert('RGBA')
qimage = toqimage(im) qimage = toqimage(im)
qimage.save('/tmp/hopper_{}_qpixmap_qimage.png'.format(im.mode))
return QPixmap.fromImage(qimage) return QPixmap.fromImage(qimage)

View File

@ -14,10 +14,14 @@ class TestToQImage(PillowQtTestCase, PillowTestCase):
PillowQtTestCase.setUp(self) PillowQtTestCase.setUp(self)
for mode in ('1', 'RGB', 'RGBA', 'L', 'P'): for mode in ('1', 'RGB', 'RGBA', 'L', 'P'):
data = ImageQt.toqimage(hopper(mode)) data = ImageQt.toqimage(hopper(mode))
data.save('/tmp/hopper_{}_qimage.png'.format(mode))
self.assertTrue(isinstance(data, QImage)) self.assertTrue(isinstance(data, QImage))
self.assertFalse(data.isNull()) self.assertFalse(data.isNull())
# Test saving the file
tempfile = self.tempfile('temp_{}.png'.format(mode))
data.save(tempfile)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -11,14 +11,17 @@ class TestToQPixmap(PillowQPixmapTestCase, PillowTestCase):
def test_sanity(self): def test_sanity(self):
PillowQtTestCase.setUp(self) PillowQtTestCase.setUp(self)
QPixmap('Tests/images/hopper.ppm').save(
'/tmp/hopper_RGB_qpixmap_file.png')
for mode in ('1', 'RGB', 'RGBA', 'L', 'P'): for mode in ('1', 'RGB', 'RGBA', 'L', 'P'):
data = ImageQt.toqpixmap(hopper(mode)) data = ImageQt.toqpixmap(hopper(mode))
data.save('/tmp/hopper_{}_qpixmap.png'.format(mode))
self.assertTrue(isinstance(data, QPixmap)) self.assertTrue(isinstance(data, QPixmap))
self.assertFalse(data.isNull()) self.assertFalse(data.isNull())
# Test saving the file
tempfile = self.tempfile('temp_{}.png'.format(mode))
data.save(tempfile)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()