Merge pull request #1360 from blink1073/fix-cmyk-jpg

Add handling of CMYK images
This commit is contained in:
Juan Nunez-Iglesias
2015-02-02 11:19:44 +11:00
2 changed files with 36 additions and 8 deletions
+3 -1
View File
@@ -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
@@ -93,6 +93,8 @@ def pil_to_ndarray(im, dtype=None, img_num=None):
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
+33 -7
View File
@@ -6,18 +6,19 @@ 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
from ...color import rgb2lab
def setup():
@@ -183,6 +184,31 @@ 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)
ref_lab = rgb2lab(ref)
new_lab = rgb2lab(new)
for i in range(3):
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.99
class TestSaveTIF:
def roundtrip(self, dtype, x):
f = NamedTemporaryFile(suffix='.tif')
@@ -205,4 +231,4 @@ class TestSaveTIF:
yield self.roundtrip, dtype, x
if __name__ == "__main__":
run_module_suite()
run_module_suite()