def transform_frame(self, array: np.ndarray) -> np.ndarray: """Transforms a frame by applying the ICC profile. Parameters ---------- array: numpy.ndarray Pixel data of a color image frame in form of an array with dimensions (Rows x Columns x SamplesPerPixel) Returns ------- numpy.ndarray Color corrected pixel data of a image frame in form of an array with dimensions (Rows x Columns x SamplesPerPixel) Raises ------ ValueError When `array` does not have 3 dimensions and thus does not represent a color image frame. """ if array.ndim != 3: raise ValueError( 'Array has incorrect dimensions for a color image frame.' ) image = Image.fromarray(array) applyTransform(image, self._icc_transform, inPlace=True) return np.asarray(image)
def cmsConvertQImage(image, cmsTransformation=None): """ Apply a Cms transformation to a copy of a QImage and return the transformed image. If cmsTransformation is None, the input image is returned (no copy). @param image: image to transform @type image: QImage @param cmsTransformation : Cms transformation @type cmsTransformation: ImageCmsTransform @return: The converted QImage @rtype: QImage """ if cmsTransformation is None: return image image = image.copy() buf = QImageBuffer(image)[:, :, :3][:, :, ::-1] # convert to the PIL context and apply cmsTransformation bufC = np.ascontiguousarray(buf) PIL_img = Image.frombuffer( 'RGB', (image.width(), image.height()), bufC, 'raw', 'RGB', 0, 1) # these 3 weird parameters are recommended by a runtime warning !!! applyTransform(PIL_img, cmsTransformation, 1) # 1=in place # back to the image buffer buf[...] = np.frombuffer(PIL_img.tobytes(), dtype=np.uint8).reshape(buf.shape) return image
def pil_profiled_resize(path, size): im = Image.open(path) # im = profileToProfile(im, SRGB_PROFILE, LINEARIZED_PROFILE) im = applyTransform(im, srgb_to_linearized) im = ImageOps.fit(im, (size, size), Image.ANTIALIAS) # im = profileToProfile(im, LINEARIZED_PROFILE, SRGB_PROFILE) im = applyTransform(im, linearized_to_srgb) return im
def _standardize_icc(self): profile_original = self._get_original_profile() profile_original_name = getProfileName(profile_original).strip() self.log('profile_original_name: "{}"'.format(profile_original_name)) if ('sRGB' in profile_original_name or 'IEC 61966-2-1' in profile_original_name): target = 'sRGB2014' else: target = 'ProPhoto' profile_target = self._get_profile_from_file(target) profile_target_name = getProfileName(profile_target).strip() if profile_original == profile_target: self.master = self.original self.log( 'Original ICC profile was already the specified target ({}).' ''.format(profile_target_name)) else: try: tx = buildTransform(profile_original, profile_target, self.original.mode, 'RGB', renderingIntent=INTENT_PERCEPTUAL) except PyCMSError as e: if str(e) == 'cannot build transform': if self.original.mode == 'P': im = self.original.convert('RGB') tx = buildTransform(profile_original, profile_target, im.mode, 'RGB', renderingIntent=INTENT_PERCEPTUAL) else: raise else: im = self.original self.master = applyTransform(im, tx, inPlace=False) self.log('Original ICC profile ({}) was converted to the standard ' 'target ({}).' ''.format(profile_original_name, profile_target_name))
def convertQImage(image, transformation=None): """ Applies a Cms transformation to a QImage and returns the transformed image. If transformation is None, the input image is returned. Caution: The format is kept, but the alpha chanel is not preserved. @param image: image @type image: QImage @param transformation : Cms transformation @type transformation: CmsTransform @return: The converted QImage @rtype: QImage """ if transformation is not None: # convert to the PIL context and apply transformation converted_image = applyTransform(QImageToPilImage(image), transformation, 0) # time 0.85s for a 15 Mpx image. # convert back to QImage img = PilImageToQImage(converted_image) # restore format return img.convertToFormat(image.format()) else: return image
def rgb2cmyk(self, r, g, b): if self.profile is None: return rgb2cmyk(r, g, b) im = Image.new("RGB", (1, 1), (r * 255, g * 255, b * 255)) newim = applyTransform(im, self.profile) return newim.getpixel(0, 0)