From a188380ba8b9258b49c993b40347e80e0f019378 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Fri, 30 Jan 2015 19:53:26 -0600 Subject: [PATCH 1/5] Add handling of CMYK images --- skimage/io/_plugins/pil_plugin.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/skimage/io/_plugins/pil_plugin.py b/skimage/io/_plugins/pil_plugin.py index 09d51c7a..f07fc6f9 100644 --- a/skimage/io/_plugins/pil_plugin.py +++ b/skimage/io/_plugins/pil_plugin.py @@ -73,7 +73,7 @@ def pil_to_ndarray(im, dtype=None, img_num=None): frame = im - if not img_num is None and img_num != i: + if img_num is not None and img_num != i: im.getdata()[0] i += 1 continue @@ -90,10 +90,9 @@ def pil_to_ndarray(im, dtype=None, img_num=None): elif im.mode == '1': frame = im.convert('L') - elif 'A' in im.mode: + elif 'A' in im.mode or im.mode == 'CMYK': frame = im.convert('RGBA') - if im.mode.startswith('I;16'): shape = im.size dtype = '>u2' if im.mode.endswith('B') else ' Date: Sat, 31 Jan 2015 20:34:28 -0600 Subject: [PATCH 2/5] Add test for cmyk and use relative imports --- skimage/io/tests/test_pil.py | 37 +++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/skimage/io/tests/test_pil.py b/skimage/io/tests/test_pil.py index 2d58e3e9..84b17bc6 100644 --- a/skimage/io/tests/test_pil.py +++ b/skimage/io/tests/test_pil.py @@ -6,18 +6,18 @@ from numpy.testing import ( from tempfile import NamedTemporaryFile -from skimage import data_dir -from skimage.io import (imread, imsave, use_plugin, reset_plugins, +from ... import data_dir +from .. import (imread, imsave, use_plugin, reset_plugins, Image as ioImage) -from skimage._shared.testing import mono_check, color_check -from skimage._shared._warnings import expected_warnings +from ..._shared.testing import mono_check, color_check +from ..._shared._warnings import expected_warnings from six import BytesIO from PIL import Image -from skimage.io._plugins.pil_plugin import ( +from .._plugins.pil_plugin import ( pil_to_ndarray, ndarray_to_pil, _palette_is_grayscale) - +from ...measure import structural_similarity as ssim def setup(): @@ -183,6 +183,28 @@ def test_multi_page_gif(): assert img2.shape == (280, 500, 3) assert_allclose(img[5], img2) + +def test_cmyk(): + ref = imread(os.path.join(data_dir, 'color.png')) + + img = Image.open(os.path.join(data_dir, 'color.png')) + img = img.convert('CMYK') + + f = NamedTemporaryFile(suffix='.jpg') + fname = f.name + f.close() + img.save(fname) + img.close() + + new = imread(fname)[:, :, :3] + + for i in range(3): + newi = np.ascontiguousarray(new[:, :, i]) + refi = np.ascontiguousarray(ref[:, :, i]) + sim = ssim(refi, newi, dynamic_range=refi.max() - refi.min()) + assert sim > 0.85 + + class TestSaveTIF: def roundtrip(self, dtype, x): f = NamedTemporaryFile(suffix='.tif') @@ -205,4 +227,5 @@ class TestSaveTIF: yield self.roundtrip, dtype, x if __name__ == "__main__": - run_module_suite() + #run_module_suite() + test_cmyk() From 3728c616150010a57a2cd0f1d6c78589831e36d5 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Sat, 31 Jan 2015 21:56:22 -0600 Subject: [PATCH 3/5] Reinstate run_module_suite --- skimage/io/tests/test_pil.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/skimage/io/tests/test_pil.py b/skimage/io/tests/test_pil.py index 84b17bc6..b210ec6f 100644 --- a/skimage/io/tests/test_pil.py +++ b/skimage/io/tests/test_pil.py @@ -227,5 +227,4 @@ class TestSaveTIF: yield self.roundtrip, dtype, x if __name__ == "__main__": - #run_module_suite() - test_cmyk() + run_module_suite() From d8f6a6697b3cf3efea84e35e51856c9d102df355 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Sun, 1 Feb 2015 10:05:40 -0600 Subject: [PATCH 4/5] Do not add alpha channel to CMYK images --- skimage/io/_plugins/pil_plugin.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/skimage/io/_plugins/pil_plugin.py b/skimage/io/_plugins/pil_plugin.py index f07fc6f9..e2108145 100644 --- a/skimage/io/_plugins/pil_plugin.py +++ b/skimage/io/_plugins/pil_plugin.py @@ -90,9 +90,12 @@ def pil_to_ndarray(im, dtype=None, img_num=None): elif im.mode == '1': frame = im.convert('L') - elif 'A' in im.mode or im.mode == 'CMYK': + elif 'A' in im.mode: frame = im.convert('RGBA') + elif im.mode == 'CMYK': + frame = im.convert('RGB') + if im.mode.startswith('I;16'): shape = im.size dtype = '>u2' if im.mode.endswith('B') else ' Date: Sun, 1 Feb 2015 10:05:52 -0600 Subject: [PATCH 5/5] Compare the images in LAB space --- skimage/io/tests/test_pil.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/skimage/io/tests/test_pil.py b/skimage/io/tests/test_pil.py index b210ec6f..37395b73 100644 --- a/skimage/io/tests/test_pil.py +++ b/skimage/io/tests/test_pil.py @@ -18,6 +18,7 @@ from PIL import Image from .._plugins.pil_plugin import ( pil_to_ndarray, ndarray_to_pil, _palette_is_grayscale) from ...measure import structural_similarity as ssim +from ...color import rgb2lab def setup(): @@ -196,13 +197,16 @@ def test_cmyk(): img.save(fname) img.close() - new = imread(fname)[:, :, :3] + new = imread(fname) + + ref_lab = rgb2lab(ref) + new_lab = rgb2lab(new) for i in range(3): - newi = np.ascontiguousarray(new[:, :, i]) - refi = np.ascontiguousarray(ref[:, :, i]) + newi = np.ascontiguousarray(new_lab[:, :, i]) + refi = np.ascontiguousarray(ref_lab[:, :, i]) sim = ssim(refi, newi, dynamic_range=refi.max() - refi.min()) - assert sim > 0.85 + assert sim > 0.99 class TestSaveTIF: @@ -227,4 +231,4 @@ class TestSaveTIF: yield self.roundtrip, dtype, x if __name__ == "__main__": - run_module_suite() + run_module_suite() \ No newline at end of file