def check_depth_more_8_or_gamma(png_image_name): r = png.Reader(str(png_image_name)) keys = [] gamma = False while True: # I used custom version that allows # invalid lenth of chunks in addition to invalid CRC key, value = r.chunk(lenient=True) if key == b'iCCP': try: f = io.BytesIO(value) prf = ImageCmsProfile(f) # ICC profile could be invalid keys.append(key) except OSError: pass else: keys.append(key) if key == b'IEND': break if b'gAMA' in keys and b'sRGB' not in keys and b'iCCP' not in keys: gamma = True r = png.Reader(str(png_image_name)) rr = r.read(lenient=True) return ( rr[3]["bitdepth"] > 8 and check_actually_16_bit(rr) ), gamma
def B_get_display_profile(handle=None): """ bLUe version for get_display_profile: should be completed. Note. The PIL function ImageCms.get_display_profile is system dependent, it fails (at least) for win64. @param handle: screen handle (Windows) @type handle: int @return: monitor profile @rtype: ImageCmsProfile """ if sys.platform == "win32": # from PIL import ImageWin # if isinstance(handle, ImageWin.HDC): profile = core.get_display_profile_win32(handle, 1) # else: # profile = core.get_display_profile_win32(handle or 0) else: profile = DEFAULT_MONITOR_PROFILE_PATH """ try: get = _imagingcms.get_display_profile except AttributeError: return None else: profile = get() """ return ImageCmsProfile(profile)
def test_lab_srgb(self): psRGB = ImageCms.createProfile("sRGB") pLab = ImageCms.createProfile("LAB") t = ImageCms.buildTransform(pLab, psRGB, "LAB", "RGB") with Image.open("Tests/images/hopper.Lab.tif") as img: 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_roundtrip(): # 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) assert i.info["icc_profile"] == ImageCmsProfile(pLab).tobytes() out = ImageCms.applyTransform(i, t2) assert_image_similar(hopper(), out, 2)
def test_lab_roundtrip(self): # check to see if we're at least internally consistent. pLab = ImageCms.createProfile("LAB") t = ImageCms.buildTransform(SRGB, pLab, "RGB", "LAB") t2 = ImageCms.buildTransform(pLab, SRGB, "LAB", "RGB") i = ImageCms.applyTransform(lena(), t) self.assertEqual(i.info['icc_profile'], ImageCmsProfile(pLab).tobytes()) out = ImageCms.applyTransform(i, t2) self.assert_image_similar(lena(), out, 2)
def test_lab_srgb(self): pLab = ImageCms.createProfile("LAB") t = ImageCms.buildTransform(pLab, SRGB, "LAB", "RGB") img = Image.open('Tests/images/lena.Lab.tif') img_srgb = ImageCms.applyTransform(img, t) # img_srgb.save('temp.srgb.tif') # visually verified vs ps. self.assert_image_similar(lena(), img_srgb, 30) self.assertTrue(img_srgb.info['icc_profile']) profile = ImageCmsProfile(BytesIO(img_srgb.info['icc_profile'])) self.assertTrue('sRGB' in ImageCms.getProfileDescription(profile))
def _build_icc_transform(icc_profile: bytes) -> ImageCmsTransform: """Builds an ICC Transformation object. Parameters ---------- icc_profile: bytes ICC Profile Returns ------- PIL.ImageCms.ImageCmsTransform ICC Transformation object """ profile: bytes try: profile = ImageCmsProfile(BytesIO(icc_profile)) except OSError: raise ValueError('Cannot read ICC Profile in image metadata.') name = getProfileName(profile).strip() description = getProfileDescription(profile).strip() logger.debug(f'found ICC Profile "{name}": "{description}"') logger.debug('build ICC Transform') intent = ImageCms.INTENT_RELATIVE_COLORIMETRIC if not isIntentSupported( profile, intent=intent, direction=ImageCms.DIRECTION_INPUT ): raise ValueError( 'ICC Profile does not support desired ' 'color transformation intent.' ) return ImageCms.buildTransform( inputProfile=profile, outputProfile=ImageCms.createProfile('sRGB'), inMode='RGB', # according to PS3.3 C.11.15.1.1 outMode='RGB' )
def setUp(self) -> None: super().setUp() self._icc_profile = ImageCmsProfile(createProfile('sRGB')).tobytes()
from Quartz.CoreGraphics import * from StringIO import StringIO from PIL.ImageCms import ImageCmsProfile colorSpace = CGDisplayCopyColorSpace(CGMainDisplayID()) print(colorSpace) iccdata = StringIO(CGColorSpaceCopyICCProfile(colorSpace)) # print(isinstance(iccdata, str)) profile = ImageCmsProfile(iccdata).profile # print dir(profile) # print(profile.model) # print(profile.profile_id) print(profile.profile_description) # print(profile.product_description) print(profile.media_white_point_temperature) print(profile.media_white_point) print(profile.chromatic_adaptation) # print(profile.__slots__) # for p in dir(profile): # print p, getattr(profile, p)