From c1da18e0ad541fd3f80de17a766b32e0e032b0ef Mon Sep 17 00:00:00 2001 From: homm Date: Sun, 3 Jul 2016 05:40:34 +0300 Subject: [PATCH 1/2] do not allow to save images discarding alpha channel --- PIL/JpegImagePlugin.py | 1 - Tests/test_file_jpeg.py | 13 +++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/PIL/JpegImagePlugin.py b/PIL/JpegImagePlugin.py index 9d4eaabb1..a08e932ab 100644 --- a/PIL/JpegImagePlugin.py +++ b/PIL/JpegImagePlugin.py @@ -534,7 +534,6 @@ RAWMODE = { "1": "L", "L": "L", "RGB": "RGB", - "RGBA": "RGB", "RGBX": "RGB", "CMYK": "CMYK;I", # assume adobe conventions "YCbCr": "YCbCr", diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 54f8b9fd1..692dc5279 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -450,6 +450,19 @@ class TestFileJpeg(PillowTestCase): # Assert self.assertEqual(im.format, "JPEG") + def test_save_correct_modes(self): + out = BytesIO() + for mode in ['1', 'L', 'RGB', 'RGBX', 'CMYK', 'YCbCr']: + img = Image.new(mode, (20, 20)) + img.save(out, "JPEG") + + def test_save_wrong_modes(self): + # ref https://github.com/python-pillow/Pillow/issues/2005 + out = BytesIO() + for mode in ['LA', 'La', 'RGBA', 'RGBa', 'P']: + img = Image.new(mode, (20, 20)) + self.assertRaises(IOError, img.save, out, "JPEG") + if __name__ == '__main__': unittest.main() From 193c7561392fd12c3bd93bc232d9041c89bec4f6 Mon Sep 17 00:00:00 2001 From: homm Date: Tue, 9 Aug 2016 03:11:35 +0300 Subject: [PATCH 2/2] return implicit RGBA to JPEG save, raise warning --- PIL/JpegImagePlugin.py | 9 +++++++++ Tests/helper.py | 4 ++-- Tests/test_file_jpeg.py | 10 ++++++++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/PIL/JpegImagePlugin.py b/PIL/JpegImagePlugin.py index a08e932ab..a0d066724 100644 --- a/PIL/JpegImagePlugin.py +++ b/PIL/JpegImagePlugin.py @@ -534,6 +534,7 @@ RAWMODE = { "1": "L", "L": "L", "RGB": "RGB", + "RGBA": "RGB", "RGBX": "RGB", "CMYK": "CMYK;I", # assume adobe conventions "YCbCr": "YCbCr", @@ -582,6 +583,14 @@ def _save(im, fp, filename): except KeyError: raise IOError("cannot write mode %s as JPEG" % im.mode) + if im.mode == 'RGBA': + warnings.warn( + 'You are saving RGBA image as JPEG. The alpha channel will be ' + 'discarded. This conversion is deprecated and will be disabled ' + 'in Pillow 3.7. Please, convert the image to RGB explicitly.', + DeprecationWarning + ) + info = im.encoderinfo dpi = info.get("dpi", (0, 0)) diff --git a/Tests/helper.py b/Tests/helper.py index abb2fbd6d..99d102e6c 100644 --- a/Tests/helper.py +++ b/Tests/helper.py @@ -99,7 +99,7 @@ class PillowTestCase(unittest.TestCase): " average pixel value difference %.4f > epsilon %.4f" % ( ave_diff, epsilon)) - def assert_warning(self, warn_class, func): + def assert_warning(self, warn_class, func, *args, **kwargs): import warnings result = None @@ -108,7 +108,7 @@ class PillowTestCase(unittest.TestCase): warnings.simplefilter("always") # Hopefully trigger a warning. - result = func() + result = func(*args, **kwargs) # Verify some things. self.assertGreaterEqual(len(w), 1) diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 692dc5279..0a07df2fa 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -457,12 +457,18 @@ class TestFileJpeg(PillowTestCase): img.save(out, "JPEG") def test_save_wrong_modes(self): - # ref https://github.com/python-pillow/Pillow/issues/2005 out = BytesIO() - for mode in ['LA', 'La', 'RGBA', 'RGBa', 'P']: + for mode in ['LA', 'La', 'RGBa', 'P']: img = Image.new(mode, (20, 20)) self.assertRaises(IOError, img.save, out, "JPEG") + def test_save_modes_with_warnings(self): + # ref https://github.com/python-pillow/Pillow/issues/2005 + out = BytesIO() + for mode in ['RGBA']: + img = Image.new(mode, (20, 20)) + self.assert_warning(DeprecationWarning, img.save, out, "JPEG") + if __name__ == '__main__': unittest.main()