Pillow/src/PIL/ImageCms.py

1000 lines
36 KiB
Python
Raw Normal View History

2014-08-28 15:44:19 +04:00
# The Python Imaging Library.
# $Id$
2016-04-05 13:42:24 +03:00
# Optional color management support, based on Kevin Cazabon's PyCMS
2014-08-28 15:44:19 +04:00
# library.
2014-08-28 15:44:19 +04:00
# History:
2014-08-28 15:44:19 +04:00
# 2009-03-08 fl Added to PIL.
2014-08-28 15:44:19 +04:00
# Copyright (C) 2002-2003 Kevin Cazabon
# Copyright (c) 2009 by Fredrik Lundh
# Copyright (c) 2013 by Eric Soroos
2014-07-15 08:59:42 +04:00
2014-08-28 15:44:19 +04:00
# See the README file for information on usage and redistribution. See
# below for the original description.
2010-07-31 06:52:47 +04:00
import sys
2016-05-24 10:36:14 +03:00
from PIL import Image
2019-03-21 16:28:20 +03:00
2016-05-24 10:36:14 +03:00
try:
from PIL import _imagingcms
except ImportError as ex:
# Allow error import for doc purposes, but error out when accessing
# anything in core.
from ._util import deferred_error
2019-03-21 16:28:20 +03:00
2016-05-24 10:36:14 +03:00
_imagingcms = deferred_error(ex)
2010-07-31 06:52:47 +04:00
DESCRIPTION = """
pyCMS
a Python / PIL interface to the littleCMS ICC Color Management System
Copyright (C) 2002-2003 Kevin Cazabon
kevin@cazabon.com
http://www.cazabon.com
pyCMS home page: http://www.cazabon.com/pyCMS
littleCMS home page: http://www.littlecms.com
(littleCMS is Copyright (C) 1998-2001 Marti Maria)
Originally released under LGPL. Graciously donated to PIL in
March 2009, for distribution under the standard PIL license
The pyCMS.py module provides a "clean" interface between Python/PIL and
pyCMSdll, taking care of some of the more complex handling of the direct
pyCMSdll functions, as well as error-checking and making sure that all
relevant data is kept together.
While it is possible to call pyCMSdll functions directly, it's not highly
recommended.
Version History:
2013-10-02 10:22:45 +04:00
1.0.0 pil Oct 2013 Port to LCMS 2.
2010-07-31 06:52:47 +04:00
0.1.0 pil mod March 10, 2009
Renamed display profile to proof profile. The proof
profile is the profile of the device that is being
simulated, not the profile of the device which is
actually used to display/print the final simulation
(that'd be the output profile) - also see LCMSAPI.txt
input colorspace -> using 'renderingIntent' -> proof
colorspace -> using 'proofRenderingIntent' -> output
colorspace
Added LCMS FLAGS support.
Added FLAGS["SOFTPROOFING"] as default flag for
buildProofTransform (otherwise the proof profile/intent
would be ignored).
0.1.0 pil March 2009 - added to PIL, as PIL.ImageCms
0.0.2 alpha Jan 6, 2002
2015-05-29 07:59:54 +03:00
Added try/except statements around type() checks of
2010-07-31 06:52:47 +04:00
potential CObjects... Python won't let you use type()
2014-06-02 10:57:49 +04:00
on them, and raises a TypeError (stupid, if you ask
me!)
2010-07-31 06:52:47 +04:00
Added buildProofTransformFromOpenProfiles() function.
Additional fixes in DLL, see DLL code for details.
0.0.1 alpha first public release, Dec. 26, 2002
Known to-do list with current version (of Python interface, not pyCMSdll):
none
"""
2013-10-02 10:22:45 +04:00
VERSION = "1.0.0 pil"
2010-07-31 06:52:47 +04:00
# --------------------------------------------------------------------.
core = _imagingcms
#
# intent/direction values
INTENT_PERCEPTUAL = 0
INTENT_RELATIVE_COLORIMETRIC = 1
INTENT_SATURATION = 2
INTENT_ABSOLUTE_COLORIMETRIC = 3
DIRECTION_INPUT = 0
DIRECTION_OUTPUT = 1
DIRECTION_PROOF = 2
#
# flags
FLAGS = {
"MATRIXINPUT": 1,
"MATRIXOUTPUT": 2,
"MATRIXONLY": (1 | 2),
"NOWHITEONWHITEFIXUP": 4, # Don't hot fix scum dot
2014-06-02 10:57:49 +04:00
# Don't create prelinearization tables on precalculated transforms
# (internal use):
"NOPRELINEARIZATION": 16,
"GUESSDEVICECLASS": 32, # Guess device class (for transform2devicelink)
"NOTCACHE": 64, # Inhibit 1-pixel cache
2010-07-31 06:52:47 +04:00
"NOTPRECALC": 256,
"NULLTRANSFORM": 512, # Don't transform anyway
2015-05-29 07:59:54 +03:00
"HIGHRESPRECALC": 1024, # Use more memory to give better accuracy
"LOWRESPRECALC": 2048, # Use less memory to minimize resources
2010-07-31 06:52:47 +04:00
"WHITEBLACKCOMPENSATION": 8192,
"BLACKPOINTCOMPENSATION": 8192,
"GAMUTCHECK": 4096, # Out of Gamut alarm
"SOFTPROOFING": 16384, # Do softproofing
"PRESERVEBLACK": 32768, # Black preservation
"NODEFAULTRESOURCEDEF": 16777216, # CRD special
2019-03-21 16:28:20 +03:00
"GRIDPOINTS": lambda n: ((n) & 0xFF) << 16, # Gridpoints
2010-07-31 06:52:47 +04:00
}
_MAX_FLAG = 0
for flag in FLAGS.values():
if isinstance(flag, int):
2010-07-31 06:52:47 +04:00
_MAX_FLAG = _MAX_FLAG | flag
2010-07-31 06:52:47 +04:00
# --------------------------------------------------------------------.
# Experimental PIL-level API
# --------------------------------------------------------------------.
##
# Profile.
class ImageCmsProfile:
2010-07-31 06:52:47 +04:00
def __init__(self, profile):
2014-08-03 08:22:51 +04:00
"""
:param profile: Either a string representing a filename,
a file like object containing a profile or a
low-level profile object
2014-08-28 15:44:19 +04:00
2014-08-03 08:22:51 +04:00
"""
2014-08-28 15:44:19 +04:00
if isinstance(profile, str):
if sys.platform == "win32":
profile_bytes_path = profile.encode()
try:
profile_bytes_path.decode("ascii")
except UnicodeDecodeError:
with open(profile, "rb") as f:
self._set(core.profile_frombytes(f.read()))
return
2010-07-31 06:52:47 +04:00
self._set(core.profile_open(profile), profile)
elif hasattr(profile, "read"):
self._set(core.profile_frombytes(profile.read()))
elif isinstance(profile, _imagingcms.CmsProfile):
self._set(profile)
2010-07-31 06:52:47 +04:00
else:
raise TypeError("Invalid type for Profile")
2010-07-31 06:52:47 +04:00
def _set(self, profile, filename=None):
self.profile = profile
self.filename = filename
if profile:
self.product_name = None # profile.product_name
self.product_info = None # profile.product_info
2010-07-31 06:52:47 +04:00
else:
self.product_name = None
self.product_info = None
def tobytes(self):
2014-08-03 08:22:51 +04:00
"""
Returns the profile in a format suitable for embedding in
saved images.
:returns: a bytes object containing the ICC profile.
"""
2014-08-28 15:44:19 +04:00
return core.profile_tobytes(self.profile)
2014-08-28 15:44:19 +04:00
2010-07-31 06:52:47 +04:00
class ImageCmsTransform(Image.ImagePointHandler):
2014-06-02 10:57:49 +04:00
2016-05-24 10:36:14 +03:00
"""
Transform. This can be used with the procedural API, or with the standard
2020-06-11 15:42:13 +03:00
:py:func:`~PIL.Image.Image.point` method.
2016-05-24 10:36:14 +03:00
2020-06-11 15:42:13 +03:00
Will return the output profile in the ``output.info['icc_profile']``.
2016-05-24 10:36:14 +03:00
"""
2014-08-03 08:22:51 +04:00
2019-03-21 16:28:20 +03:00
def __init__(
self,
input,
output,
input_mode,
output_mode,
intent=INTENT_PERCEPTUAL,
proof=None,
proof_intent=INTENT_ABSOLUTE_COLORIMETRIC,
flags=0,
):
2010-07-31 06:52:47 +04:00
if proof is None:
self.transform = core.buildTransform(
2019-03-21 16:28:20 +03:00
input.profile, output.profile, input_mode, output_mode, intent, flags
)
2010-07-31 06:52:47 +04:00
else:
self.transform = core.buildProofTransform(
2019-03-21 16:28:20 +03:00
input.profile,
output.profile,
proof.profile,
input_mode,
output_mode,
intent,
proof_intent,
flags,
)
2010-07-31 06:52:47 +04:00
# Note: inputMode and outputMode are for pyCMS compatibility only
self.input_mode = self.inputMode = input_mode
self.output_mode = self.outputMode = output_mode
self.output_profile = output
2010-07-31 06:52:47 +04:00
def point(self, im):
return self.apply(im)
def apply(self, im, imOut=None):
im.load()
if imOut is None:
imOut = Image.new(self.output_mode, im.size, None)
self.transform.apply(im.im.id, imOut.im.id)
2019-03-21 16:28:20 +03:00
imOut.info["icc_profile"] = self.output_profile.tobytes()
2010-07-31 06:52:47 +04:00
return imOut
def apply_in_place(self, im):
im.load()
if im.mode != self.output_mode:
raise ValueError("mode mismatch") # wrong output mode
self.transform.apply(im.im.id, im.im.id)
2019-03-21 16:28:20 +03:00
im.info["icc_profile"] = self.output_profile.tobytes()
2010-07-31 06:52:47 +04:00
return im
2010-07-31 06:52:47 +04:00
def get_display_profile(handle=None):
2020-09-01 20:16:46 +03:00
"""
(experimental) Fetches the profile for the current display device.
2020-06-11 15:42:13 +03:00
:returns: ``None`` if the profile is not known.
2014-04-04 02:47:57 +04:00
"""
if sys.platform != "win32":
return None
2019-03-21 16:28:20 +03:00
from PIL import ImageWin
if isinstance(handle, ImageWin.HDC):
profile = core.get_display_profile_win32(handle, 1)
2010-07-31 06:52:47 +04:00
else:
profile = core.get_display_profile_win32(handle or 0)
if profile is None:
return None
2010-07-31 06:52:47 +04:00
return ImageCmsProfile(profile)
2010-07-31 06:52:47 +04:00
# --------------------------------------------------------------------.
# pyCMS compatible layer
# --------------------------------------------------------------------.
2019-03-21 16:28:20 +03:00
2010-07-31 06:52:47 +04:00
class PyCMSError(Exception):
2014-06-02 10:57:49 +04:00
2020-09-01 20:16:46 +03:00
"""(pyCMS) Exception class.
This is used for all errors in the pyCMS API."""
2019-03-21 16:28:20 +03:00
2010-07-31 06:52:47 +04:00
pass
2014-06-02 10:57:49 +04:00
def profileToProfile(
2019-03-21 16:28:20 +03:00
im,
inputProfile,
outputProfile,
renderingIntent=INTENT_PERCEPTUAL,
outputMode=None,
inPlace=False,
flags=0,
):
2014-04-04 02:47:57 +04:00
"""
(pyCMS) Applies an ICC transformation to a given image, mapping from
2020-06-11 15:42:13 +03:00
``inputProfile`` to ``outputProfile``.
2014-04-04 02:47:57 +04:00
If the input or output profiles specified are not valid filenames, a
:exc:`PyCMSError` will be raised. If ``inPlace`` is ``True`` and
``outputMode != im.mode``, a :exc:`PyCMSError` will be raised.
If an error occurs during application of the profiles,
a :exc:`PyCMSError` will be raised.
2020-06-11 15:42:13 +03:00
If ``outputMode`` is not a mode supported by the ``outputProfile`` (or by pyCMS),
a :exc:`PyCMSError` will be raised.
2020-06-11 15:42:13 +03:00
This function applies an ICC transformation to im from ``inputProfile``'s
color space to ``outputProfile``'s color space using the specified rendering
2014-04-04 02:47:57 +04:00
intent to decide how to handle out-of-gamut colors.
2020-06-11 15:42:13 +03:00
``outputMode`` can be used to specify that a color mode conversion is to
2014-04-04 02:47:57 +04:00
be done using these profiles, but the specified profiles must be able
to handle that mode. I.e., if converting im from RGB to CMYK using
profiles, the input profile must handle RGB data, and the output
profile must handle CMYK data.
2020-06-11 15:42:13 +03:00
:param im: An open :py:class:`~PIL.Image.Image` object (i.e. Image.new(...)
or Image.open(...), etc.)
2014-06-02 10:57:49 +04:00
:param inputProfile: String, as a valid filename path to the ICC input
profile you wish to use for this image, or a profile object
2014-04-04 02:47:57 +04:00
:param outputProfile: String, as a valid filename path to the ICC output
profile you wish to use for this image, or a profile object
2014-06-02 10:57:49 +04:00
:param renderingIntent: Integer (0-3) specifying the rendering intent you
wish to use for the transform
2014-04-04 02:47:57 +04:00
2018-06-24 15:32:25 +03:00
ImageCms.INTENT_PERCEPTUAL = 0 (DEFAULT)
ImageCms.INTENT_RELATIVE_COLORIMETRIC = 1
ImageCms.INTENT_SATURATION = 2
ImageCms.INTENT_ABSOLUTE_COLORIMETRIC = 3
2014-04-04 02:47:57 +04:00
2014-06-02 10:57:49 +04:00
see the pyCMS documentation for details on rendering intents and what
they do.
:param outputMode: A valid PIL mode for the output image (i.e. "RGB",
"CMYK", etc.). Note: if rendering the image "inPlace", outputMode
MUST be the same mode as the input, or omitted completely. If
omitted, the outputMode will be the same as the mode of the input
image (im.mode)
2020-06-11 15:42:13 +03:00
:param inPlace: Boolean. If ``True``, the original image is modified in-place,
and ``None`` is returned. If ``False`` (default), a new
:py:class:`~PIL.Image.Image` object is returned with the transform applied.
2014-04-04 02:47:57 +04:00
:param flags: Integer (0-...) specifying additional flags
2020-06-11 15:42:13 +03:00
:returns: Either None or a new :py:class:`~PIL.Image.Image` object, depending on
the value of ``inPlace``
2014-04-04 02:47:57 +04:00
:exception PyCMSError:
"""
2010-07-31 06:52:47 +04:00
if outputMode is None:
outputMode = im.mode
if not isinstance(renderingIntent, int) or not (0 <= renderingIntent <= 3):
2010-07-31 06:52:47 +04:00
raise PyCMSError("renderingIntent must be an integer between 0 and 3")
if not isinstance(flags, int) or not (0 <= flags <= _MAX_FLAG):
2019-03-21 16:28:20 +03:00
raise PyCMSError("flags must be an integer between 0 and %s" + _MAX_FLAG)
2010-07-31 06:52:47 +04:00
try:
if not isinstance(inputProfile, ImageCmsProfile):
inputProfile = ImageCmsProfile(inputProfile)
if not isinstance(outputProfile, ImageCmsProfile):
outputProfile = ImageCmsProfile(outputProfile)
transform = ImageCmsTransform(
2019-03-21 16:28:20 +03:00
inputProfile,
outputProfile,
im.mode,
outputMode,
renderingIntent,
flags=flags,
)
2010-07-31 06:52:47 +04:00
if inPlace:
transform.apply_in_place(im)
imOut = None
else:
imOut = transform.apply(im)
except (OSError, TypeError, ValueError) as v:
raise PyCMSError(v) from v
2010-07-31 06:52:47 +04:00
return imOut
def getOpenProfile(profileFilename):
2014-04-04 02:47:57 +04:00
"""
(pyCMS) Opens an ICC profile file.
The PyCMSProfile object can be passed back into pyCMS for use in creating
transforms and such (as in ImageCms.buildTransformFromOpenProfiles()).
If ``profileFilename`` is not a valid filename for an ICC profile,
a :exc:`PyCMSError` will be raised.
2014-04-04 02:47:57 +04:00
2014-06-02 10:57:49 +04:00
:param profileFilename: String, as a valid filename path to the ICC profile
you wish to open, or a file-like object.
2014-04-04 02:47:57 +04:00
:returns: A CmsProfile class object.
:exception PyCMSError:
"""
2010-07-31 06:52:47 +04:00
try:
return ImageCmsProfile(profileFilename)
except (OSError, TypeError, ValueError) as v:
raise PyCMSError(v) from v
2010-07-31 06:52:47 +04:00
2014-06-02 10:57:49 +04:00
def buildTransform(
2019-03-21 16:28:20 +03:00
inputProfile,
outputProfile,
inMode,
outMode,
renderingIntent=INTENT_PERCEPTUAL,
flags=0,
):
2014-04-04 02:47:57 +04:00
"""
2020-06-11 15:42:13 +03:00
(pyCMS) Builds an ICC transform mapping from the ``inputProfile`` to the
``outputProfile``. Use applyTransform to apply the transform to a given
2014-04-04 02:47:57 +04:00
image.
If the input or output profiles specified are not valid filenames, a
:exc:`PyCMSError` will be raised. If an error occurs during creation
of the transform, a :exc:`PyCMSError` will be raised.
2014-04-04 02:47:57 +04:00
2020-06-11 15:42:13 +03:00
If ``inMode`` or ``outMode`` are not a mode supported by the ``outputProfile``
(or by pyCMS), a :exc:`PyCMSError` will be raised.
2014-04-04 02:47:57 +04:00
2020-06-11 15:42:13 +03:00
This function builds and returns an ICC transform from the ``inputProfile``
to the ``outputProfile`` using the ``renderingIntent`` to determine what to do
2014-04-04 02:47:57 +04:00
with out-of-gamut colors. It will ONLY work for converting images that
2020-06-11 15:42:13 +03:00
are in ``inMode`` to images that are in ``outMode`` color format (PIL mode,
2014-04-04 02:47:57 +04:00
i.e. "RGB", "RGBA", "CMYK", etc.).
Building the transform is a fair part of the overhead in
ImageCms.profileToProfile(), so if you're planning on converting multiple
images using the same input/output settings, this can save you time.
Once you have a transform object, it can be used with
ImageCms.applyProfile() to convert images without the need to re-compute
the lookup table for the transform.
The reason pyCMS returns a class object rather than a handle directly
to the transform is that it needs to keep track of the PIL input/output
modes that the transform is meant for. These attributes are stored in
2020-06-11 15:42:13 +03:00
the ``inMode`` and ``outMode`` attributes of the object (which can be
2014-04-04 02:47:57 +04:00
manually overridden if you really want to, but I don't know of any
time that would be of use, or would even work).
2014-06-02 10:57:49 +04:00
:param inputProfile: String, as a valid filename path to the ICC input
profile you wish to use for this transform, or a profile object
2014-04-04 02:47:57 +04:00
:param outputProfile: String, as a valid filename path to the ICC output
profile you wish to use for this transform, or a profile object
2014-06-02 10:57:49 +04:00
:param inMode: String, as a valid PIL mode that the appropriate profile
also supports (i.e. "RGB", "RGBA", "CMYK", etc.)
:param outMode: String, as a valid PIL mode that the appropriate profile
also supports (i.e. "RGB", "RGBA", "CMYK", etc.)
2014-04-04 02:47:57 +04:00
:param renderingIntent: Integer (0-3) specifying the rendering intent you
wish to use for the transform
2018-06-24 15:32:25 +03:00
ImageCms.INTENT_PERCEPTUAL = 0 (DEFAULT)
ImageCms.INTENT_RELATIVE_COLORIMETRIC = 1
ImageCms.INTENT_SATURATION = 2
ImageCms.INTENT_ABSOLUTE_COLORIMETRIC = 3
2014-04-04 02:47:57 +04:00
2014-06-02 10:57:49 +04:00
see the pyCMS documentation for details on rendering intents and what
they do.
2014-04-04 02:47:57 +04:00
:param flags: Integer (0-...) specifying additional flags
:returns: A CmsTransform class object.
:exception PyCMSError:
"""
if not isinstance(renderingIntent, int) or not (0 <= renderingIntent <= 3):
2010-07-31 06:52:47 +04:00
raise PyCMSError("renderingIntent must be an integer between 0 and 3")
if not isinstance(flags, int) or not (0 <= flags <= _MAX_FLAG):
2019-03-21 16:28:20 +03:00
raise PyCMSError("flags must be an integer between 0 and %s" + _MAX_FLAG)
2010-07-31 06:52:47 +04:00
try:
if not isinstance(inputProfile, ImageCmsProfile):
inputProfile = ImageCmsProfile(inputProfile)
if not isinstance(outputProfile, ImageCmsProfile):
outputProfile = ImageCmsProfile(outputProfile)
2014-06-02 10:57:49 +04:00
return ImageCmsTransform(
2019-03-21 16:28:20 +03:00
inputProfile, outputProfile, inMode, outMode, renderingIntent, flags=flags
)
except (OSError, TypeError, ValueError) as v:
raise PyCMSError(v) from v
2010-07-31 06:52:47 +04:00
2014-06-02 10:57:49 +04:00
def buildProofTransform(
2019-03-21 16:28:20 +03:00
inputProfile,
outputProfile,
proofProfile,
inMode,
outMode,
renderingIntent=INTENT_PERCEPTUAL,
proofRenderingIntent=INTENT_ABSOLUTE_COLORIMETRIC,
flags=FLAGS["SOFTPROOFING"],
):
2014-04-04 02:47:57 +04:00
"""
2020-06-11 15:42:13 +03:00
(pyCMS) Builds an ICC transform mapping from the ``inputProfile`` to the
``outputProfile``, but tries to simulate the result that would be
obtained on the ``proofProfile`` device.
2014-04-04 02:47:57 +04:00
If the input, output, or proof profiles specified are not valid
filenames, a :exc:`PyCMSError` will be raised.
2014-04-04 02:47:57 +04:00
If an error occurs during creation of the transform,
a :exc:`PyCMSError` will be raised.
2014-04-04 02:47:57 +04:00
2020-06-11 15:42:13 +03:00
If ``inMode`` or ``outMode`` are not a mode supported by the ``outputProfile``
(or by pyCMS), a :exc:`PyCMSError` will be raised.
2014-04-04 02:47:57 +04:00
2020-06-11 15:42:13 +03:00
This function builds and returns an ICC transform from the ``inputProfile``
to the ``outputProfile``, but tries to simulate the result that would be
obtained on the ``proofProfile`` device using ``renderingIntent`` and
``proofRenderingIntent`` to determine what to do with out-of-gamut
2014-04-04 02:47:57 +04:00
colors. This is known as "soft-proofing". It will ONLY work for
2020-06-11 15:42:13 +03:00
converting images that are in ``inMode`` to images that are in outMode
2014-04-04 02:47:57 +04:00
color format (PIL mode, i.e. "RGB", "RGBA", "CMYK", etc.).
Usage of the resulting transform object is exactly the same as with
ImageCms.buildTransform().
Proof profiling is generally used when using an output device to get a
good idea of what the final printed/displayed image would look like on
2020-06-11 15:42:13 +03:00
the ``proofProfile`` device when it's quicker and easier to use the
2014-04-04 02:47:57 +04:00
output device for judging color. Generally, this means that the
output device is a monitor, or a dye-sub printer (etc.), and the simulated
device is something more expensive, complicated, or time consuming
(making it difficult to make a real print for color judgement purposes).
Soft-proofing basically functions by adjusting the colors on the
output device to match the colors of the device being simulated. However,
when the simulated device has a much wider gamut than the output
device, you may obtain marginal results.
2014-06-02 10:57:49 +04:00
:param inputProfile: String, as a valid filename path to the ICC input
profile you wish to use for this transform, or a profile object
2014-04-04 02:47:57 +04:00
:param outputProfile: String, as a valid filename path to the ICC output
(monitor, usually) profile you wish to use for this transform, or a
profile object
2014-06-02 10:57:49 +04:00
:param proofProfile: String, as a valid filename path to the ICC proof
profile you wish to use for this transform, or a profile object
:param inMode: String, as a valid PIL mode that the appropriate profile
also supports (i.e. "RGB", "RGBA", "CMYK", etc.)
:param outMode: String, as a valid PIL mode that the appropriate profile
also supports (i.e. "RGB", "RGBA", "CMYK", etc.)
2014-04-04 02:47:57 +04:00
:param renderingIntent: Integer (0-3) specifying the rendering intent you
wish to use for the input->proof (simulated) transform
2018-06-24 15:32:25 +03:00
ImageCms.INTENT_PERCEPTUAL = 0 (DEFAULT)
ImageCms.INTENT_RELATIVE_COLORIMETRIC = 1
ImageCms.INTENT_SATURATION = 2
ImageCms.INTENT_ABSOLUTE_COLORIMETRIC = 3
2014-04-04 02:47:57 +04:00
2014-06-02 10:57:49 +04:00
see the pyCMS documentation for details on rendering intents and what
they do.
2018-06-24 15:32:25 +03:00
:param proofRenderingIntent: Integer (0-3) specifying the rendering intent
you wish to use for proof->output transform
2014-04-04 02:47:57 +04:00
2018-06-24 15:32:25 +03:00
ImageCms.INTENT_PERCEPTUAL = 0 (DEFAULT)
ImageCms.INTENT_RELATIVE_COLORIMETRIC = 1
ImageCms.INTENT_SATURATION = 2
ImageCms.INTENT_ABSOLUTE_COLORIMETRIC = 3
2014-04-04 02:47:57 +04:00
2014-06-02 10:57:49 +04:00
see the pyCMS documentation for details on rendering intents and what
they do.
2014-04-04 02:47:57 +04:00
:param flags: Integer (0-...) specifying additional flags
:returns: A CmsTransform class object.
:exception PyCMSError:
"""
if not isinstance(renderingIntent, int) or not (0 <= renderingIntent <= 3):
2010-07-31 06:52:47 +04:00
raise PyCMSError("renderingIntent must be an integer between 0 and 3")
if not isinstance(flags, int) or not (0 <= flags <= _MAX_FLAG):
2019-03-21 16:28:20 +03:00
raise PyCMSError("flags must be an integer between 0 and %s" + _MAX_FLAG)
2010-07-31 06:52:47 +04:00
try:
if not isinstance(inputProfile, ImageCmsProfile):
inputProfile = ImageCmsProfile(inputProfile)
if not isinstance(outputProfile, ImageCmsProfile):
outputProfile = ImageCmsProfile(outputProfile)
if not isinstance(proofProfile, ImageCmsProfile):
proofProfile = ImageCmsProfile(proofProfile)
2014-06-02 10:57:49 +04:00
return ImageCmsTransform(
2019-03-21 16:28:20 +03:00
inputProfile,
outputProfile,
inMode,
outMode,
renderingIntent,
proofProfile,
proofRenderingIntent,
flags,
)
except (OSError, TypeError, ValueError) as v:
raise PyCMSError(v) from v
2010-07-31 06:52:47 +04:00
2018-03-03 12:54:00 +03:00
2010-07-31 06:52:47 +04:00
buildTransformFromOpenProfiles = buildTransform
buildProofTransformFromOpenProfiles = buildProofTransform
def applyTransform(im, transform, inPlace=False):
2014-04-04 02:47:57 +04:00
"""
(pyCMS) Applies a transform to a given image.
If ``im.mode != transform.inMode``, a :exc:`PyCMSError` is raised.
2014-04-04 02:47:57 +04:00
2020-06-11 15:42:13 +03:00
If ``inPlace`` is ``True`` and ``transform.inMode != transform.outMode``, a
:exc:`PyCMSError` is raised.
2014-04-04 02:47:57 +04:00
2020-06-11 15:42:13 +03:00
If ``im.mode``, ``transform.inMode`` or ``transform.outMode`` is not
supported by pyCMSdll or the profiles you used for the transform, a
:exc:`PyCMSError` is raised.
2014-04-04 02:47:57 +04:00
If an error occurs while the transform is being applied,
a :exc:`PyCMSError` is raised.
2014-04-04 02:47:57 +04:00
This function applies a pre-calculated transform (from
2014-06-02 10:57:49 +04:00
ImageCms.buildTransform() or ImageCms.buildTransformFromOpenProfiles())
2020-06-11 15:42:13 +03:00
to an image. The transform can be used for multiple images, saving
2015-05-29 07:59:54 +03:00
considerable calculation time if doing the same conversion multiple times.
2014-04-04 02:47:57 +04:00
If you want to modify im in-place instead of receiving a new image as
2020-06-11 15:42:13 +03:00
the return value, set ``inPlace`` to ``True``. This can only be done if
``transform.inMode`` and ``transform.outMode`` are the same, because we can't
2014-04-04 02:47:57 +04:00
change the mode in-place (the buffer sizes for some modes are
2020-06-11 15:42:13 +03:00
different). The default behavior is to return a new :py:class:`~PIL.Image.Image`
object of the same dimensions in mode ``transform.outMode``.
2014-04-04 02:47:57 +04:00
2020-06-11 15:42:13 +03:00
:param im: An :py:class:`~PIL.Image.Image` object, and im.mode must be the same
as the ``inMode`` supported by the transform.
2014-04-04 02:47:57 +04:00
:param transform: A valid CmsTransform class object
2020-09-01 20:16:46 +03:00
:param inPlace: Bool. If ``True``, ``im`` is modified in place and ``None`` is
2020-06-11 15:42:13 +03:00
returned, if ``False``, a new :py:class:`~PIL.Image.Image` object with the
transform applied is returned (and ``im`` is not changed). The default is
``False``.
:returns: Either ``None``, or a new :py:class:`~PIL.Image.Image` object,
depending on the value of ``inPlace``. The profile will be returned in
the image's ``info['icc_profile']``.
2014-04-04 02:47:57 +04:00
:exception PyCMSError:
"""
2010-07-31 06:52:47 +04:00
try:
if inPlace:
transform.apply_in_place(im)
imOut = None
else:
imOut = transform.apply(im)
except (TypeError, ValueError) as v:
raise PyCMSError(v) from v
2010-07-31 06:52:47 +04:00
return imOut
2010-07-31 06:52:47 +04:00
def createProfile(colorSpace, colorTemp=-1):
2014-04-04 02:47:57 +04:00
"""
(pyCMS) Creates a profile.
If colorSpace not in ``["LAB", "XYZ", "sRGB"]``,
a :exc:`PyCMSError` is raised.
2014-04-04 02:47:57 +04:00
If using LAB and ``colorTemp`` is not a positive integer,
a :exc:`PyCMSError` is raised.
2014-04-04 02:47:57 +04:00
If an error occurs while creating the profile,
a :exc:`PyCMSError` is raised.
2014-04-04 02:47:57 +04:00
Use this function to create common profiles on-the-fly instead of
having to supply a profile on disk and knowing the path to it. It
returns a normal CmsProfile object that can be passed to
ImageCms.buildTransformFromOpenProfiles() to create a transform to apply
to images.
2014-06-02 10:57:49 +04:00
:param colorSpace: String, the color space of the profile you wish to
create.
2014-04-04 02:47:57 +04:00
Currently only "LAB", "XYZ", and "sRGB" are supported.
:param colorTemp: Positive integer for the white point for the profile, in
degrees Kelvin (i.e. 5000, 6500, 9600, etc.). The default is for D50
2014-06-02 10:57:49 +04:00
illuminant if omitted (5000k). colorTemp is ONLY applied to LAB
profiles, and is ignored for XYZ and sRGB.
2014-04-04 02:47:57 +04:00
:returns: A CmsProfile class object
:exception PyCMSError:
"""
2010-07-31 06:52:47 +04:00
if colorSpace not in ["LAB", "XYZ", "sRGB"]:
2014-06-02 10:57:49 +04:00
raise PyCMSError(
f"Color space not supported for on-the-fly profile creation ({colorSpace})"
2019-03-21 16:28:20 +03:00
)
2010-07-31 06:52:47 +04:00
if colorSpace == "LAB":
try:
colorTemp = float(colorTemp)
except (TypeError, ValueError) as e:
2014-06-02 10:57:49 +04:00
raise PyCMSError(
f'Color temperature must be numeric, "{colorTemp}" not valid'
) from e
2010-07-31 06:52:47 +04:00
try:
return core.createProfile(colorSpace, colorTemp)
except (TypeError, ValueError) as v:
raise PyCMSError(v) from v
2010-07-31 06:52:47 +04:00
2010-07-31 06:52:47 +04:00
def getProfileName(profile):
2014-04-04 02:47:57 +04:00
"""
(pyCMS) Gets the internal product name for the given profile.
2020-06-11 15:42:13 +03:00
If ``profile`` isn't a valid CmsProfile object or filename to a profile,
a :exc:`PyCMSError` is raised If an error occurs while trying
to obtain the name tag, a :exc:`PyCMSError` is raised.
2014-04-04 02:47:57 +04:00
Use this function to obtain the INTERNAL name of the profile (stored
in an ICC tag in the profile itself), usually the one used when the
profile was originally created. Sometimes this tag also contains
additional information supplied by the creator.
2014-06-02 10:57:49 +04:00
:param profile: EITHER a valid CmsProfile object, OR a string of the
filename of an ICC profile.
:returns: A string containing the internal name of the profile as stored
in an ICC tag.
2014-04-04 02:47:57 +04:00
:exception PyCMSError:
"""
2010-07-31 06:52:47 +04:00
try:
# add an extra newline to preserve pyCMS compatibility
if not isinstance(profile, ImageCmsProfile):
profile = ImageCmsProfile(profile)
# do it in python, not c.
# // name was "%s - %s" (model, manufacturer) || Description ,
# // but if the Model and Manufacturer were the same or the model
# // was long, Just the model, in 1.x
model = profile.profile.model
manufacturer = profile.profile.manufacturer
if not (model or manufacturer):
return (profile.profile.profile_description or "") + "\n"
if not manufacturer or len(model) > 30:
return model + "\n"
return f"{model} - {manufacturer}\n"
except (AttributeError, OSError, TypeError, ValueError) as v:
raise PyCMSError(v) from v
2010-07-31 06:52:47 +04:00
2010-07-31 06:52:47 +04:00
def getProfileInfo(profile):
2014-04-04 02:47:57 +04:00
"""
(pyCMS) Gets the internal product information for the given profile.
2020-06-11 15:42:13 +03:00
If ``profile`` isn't a valid CmsProfile object or filename to a profile,
a :exc:`PyCMSError` is raised.
2014-04-04 02:47:57 +04:00
If an error occurs while trying to obtain the info tag,
a :exc:`PyCMSError` is raised.
2014-04-04 02:47:57 +04:00
Use this function to obtain the information stored in the profile's
info tag. This often contains details about the profile, and how it
was created, as supplied by the creator.
2014-06-02 10:57:49 +04:00
:param profile: EITHER a valid CmsProfile object, OR a string of the
filename of an ICC profile.
:returns: A string containing the internal profile information stored in
an ICC tag.
2014-04-04 02:47:57 +04:00
:exception PyCMSError:
"""
2010-07-31 06:52:47 +04:00
try:
if not isinstance(profile, ImageCmsProfile):
profile = ImageCmsProfile(profile)
# add an extra newline to preserve pyCMS compatibility
2014-06-02 10:57:49 +04:00
# Python, not C. the white point bits weren't working well,
# so skipping.
2018-10-24 22:24:03 +03:00
# info was description \r\n\r\n copyright \r\n\r\n K007 tag \r\n\r\n whitepoint
description = profile.profile.profile_description
cpright = profile.profile.copyright
arr = []
for elt in (description, cpright):
if elt:
arr.append(elt)
return "\r\n\r\n".join(arr) + "\r\n\r\n"
except (AttributeError, OSError, TypeError, ValueError) as v:
raise PyCMSError(v) from v
2010-07-31 06:52:47 +04:00
def getProfileCopyright(profile):
2014-04-04 02:47:57 +04:00
"""
(pyCMS) Gets the copyright for the given profile.
2020-06-11 15:42:13 +03:00
If ``profile`` isn't a valid CmsProfile object or filename to a profile, a
:exc:`PyCMSError` is raised.
2014-04-04 02:47:57 +04:00
If an error occurs while trying to obtain the copyright tag,
a :exc:`PyCMSError` is raised.
2014-04-04 02:47:57 +04:00
Use this function to obtain the information stored in the profile's
copyright tag.
2014-04-04 02:47:57 +04:00
2014-06-02 10:57:49 +04:00
:param profile: EITHER a valid CmsProfile object, OR a string of the
filename of an ICC profile.
:returns: A string containing the internal profile information stored in
an ICC tag.
2014-04-04 02:47:57 +04:00
:exception PyCMSError:
"""
try:
# add an extra newline to preserve pyCMS compatibility
if not isinstance(profile, ImageCmsProfile):
profile = ImageCmsProfile(profile)
return (profile.profile.copyright or "") + "\n"
except (AttributeError, OSError, TypeError, ValueError) as v:
raise PyCMSError(v) from v
def getProfileManufacturer(profile):
2014-04-04 02:47:57 +04:00
"""
(pyCMS) Gets the manufacturer for the given profile.
2020-06-11 15:42:13 +03:00
If ``profile`` isn't a valid CmsProfile object or filename to a profile, a
:exc:`PyCMSError` is raised.
2014-04-04 02:47:57 +04:00
2014-06-02 10:57:49 +04:00
If an error occurs while trying to obtain the manufacturer tag, a
:exc:`PyCMSError` is raised.
2014-04-04 02:47:57 +04:00
Use this function to obtain the information stored in the profile's
manufacturer tag.
2014-04-04 02:47:57 +04:00
2014-06-02 10:57:49 +04:00
:param profile: EITHER a valid CmsProfile object, OR a string of the
filename of an ICC profile.
:returns: A string containing the internal profile information stored in
an ICC tag.
2014-04-04 02:47:57 +04:00
:exception PyCMSError:
"""
try:
# add an extra newline to preserve pyCMS compatibility
if not isinstance(profile, ImageCmsProfile):
profile = ImageCmsProfile(profile)
return (profile.profile.manufacturer or "") + "\n"
except (AttributeError, OSError, TypeError, ValueError) as v:
raise PyCMSError(v) from v
def getProfileModel(profile):
2014-04-04 02:47:57 +04:00
"""
(pyCMS) Gets the model for the given profile.
2020-06-11 15:42:13 +03:00
If ``profile`` isn't a valid CmsProfile object or filename to a profile, a
:exc:`PyCMSError` is raised.
If an error occurs while trying to obtain the model tag,
a :exc:`PyCMSError` is raised.
2014-04-04 02:47:57 +04:00
Use this function to obtain the information stored in the profile's
model tag.
2014-06-02 10:57:49 +04:00
:param profile: EITHER a valid CmsProfile object, OR a string of the
filename of an ICC profile.
:returns: A string containing the internal profile information stored in
an ICC tag.
2014-04-04 02:47:57 +04:00
:exception PyCMSError:
"""
try:
# add an extra newline to preserve pyCMS compatibility
if not isinstance(profile, ImageCmsProfile):
profile = ImageCmsProfile(profile)
return (profile.profile.model or "") + "\n"
except (AttributeError, OSError, TypeError, ValueError) as v:
raise PyCMSError(v) from v
def getProfileDescription(profile):
2014-04-04 02:47:57 +04:00
"""
(pyCMS) Gets the description for the given profile.
2020-06-11 15:42:13 +03:00
If ``profile`` isn't a valid CmsProfile object or filename to a profile, a
:exc:`PyCMSError` is raised.
2014-04-04 02:47:57 +04:00
If an error occurs while trying to obtain the description tag,
a :exc:`PyCMSError` is raised.
2014-04-04 02:47:57 +04:00
Use this function to obtain the information stored in the profile's
description tag.
2014-04-04 02:47:57 +04:00
2014-06-02 10:57:49 +04:00
:param profile: EITHER a valid CmsProfile object, OR a string of the
filename of an ICC profile.
:returns: A string containing the internal profile information stored in an
ICC tag.
2014-04-04 02:47:57 +04:00
:exception PyCMSError:
"""
try:
# add an extra newline to preserve pyCMS compatibility
if not isinstance(profile, ImageCmsProfile):
profile = ImageCmsProfile(profile)
return (profile.profile.profile_description or "") + "\n"
except (AttributeError, OSError, TypeError, ValueError) as v:
raise PyCMSError(v) from v
2014-04-04 02:47:57 +04:00
def getDefaultIntent(profile):
"""
(pyCMS) Gets the default intent name for the given profile.
2020-06-11 15:42:13 +03:00
If ``profile`` isn't a valid CmsProfile object or filename to a profile, a
:exc:`PyCMSError` is raised.
2014-04-04 02:47:57 +04:00
If an error occurs while trying to obtain the default intent, a
:exc:`PyCMSError` is raised.
2014-04-04 02:47:57 +04:00
2015-05-29 07:59:54 +03:00
Use this function to determine the default (and usually best optimized)
2014-04-04 02:47:57 +04:00
rendering intent for this profile. Most profiles support multiple
rendering intents, but are intended mostly for one type of conversion.
If you wish to use a different intent than returned, use
ImageCms.isIntentSupported() to verify it will work first.
2014-06-02 10:57:49 +04:00
:param profile: EITHER a valid CmsProfile object, OR a string of the
filename of an ICC profile.
:returns: Integer 0-3 specifying the default rendering intent for this
profile.
2014-04-04 02:47:57 +04:00
2018-06-24 15:32:25 +03:00
ImageCms.INTENT_PERCEPTUAL = 0 (DEFAULT)
ImageCms.INTENT_RELATIVE_COLORIMETRIC = 1
ImageCms.INTENT_SATURATION = 2
ImageCms.INTENT_ABSOLUTE_COLORIMETRIC = 3
2014-04-04 02:47:57 +04:00
2014-06-02 10:57:49 +04:00
see the pyCMS documentation for details on rendering intents and what
they do.
2014-04-04 02:47:57 +04:00
:exception PyCMSError:
"""
2010-07-31 06:52:47 +04:00
try:
if not isinstance(profile, ImageCmsProfile):
profile = ImageCmsProfile(profile)
return profile.profile.rendering_intent
except (AttributeError, OSError, TypeError, ValueError) as v:
raise PyCMSError(v) from v
2010-07-31 06:52:47 +04:00
2010-07-31 06:52:47 +04:00
def isIntentSupported(profile, intent, direction):
2014-04-04 02:47:57 +04:00
"""
(pyCMS) Checks if a given intent is supported.
Use this function to verify that you can use your desired
2020-06-11 15:42:13 +03:00
``intent`` with ``profile``, and that ``profile`` can be used for the
2014-04-04 02:47:57 +04:00
input/output/proof profile as you desire.
Some profiles are created specifically for one "direction", can cannot
2020-06-11 15:42:13 +03:00
be used for others. Some profiles can only be used for certain
rendering intents, so it's best to either verify this before trying
2014-04-04 02:47:57 +04:00
to create a transform with them (using this function), or catch the
potential :exc:`PyCMSError` that will occur if they don't
support the modes you select.
2014-04-04 02:47:57 +04:00
2014-06-02 10:57:49 +04:00
:param profile: EITHER a valid CmsProfile object, OR a string of the
filename of an ICC profile.
:param intent: Integer (0-3) specifying the rendering intent you wish to
use with this profile
2014-04-04 02:47:57 +04:00
2018-06-24 15:32:25 +03:00
ImageCms.INTENT_PERCEPTUAL = 0 (DEFAULT)
ImageCms.INTENT_RELATIVE_COLORIMETRIC = 1
ImageCms.INTENT_SATURATION = 2
ImageCms.INTENT_ABSOLUTE_COLORIMETRIC = 3
2014-04-04 02:47:57 +04:00
2014-06-02 10:57:49 +04:00
see the pyCMS documentation for details on rendering intents and what
they do.
2018-06-24 15:32:25 +03:00
:param direction: Integer specifying if the profile is to be used for
input, output, or proof
2014-04-04 02:47:57 +04:00
INPUT = 0 (or use ImageCms.DIRECTION_INPUT)
OUTPUT = 1 (or use ImageCms.DIRECTION_OUTPUT)
PROOF = 2 (or use ImageCms.DIRECTION_PROOF)
:returns: 1 if the intent/direction are supported, -1 if they are not.
:exception PyCMSError:
"""
2010-07-31 06:52:47 +04:00
try:
if not isinstance(profile, ImageCmsProfile):
profile = ImageCmsProfile(profile)
# FIXME: I get different results for the same data w. different
# compilers. Bug in LittleCMS or in the binding?
if profile.profile.is_intent_supported(intent, direction):
return 1
else:
return -1
except (AttributeError, OSError, TypeError, ValueError) as v:
raise PyCMSError(v) from v
2010-07-31 06:52:47 +04:00
2010-07-31 06:52:47 +04:00
def versions():
2014-04-04 02:47:57 +04:00
"""
(pyCMS) Fetches versions.
"""
2019-03-21 16:28:20 +03:00
return (VERSION, core.littlecms_version, sys.version.split()[0], Image.__version__)