diff --git a/PIL/ImageQt.py b/PIL/ImageQt.py index 2131f8264..591d85ff8 100644 --- a/PIL/ImageQt.py +++ b/PIL/ImageQt.py @@ -53,7 +53,12 @@ def rgb(r, g, b, a=255): def fromqimage(im): buffer = QBuffer() buffer.open(QIODevice.ReadWrite) - im.save(buffer, 'ppm') + # preserve alha channel with png + # otherwise ppm is more friendly with Image.open + if im.hasAlphaChannel(): + im.save(buffer, 'png') + else: + im.save(buffer, 'ppm') b = BytesIO() try: diff --git a/Tests/test_image_fromqimage.py b/Tests/test_image_fromqimage.py index 24ec19616..57a95f104 100644 --- a/Tests/test_image_fromqimage.py +++ b/Tests/test_image_fromqimage.py @@ -13,9 +13,15 @@ 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')) + # PIL -> Qt + intermediate = expected.toqimage() + # Qt -> PIL + result = ImageQt.fromqimage(intermediate) + + if intermediate.hasAlphaChannel(): + self.assert_image_equal(result, expected.convert('RGBA')) + else: + self.assert_image_equal(result, expected.convert('RGB')) def test_sanity_1(self): for im in self.files_to_test: