def test_auxiliary_channels_isolated(self): # test data in aux channels does not affect non-aux channels aux_channel_formats = [ # format, profile, color-only format, source test image ('RGBA', 'sRGB', 'RGB', hopper('RGBA')), ('RGBX', 'sRGB', 'RGB', hopper('RGBX')), ('LAB', 'LAB', 'LAB', Image.open('Tests/images/hopper.Lab.tif')), ] for src_format in aux_channel_formats: for dst_format in aux_channel_formats: for transform_in_place in [True, False]: # inplace only if format doesn't change if transform_in_place and src_format[0] != dst_format[0]: continue # convert with and without AUX data, test colors are equal source_profile = ImageCms.createProfile(src_format[1]) destination_profile = ImageCms.createProfile(dst_format[1]) source_image = src_format[3] test_transform = ImageCms.buildTransform( source_profile, destination_profile, inMode=src_format[0], outMode=dst_format[0]) # test conversion from aux-ful source if transform_in_place: test_image = source_image.copy() ImageCms.applyTransform(test_image, test_transform, inPlace=True) else: test_image = ImageCms.applyTransform(source_image, test_transform, inPlace=False) # reference conversion from aux-less source reference_transform = ImageCms.buildTransform( source_profile, destination_profile, inMode=src_format[2], outMode=dst_format[2]) reference_image = ImageCms.applyTransform( source_image.convert(src_format[2]), reference_transform) self.assert_image_equal(test_image.convert(dst_format[2]), reference_image)
def test_lab_roundtrip(self): # check to see if we're at least internally consistent. psRGB = ImageCms.createProfile("sRGB") pLab = ImageCms.createProfile("LAB") t = ImageCms.buildTransform(psRGB, pLab, "RGB", "LAB") t2 = ImageCms.buildTransform(pLab, psRGB, "LAB", "RGB") i = ImageCms.applyTransform(hopper(), t) self.assertEqual(i.info['icc_profile'], ImageCmsProfile(pLab).tobytes()) out = ImageCms.applyTransform(i, t2) self.assert_image_similar(hopper(), out, 2)
def assert_aux_channel_preserved(self, mode, transform_in_place, preserved_channel): def create_test_image(): # set up test image with something interesting in the tested aux # channel. nine_grid_deltas = [ (-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 0), (0, 1), (1, -1), (1, 0), (1, 1), ] chans = [] bands = ImageMode.getmode(mode).bands for band_ndx in range(len(bands)): channel_type = 'L' # 8-bit unorm channel_pattern = hopper(channel_type) # paste pattern with varying offsets to avoid correlation # potentially hiding some bugs (like channels getting mixed). paste_offset = (int(band_ndx / float(len(bands)) * channel_pattern.size[0]), int(band_ndx / float(len(bands) * 2) * channel_pattern.size[1])) channel_data = Image.new(channel_type, channel_pattern.size) for delta in nine_grid_deltas: channel_data.paste( channel_pattern, tuple(paste_offset[c] + delta[c] * channel_pattern.size[c] for c in range(2))) chans.append(channel_data) return Image.merge(mode, chans) source_image = create_test_image() source_image_aux = source_image.getchannel(preserved_channel) # create some transform, it doesn't matter which one source_profile = ImageCms.createProfile("sRGB") destination_profile = ImageCms.createProfile("sRGB") t = ImageCms.buildTransform(source_profile, destination_profile, inMode=mode, outMode=mode) # apply transform if transform_in_place: ImageCms.applyTransform(source_image, t, inPlace=True) result_image = source_image else: result_image = ImageCms.applyTransform(source_image, t, inPlace=False) result_image_aux = result_image.getchannel(preserved_channel) self.assert_image_equal(source_image_aux, result_image_aux)
def test_sanity(self): # basic smoke test. # this mostly follows the cms_test outline. v = ImageCms.versions() # should return four strings self.assertEqual(v[0], '1.0.0 pil') self.assertEqual(list(map(type, v)), [str, str, str, str]) # internal version number self.assertRegex(ImageCms.core.littlecms_version, r"\d+\.\d+$") self.skip_missing() i = ImageCms.profileToProfile(hopper(), SRGB, SRGB) self.assert_image(i, "RGB", (128, 128)) i = hopper() ImageCms.profileToProfile(i, SRGB, SRGB, inPlace=True) self.assert_image(i, "RGB", (128, 128)) t = ImageCms.buildTransform(SRGB, SRGB, "RGB", "RGB") i = ImageCms.applyTransform(hopper(), t) self.assert_image(i, "RGB", (128, 128)) i = hopper() t = ImageCms.buildTransform(SRGB, SRGB, "RGB", "RGB") ImageCms.applyTransform(hopper(), t, inPlace=True) self.assert_image(i, "RGB", (128, 128)) p = ImageCms.createProfile("sRGB") o = ImageCms.getOpenProfile(SRGB) t = ImageCms.buildTransformFromOpenProfiles(p, o, "RGB", "RGB") i = ImageCms.applyTransform(hopper(), t) self.assert_image(i, "RGB", (128, 128)) t = ImageCms.buildProofTransform(SRGB, SRGB, SRGB, "RGB", "RGB") self.assertEqual(t.inputMode, "RGB") self.assertEqual(t.outputMode, "RGB") i = ImageCms.applyTransform(hopper(), t) self.assert_image(i, "RGB", (128, 128)) # test PointTransform convenience API hopper().point(t)
def test_lab_srgb(self): psRGB = ImageCms.createProfile("sRGB") pLab = ImageCms.createProfile("LAB") t = ImageCms.buildTransform(pLab, psRGB, "LAB", "RGB") img = Image.open('Tests/images/hopper.Lab.tif') img_srgb = ImageCms.applyTransform(img, t) # img_srgb.save('temp.srgb.tif') # visually verified vs ps. self.assert_image_similar(hopper(), img_srgb, 30) self.assertTrue(img_srgb.info['icc_profile']) profile = ImageCmsProfile(BytesIO(img_srgb.info['icc_profile'])) self.assertIn('sRGB', ImageCms.getProfileDescription(profile))
def test_lab_color(self): psRGB = ImageCms.createProfile("sRGB") pLab = ImageCms.createProfile("LAB") t = ImageCms.buildTransform(psRGB, pLab, "RGB", "LAB") # Need to add a type mapping for some PIL2 type to TYPE_Lab_8 in # findLCMSType, and have that mapping work back to a PIL2 mode # (likely RGB). i = ImageCms.applyTransform(hopper(), t) self.assert_image(i, "LAB", (128, 128)) # i.save('temp.lab.tif') # visually verified vs PS. target = Image.open('Tests/images/hopper.Lab.tif') self.assert_image_similar(i, target, 3.5)
def test_exceptions(self): # Test mode mismatch psRGB = ImageCms.createProfile("sRGB") pLab = ImageCms.createProfile("LAB") t = ImageCms.buildTransform(pLab, psRGB, "LAB", "RGB") self.assertRaises(ValueError, t.apply_in_place, hopper("RGBA")) # the procedural pyCMS API uses PyCMSError for all sorts of errors self.assertRaises(ImageCms.PyCMSError, ImageCms.profileToProfile, hopper(), "foo", "bar") self.assertRaises(ImageCms.PyCMSError, ImageCms.buildTransform, "foo", "bar", "RGB", "RGB") self.assertRaises(ImageCms.PyCMSError, ImageCms.getProfileName, None) self.skip_missing() self.assertRaises(ImageCms.PyCMSError, ImageCms.isIntentSupported, SRGB, None, None)
def test_simple_lab(self): i = Image.new('RGB', (10, 10), (128, 128, 128)) psRGB = ImageCms.createProfile("sRGB") pLab = ImageCms.createProfile("LAB") t = ImageCms.buildTransform(psRGB, pLab, "RGB", "LAB") i_lab = ImageCms.applyTransform(i, t) self.assertEqual(i_lab.mode, 'LAB') k = i_lab.getpixel((0, 0)) # not a linear luminance map. so L != 128: self.assertEqual(k, (137, 128, 128)) l_data = i_lab.getdata(0) a_data = i_lab.getdata(1) b_data = i_lab.getdata(2) self.assertEqual(list(l_data), [137] * 100) self.assertEqual(list(a_data), [128] * 100) self.assertEqual(list(b_data), [128] * 100)