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) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 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: 0.0.2 alpha: Minor updates, added interfaces to littleCMS features, - Jan 6, 2003 - fixed some memory holes in how transforms/profiles were created and passed back to Python due to improper destructor setup for PyCObjects - added buildProofTransformFromOpenProfiles() function - eliminated some code redundancy, centralizing several common tasks with internal functions 0.0.1 alpha first public release, Dec. 26, 2002 Known to-do list with current version (of Python interface, not pyCMSdll): none ================================================================================ DIRECTION_INPUT = 0 DIRECTION_OUTPUT = 1 DIRECTION_PROOF = 2 These three constants are used to signify if you intend to use a profile for input, output, or proofing for some functions. The integer equivalents can also be used, as indicated above. ================================================================================ TRUE = 1 FALSE = None (or 0) These constants are used for clarity only, as Python doesn't have a BOOL type. All comparisons within pyCMS are made against TRUE, so it doesn't really matter if you use 0 or None for FALSE. ================================================================================ INTENT_PERCEPTUAL = 0 INTENT_RELATIVE_COLORIMETRIC = 1 INTENT_SATURATION = 2 INTENT_ABSOLUTE_COLORIMETRIC = 3 These constants are used to choose the "Rendering Intent" you wish to use to determine how to fit the input color space into the gamut of the output color space. Because the input and output color spaces are different shapes and sizes, you either have to "clip" colors that are outside of the output gamut, or somehow squeeze everything in. For most "photographic" or pictoral applications, INTENT_PERCEPTUAL is the one you want to use (and it's the default) because it keeps the overal "look and feel" the same from input to output, although the color rendition may not be exact. The input gamut is squeezed into the output color space while keeping rendition as accurate as it can. INTENT_RELATIVE_COLORIMETRIC keeps the relationship between one color and adjacent colors as close as possible, while the exact hue/shade may be sacrificed slightly if necessary. This is useful when you need to distinguish small gradations or changes in color, but perfect reproduction of a specific color is not required. INTENT_SATURATION will attempt to preserve the saturation of a color as best as possible, sacrificing the hue if necessary to fit it in the output gamut. INTENT_ABSOLUTE_COLORIMETRIC will preserve colors within the output gamut exactly, however colors outside of that gamut are "clipped" to the gamut boundary. If you must reproduce an exact color, and you know it is within the output color gamut, use this option. However, if you have many colors near or beyond the gamut of your output device, you will lose detail and may see noticable banding. ================================================================================ PyCMSError PyCMSError is a class that inherits from Exception. pyCMS raises a PyCMSError in many cases where things either won't work (because incorrect information was passed to pyCMS), or don't work (because of an error during processing. In most cases, you'll want to make pyCMS calls within a try:/except statement, catching potential PyCMSErrors and the text associated with them. Example: try: im2 = pyCMS.profileToProfile(im, "c:\\sRGB.icm", "c:\\invalid.icm") except PyCMSError, reason: print "This falied, here's the reason: %s" %reason ================================================================================ PyCMSProfile PyCMSTransform pyCMS uses these wrapper classes to return to your code when you ask for a profile or transform handle. This is done so that additional information about the profile or transform can follow it around and pyCMS has everything it needs when you go to use it again. Currently, PyCMSProfile doesn't hold any additional data, but PyCMSTransform tracks the valid input and output modes for that transform. ================================================================================ pyCMSdll This is the actual C++ DLL that interfaces to littleCMS. It can be called directly from your application if you need to, but pyCMS handles a lot of little details for you automatically that you'd otherwise need to consider. If you'd like to use pyCMSdll directly, consult pyCMSdll.c for details. ================================================================================ versions pyCMS.versions() Returns a tuple of (pyCMSVersion, littleCMSVersion, pythonVersion, PILVersion). However, don't trust these values 100%, because they're currently configured manually in pyCMSdll.c before building the DLL. ================================================================================ about pyCMS.about() Returns a string containing information about pyCMSdll.dll, including copyright and license information. ================================================================================ copyright pyCMS.copyright() Returns a string containing information about pyCMSdll.dll, including copyright and license information. ================================================================================ profileToProfile pyCMS.profileToProfile(im, inputProfile, outputProfile, [renderingIntent], [outputMode], [inPlace]) Returns either None or a new PIL image object, depending on value of inPlace (see below). im = an open PIL image object (i.e. Image.new(...) or Image.open(...), etc.) inputProfile = string, as a valid filename path to the ICC input profile you wish to use for this image outputProfile = string, as a valid filename path to the ICC output profile you wish to use for this image renderingIntent = integer (0-3) specifying the rendering intent you wish to use for the transform INTENT_PERCEPTUAL = 0 (DEFAULT) (pyCMS.INTENT_PERCEPTUAL) INTENT_RELATIVE_COLORIMETRIC =1 (pyCMS.INTENT_RELATIVE_COLORIMETRIC) INTENT_SATURATION = 2 (pyCMS.INTENT_SATURATION) INTENT_ABSOLUTE_COLORIMETRIC =3 (pyCMS.INTENT_ABSOLUTE_COLORIMETRIC) see the pyCMS documentation for details on rendering intents and what they do. 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) inPlace = BOOL (1 = TRUE, None or 0 = FALSE). If TRUE, the original image is modified in-place, and None is returned. If FALSE (default), a new Image object is returned with the transform applied. If the input or output profiles specified are not valid filenames, a PyCMSError will be raised. If inPlace == TRUE and outputMode != im.mode, a PyCMSError will be raised. If an error occurs during application of the profiles, a PyCMSError will be raised. If outputMode is not a mode supported by the outputProfile (or by pyCMS), a PyCMSError will be raised. This function applies an ICC transformation to im from inputProfile's color space to outputProfile's color space using the specified rendering intent to decide how to handle out-of-gamut colors. OutputMode can be used to specify that a color mode conversion is to 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. ================================================================================ getOpenProfile pyCMS.getOpenProfile(profileFilename) Returns a PyCMSProfile class object. profileFilename = string, as a valid filename path to the ICC profile you wish to open The PyCMSProfile object can be passed back into pyCMS for use in creating transforms and such (as in pyCMS.buildTransformFromOpenProfiles()). If profileFilename is not a vaild filename for an ICC profile, a PyCMSError will be raised. ================================================================================ createProfile pyCMS.createProfile(colorSpace, [colorTemp]) Returns a PyCMSProfile class object colorSpace = string, the color space of the profile you wish to create. Currently only "LAB", "XYZ", and "sRGB" are supported. colorTemp = positive integer for the white point for the profile, in degrees Kelvin (i.e. 5000, 6500, 9600, etc.). The default is for D50 illuminant if omitted (5000k). colorTemp is ONLY applied to LAB profiles, and is ignored for XYZ and sRGB. If colorSpace not in ["LAB", "XYZ", "sRGB"], a PyCMSError is raised If using LAB and colorTemp != a positive integer, a PyCMSError is raised. If an error occurs while creating the profile, a PyCMSError is raised. 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 PyCMSProfile object that can be passed to pyCMS.buildTransformFromOpenProfiles() to create a transform to apply to images. ================================================================================ buildTransform pyCMS.buildTransform(inputProfile, outputProfile, inMode, outMode, [renderingIntent]) Returns a PyCMSTransform class object. inputProfile = string, as a valid filename path to the ICC input profile you wish to use for this transform outputProfile = string, as a valid filename path to the ICC output profile you wish to use for this transform inMode = string, as a valid PIL mode that the appropriate profile also supports (i.e. "RGB", "RGBA", "CMYK", etc.) outMode = string, as a valid PIL mode that the appropriate profile also supports (i.e. "RGB", "RGBA", "CMYK", etc.) renderingIntent = integer (0-3) specifying the rendering intent you wish to use for the transform INTENT_PERCEPTUAL = 0 (DEFAULT) (pyCMS.INTENT_PERCEPTUAL) INTENT_RELATIVE_COLORIMETRIC =1 (pyCMS.INTENT_RELATIVE_COLORIMETRIC) INTENT_SATURATION = 2 (pyCMS.INTENT_SATURATION) INTENT_ABSOLUTE_COLORIMETRIC =3 (pyCMS.INTENT_ABSOLUTE_COLORIMETRIC) see the pyCMS documentation for details on rendering intents and what they do. If the input or output profiles specified are not valid filenames, a PyCMSError will be raised. If an error occurs during creation of the transform, a PyCMSError will be raised. If inMode or outMode are not a mode supported by the outputProfile (or by pyCMS), a PyCMSError will be raised. This function builds and returns an ICC transform from the inputProfile to the outputProfile using the renderingIntent to determine what to do with out-of-gamut colors. It will ONLY work for converting images that are in inMode to images that are in outMode color format (PIL mode, i.e. "RGB", "RGBA", "CMYK", etc.). Building the transform is a fair part of the overhead in pyCMS.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 pyCMS.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 the "inMode" and "outMode" attributes of the object (which can be manually overridden if you really want to, but I don't know of any time that would be of use, or would even work). ================================================================================ buildProofTransform pyCMS.buildProofTransform(inputProfile, outputProfile, displayProfile, inMode, outMode, [renderingIntent], [displayRenderingIntent]) Returns a PyCMSTransform class object. inputProfile = string, as a valid filename path to the ICC input profile you wish to use for this transform outputProfile = string, as a valid filename path to the ICC output profile you wish to use for this transform displayProfile = string, as a valid filename path to the ICC display (monitor, usually) profile you wish to use for this transform inMode = string, as a valid PIL mode that the appropriate profile also supports (i.e. "RGB", "RGBA", "CMYK", etc.) outMode = string, as a valid PIL mode that the appropriate profile also supports (i.e. "RGB", "RGBA", "CMYK", etc.) renderingIntent = integer (0-3) specifying the rendering intent you wish to use for the input->output (simulated) transform INTENT_PERCEPTUAL = 0 (DEFAULT) (pyCMS.INTENT_PERCEPTUAL) INTENT_RELATIVE_COLORIMETRIC =1 (pyCMS.INTENT_RELATIVE_COLORIMETRIC) INTENT_SATURATION = 2 (pyCMS.INTENT_SATURATION) INTENT_ABSOLUTE_COLORIMETRIC =3 (pyCMS.INTENT_ABSOLUTE_COLORIMETRIC) see the pyCMS documentation for details on rendering intents and what they do. displayRenderingIntent = integer (0-3) specifying the rendering intent you wish to use for (input/output simulation)->display transform INTENT_PERCEPTUAL = 0 (DEFAULT) (pyCMS.INTENT_PERCEPTUAL) INTENT_RELATIVE_COLORIMETRIC =1 (pyCMS.INTENT_RELATIVE_COLORIMETRIC) INTENT_SATURATION = 2 (pyCMS.INTENT_SATURATION) INTENT_ABSOLUTE_COLORIMETRIC =3 (pyCMS.INTENT_ABSOLUTE_COLORIMETRIC) see the pyCMS documentation for details on rendering intents and what they do. If the input, output, or display profiles specified are not valid filenames, a PyCMSError will be raised. If an error occurs during creation of the transform, a PyCMSError will be raised. If inMode or outMode are not a mode supported by the outputProfile (or by pyCMS), a PyCMSError will be raised. This function builds and returns an ICC transform from the inputProfile to the displayProfile, but tries to simulate the result that would be obtained on the outputProfile device using renderingIntent and displayRenderingIntent to determine what to do with out-of-gamut colors. This is known as "soft-proofing". It will ONLY work for converting images that are in inMode to images that are in outMode color format (PIL mode, i.e. "RGB", "RGBA", "CMYK", etc.). Usage of the resulting transform object is exactly the same as with pyCMS.buildTransform(). Proof profiling is generally used when using a "proof" device to get a good idea of what the final printed/displayed image would look like on the outputProfile device when it's quicker and easier to use the display device for judging color. Generally, this means that displayDevice is a monitor, or a dye-sub printer (etc.), and the output 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 limiting the color gamut on the display device to the gamut availabile on the output device. However, when the final output device has a much wider gamut than the display device, you may obtain marginal results. ================================================================================ buildTransformFromOpenProfiles pyCMS.buildTransformFromOpenProfiles(inputProfile, outputProfile, inMode, outMode, [renderingIntent]) Returns a PyCMSTransform class object inputProfile = a valid PyCMSProfile class object outputProfile = a valid PyCMSProfile class object inMode = string, as a valid PIL mode that the appropriate profile also supports (i.e. "RGB", "RGBA", "CMYK", etc.) outMode = string, as a valid PIL mode that the appropriate profile also supports (i.e. "RGB", "RGBA", "CMYK", etc.) renderingIntent = integer (0-3) specifying the rendering intent you wish to use for the transform INTENT_PERCEPTUAL = 0 (DEFAULT) (pyCMS.INTENT_PERCEPTUAL) INTENT_RELATIVE_COLORIMETRIC =1 (pyCMS.INTENT_RELATIVE_COLORIMETRIC) INTENT_SATURATION = 2 (pyCMS.INTENT_SATURATION) INTENT_ABSOLUTE_COLORIMETRIC =3 (pyCMS.INTENT_ABSOLUTE_COLORIMETRIC) see the pyCMS documentation for details on rendering intents and what they do. This function operates exactly the same as pyCMS.buildTransform() except that the inputProfile and outputProfile arguments must be valid PyCMSProfile objects rather than filenames. Use this function to create pre-calculated transforms from open PyCMSProfiles (such as profiles created on-the-fly using pyCMS.createProfile(), or [in the future] profiles extracted from image files. ================================================================================ buildProofTransformFromOpenProfiles pyCMS.buildProofTransformFromOpenProfiles(inputProfile, outputProfile, displayProfile, inMode, outMode, [renderingIntent], [displayRenderingIntent]) Returns a PyCMSTransform class object inputProfile = a valid PyCMSProfile class object outputProfile = a valid PyCMSProfile class object displayProfile = a valid PyCMSProfile class object inMode = string, as a valid PIL mode that the appropriate profile also supports (i.e. "RGB", "RGBA", "CMYK", etc.) outMode = string, as a valid PIL mode that the appropriate profile also supports (i.e. "RGB", "RGBA", "CMYK", etc.) renderingIntent = integer (0-3) specifying the rendering intent you wish to use for the transform INTENT_PERCEPTUAL = 0 (DEFAULT) (pyCMS.INTENT_PERCEPTUAL) INTENT_RELATIVE_COLORIMETRIC =1 (pyCMS.INTENT_RELATIVE_COLORIMETRIC) INTENT_SATURATION = 2 (pyCMS.INTENT_SATURATION) INTENT_ABSOLUTE_COLORIMETRIC =3 (pyCMS.INTENT_ABSOLUTE_COLORIMETRIC) see the pyCMS documentation for details on rendering intents and what they do. displayRenderingIntent = integer (0-3) specifying the rendering intent you wish to use for the transform INTENT_PERCEPTUAL = 0 (DEFAULT) (pyCMS.INTENT_PERCEPTUAL) INTENT_RELATIVE_COLORIMETRIC =1 (pyCMS.INTENT_RELATIVE_COLORIMETRIC) INTENT_SATURATION = 2 (pyCMS.INTENT_SATURATION) INTENT_ABSOLUTE_COLORIMETRIC =3 (pyCMS.INTENT_ABSOLUTE_COLORIMETRIC) see the pyCMS documentation for details on rendering intents and what they do. This function operates exactly the same as pyCMS.buildProofTransform() except that the profile arguments must be valid PyCMSProfile objects rather than filenames. Use this function to create pre-calculated proof transforms from open PyCMSProfiles (such as profiles created on-the-fly using pyCMS.createProfile(), or [in the future] profiles extracted from image files. ================================================================================ applyTransform pyCMS.applyTransform(im, transform, [inPlace]) Returns either None, or a new PIL Image object, depending on the value of inPlace (see below) im = a PIL Image object, and im.mode must be the same as the inMode supported by the transform. transform = a valid PyCMSTransform class object inPlace = BOOL (1 == TRUE, 0 or None == FALSE). If TRUE, im is modified in place and None is returned, if FALSE, a new Image object with the transform applied is returned (and im is not changed). The default is FALSE. If im.mode != transform.inMode, a PyCMSError is raised. If inPlace == TRUE and transform.inMode != transform.outMode, a PyCMSError is raised. If im.mode, transfer.inMode, or transfer.outMode is not supported by pyCMSdll or the profiles you used for the transform, a PyCMSError is raised. If an error occurs while the transform is being applied, a PyCMSError is raised. This function applies a pre-calculated transform (from pyCMS.buildTransform() or pyCMS.buildTransformFromOpenProfiles()) to an image. The transform can be used for multiple images, saving considerable calcuation time if doing the same conversion multiple times. If you want to modify im in-place instead of receiving a new image as 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 change the mode in-place (the buffer sizes for some modes are different). The default behavior is to return a new Image object of the same dimensions in mode transform.outMode. ================================================================================ getProfileInfo pyCMS.getProfileInfo(profile) Returns a string containing the internal profile information stored in an ICC tag. profile = EITHER a valid PyCMSProfile object, OR a string of the filename of an ICC profile. If profile isn't a valid PyCMSProfile object or filename to a profile, a PyCMSError is raised. If an error occurs while trying to obtain the info tag, a PyCMSError is raised 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. ================================================================================ getProfileName pyCMS.getProfileName(profile) Returns a string containing the internal name of the profile as stored in an ICC tag. profile = EITHER a valid PyCMSProfile object, OR a string of the filename of an ICC profile. If profile isn't a valid PyCMSProfile object or filename to a profile, a PyCMSError is raised If an error occurs while trying to obtain the name tag, a PyCMSError is raised. 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. ================================================================================ getDefaultIntent pyCMS.getDefaultIntent(profile) Returns integer 0-3 specifying the default rendering intent for this profile. INTENT_PERCEPTUAL = 0 (DEFAULT) (pyCMS.INTENT_PERCEPTUAL) INTENT_RELATIVE_COLORIMETRIC =1 (pyCMS.INTENT_RELATIVE_COLORIMETRIC) INTENT_SATURATION = 2 (pyCMS.INTENT_SATURATION) INTENT_ABSOLUTE_COLORIMETRIC =3 (pyCMS.INTENT_ABSOLUTE_COLORIMETRIC) see the pyCMS documentation for details on rendering intents and what they do. profile = EITHER a valid PyCMSProfile object, OR a string of the filename of an ICC profile. If profile isn't a valid PyCMSProfile object or filename to a profile, a PyCMSError is raised. If an error occurs while trying to obtain the default intent, a PyCMSError is raised. Use this function to determine the default (and usually best optomized) 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 pyCMS.isIntentSupported() to verify it will work first. ================================================================================ isIntentSupported pyCMS.isIntentSupported(profile, intent, direction) Returns 1 if the intent/direction are supported, -1 if they are not. profile = EITHER a valid PyCMSProfile object, OR a string of the filename of an ICC profile. intent = integer (0-3) specifying the rendering intent you wish to use with this profile INTENT_PERCEPTUAL = 0 (DEFAULT) (pyCMS.INTENT_PERCEPTUAL) INTENT_RELATIVE_COLORIMETRIC =1 (pyCMS.INTENT_RELATIVE_COLORIMETRIC) INTENT_SATURATION = 2 (pyCMS.INTENT_SATURATION) INTENT_ABSOLUTE_COLORIMETRIC =3 (pyCMS.INTENT_ABSOLUTE_COLORIMETRIC) see the pyCMS documentation for details on rendering intents and what they do. direction = integer specifing if the profile is to be used for input, output, or display/proof INPUT = 0 (or use pyCMS.DIRECTION_INPUT) OUTPUT = 1 (or use pyCMS.DIRECTION_OUTPUT) PROOF (or display) = 2 (or use pyCMS.DIRECTION_PROOF) Use this function to verify that you can use your desired renderingIntent with profile, and that profile can be used for the input/output/display profile as you desire. Some profiles are created specifically for one "direction", can cannot be used for others. Some profiles can only be used for certain rendering intents... so it's best to either verify this before trying to create a transform with them (using this function), or catch the potential PyCMSError that will occur if they don't support the modes you select.