ImageCms.py 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. # The Python Imaging Library.
  2. # $Id$
  3. # Optional color management support, based on Kevin Cazabon's PyCMS
  4. # library.
  5. # Originally released under LGPL. Graciously donated to PIL in
  6. # March 2009, for distribution under the standard PIL license
  7. # History:
  8. # 2009-03-08 fl Added to PIL.
  9. # Copyright (C) 2002-2003 Kevin Cazabon
  10. # Copyright (c) 2009 by Fredrik Lundh
  11. # Copyright (c) 2013 by Eric Soroos
  12. # See the README file for information on usage and redistribution. See
  13. # below for the original description.
  14. from __future__ import annotations
  15. import operator
  16. import sys
  17. from enum import IntEnum, IntFlag
  18. from functools import reduce
  19. from typing import Any, Literal, SupportsFloat, SupportsInt, Union
  20. from . import Image, __version__
  21. from ._deprecate import deprecate
  22. from ._typing import SupportsRead
  23. try:
  24. from . import _imagingcms as core
  25. except ImportError as ex:
  26. # Allow error import for doc purposes, but error out when accessing
  27. # anything in core.
  28. from ._util import DeferredError
  29. core = DeferredError.new(ex)
  30. _DESCRIPTION = """
  31. pyCMS
  32. a Python / PIL interface to the littleCMS ICC Color Management System
  33. Copyright (C) 2002-2003 Kevin Cazabon
  34. kevin@cazabon.com
  35. https://www.cazabon.com
  36. pyCMS home page: https://www.cazabon.com/pyCMS
  37. littleCMS home page: https://www.littlecms.com
  38. (littleCMS is Copyright (C) 1998-2001 Marti Maria)
  39. Originally released under LGPL. Graciously donated to PIL in
  40. March 2009, for distribution under the standard PIL license
  41. The pyCMS.py module provides a "clean" interface between Python/PIL and
  42. pyCMSdll, taking care of some of the more complex handling of the direct
  43. pyCMSdll functions, as well as error-checking and making sure that all
  44. relevant data is kept together.
  45. While it is possible to call pyCMSdll functions directly, it's not highly
  46. recommended.
  47. Version History:
  48. 1.0.0 pil Oct 2013 Port to LCMS 2.
  49. 0.1.0 pil mod March 10, 2009
  50. Renamed display profile to proof profile. The proof
  51. profile is the profile of the device that is being
  52. simulated, not the profile of the device which is
  53. actually used to display/print the final simulation
  54. (that'd be the output profile) - also see LCMSAPI.txt
  55. input colorspace -> using 'renderingIntent' -> proof
  56. colorspace -> using 'proofRenderingIntent' -> output
  57. colorspace
  58. Added LCMS FLAGS support.
  59. Added FLAGS["SOFTPROOFING"] as default flag for
  60. buildProofTransform (otherwise the proof profile/intent
  61. would be ignored).
  62. 0.1.0 pil March 2009 - added to PIL, as PIL.ImageCms
  63. 0.0.2 alpha Jan 6, 2002
  64. Added try/except statements around type() checks of
  65. potential CObjects... Python won't let you use type()
  66. on them, and raises a TypeError (stupid, if you ask
  67. me!)
  68. Added buildProofTransformFromOpenProfiles() function.
  69. Additional fixes in DLL, see DLL code for details.
  70. 0.0.1 alpha first public release, Dec. 26, 2002
  71. Known to-do list with current version (of Python interface, not pyCMSdll):
  72. none
  73. """
  74. _VERSION = "1.0.0 pil"
  75. def __getattr__(name: str) -> Any:
  76. if name == "DESCRIPTION":
  77. deprecate("PIL.ImageCms.DESCRIPTION", 12)
  78. return _DESCRIPTION
  79. elif name == "VERSION":
  80. deprecate("PIL.ImageCms.VERSION", 12)
  81. return _VERSION
  82. elif name == "FLAGS":
  83. deprecate("PIL.ImageCms.FLAGS", 12, "PIL.ImageCms.Flags")
  84. return _FLAGS
  85. msg = f"module '{__name__}' has no attribute '{name}'"
  86. raise AttributeError(msg)
  87. # --------------------------------------------------------------------.
  88. #
  89. # intent/direction values
  90. class Intent(IntEnum):
  91. PERCEPTUAL = 0
  92. RELATIVE_COLORIMETRIC = 1
  93. SATURATION = 2
  94. ABSOLUTE_COLORIMETRIC = 3
  95. class Direction(IntEnum):
  96. INPUT = 0
  97. OUTPUT = 1
  98. PROOF = 2
  99. #
  100. # flags
  101. class Flags(IntFlag):
  102. """Flags and documentation are taken from ``lcms2.h``."""
  103. NONE = 0
  104. NOCACHE = 0x0040
  105. """Inhibit 1-pixel cache"""
  106. NOOPTIMIZE = 0x0100
  107. """Inhibit optimizations"""
  108. NULLTRANSFORM = 0x0200
  109. """Don't transform anyway"""
  110. GAMUTCHECK = 0x1000
  111. """Out of Gamut alarm"""
  112. SOFTPROOFING = 0x4000
  113. """Do softproofing"""
  114. BLACKPOINTCOMPENSATION = 0x2000
  115. NOWHITEONWHITEFIXUP = 0x0004
  116. """Don't fix scum dot"""
  117. HIGHRESPRECALC = 0x0400
  118. """Use more memory to give better accuracy"""
  119. LOWRESPRECALC = 0x0800
  120. """Use less memory to minimize resources"""
  121. # this should be 8BITS_DEVICELINK, but that is not a valid name in Python:
  122. USE_8BITS_DEVICELINK = 0x0008
  123. """Create 8 bits devicelinks"""
  124. GUESSDEVICECLASS = 0x0020
  125. """Guess device class (for ``transform2devicelink``)"""
  126. KEEP_SEQUENCE = 0x0080
  127. """Keep profile sequence for devicelink creation"""
  128. FORCE_CLUT = 0x0002
  129. """Force CLUT optimization"""
  130. CLUT_POST_LINEARIZATION = 0x0001
  131. """create postlinearization tables if possible"""
  132. CLUT_PRE_LINEARIZATION = 0x0010
  133. """create prelinearization tables if possible"""
  134. NONEGATIVES = 0x8000
  135. """Prevent negative numbers in floating point transforms"""
  136. COPY_ALPHA = 0x04000000
  137. """Alpha channels are copied on ``cmsDoTransform()``"""
  138. NODEFAULTRESOURCEDEF = 0x01000000
  139. _GRIDPOINTS_1 = 1 << 16
  140. _GRIDPOINTS_2 = 2 << 16
  141. _GRIDPOINTS_4 = 4 << 16
  142. _GRIDPOINTS_8 = 8 << 16
  143. _GRIDPOINTS_16 = 16 << 16
  144. _GRIDPOINTS_32 = 32 << 16
  145. _GRIDPOINTS_64 = 64 << 16
  146. _GRIDPOINTS_128 = 128 << 16
  147. @staticmethod
  148. def GRIDPOINTS(n: int) -> Flags:
  149. """
  150. Fine-tune control over number of gridpoints
  151. :param n: :py:class:`int` in range ``0 <= n <= 255``
  152. """
  153. return Flags.NONE | ((n & 0xFF) << 16)
  154. _MAX_FLAG = reduce(operator.or_, Flags)
  155. _FLAGS = {
  156. "MATRIXINPUT": 1,
  157. "MATRIXOUTPUT": 2,
  158. "MATRIXONLY": (1 | 2),
  159. "NOWHITEONWHITEFIXUP": 4, # Don't hot fix scum dot
  160. # Don't create prelinearization tables on precalculated transforms
  161. # (internal use):
  162. "NOPRELINEARIZATION": 16,
  163. "GUESSDEVICECLASS": 32, # Guess device class (for transform2devicelink)
  164. "NOTCACHE": 64, # Inhibit 1-pixel cache
  165. "NOTPRECALC": 256,
  166. "NULLTRANSFORM": 512, # Don't transform anyway
  167. "HIGHRESPRECALC": 1024, # Use more memory to give better accuracy
  168. "LOWRESPRECALC": 2048, # Use less memory to minimize resources
  169. "WHITEBLACKCOMPENSATION": 8192,
  170. "BLACKPOINTCOMPENSATION": 8192,
  171. "GAMUTCHECK": 4096, # Out of Gamut alarm
  172. "SOFTPROOFING": 16384, # Do softproofing
  173. "PRESERVEBLACK": 32768, # Black preservation
  174. "NODEFAULTRESOURCEDEF": 16777216, # CRD special
  175. "GRIDPOINTS": lambda n: (n & 0xFF) << 16, # Gridpoints
  176. }
  177. # --------------------------------------------------------------------.
  178. # Experimental PIL-level API
  179. # --------------------------------------------------------------------.
  180. ##
  181. # Profile.
  182. class ImageCmsProfile:
  183. def __init__(self, profile: str | SupportsRead[bytes] | core.CmsProfile) -> None:
  184. """
  185. :param profile: Either a string representing a filename,
  186. a file like object containing a profile or a
  187. low-level profile object
  188. """
  189. if isinstance(profile, str):
  190. if sys.platform == "win32":
  191. profile_bytes_path = profile.encode()
  192. try:
  193. profile_bytes_path.decode("ascii")
  194. except UnicodeDecodeError:
  195. with open(profile, "rb") as f:
  196. self._set(core.profile_frombytes(f.read()))
  197. return
  198. self._set(core.profile_open(profile), profile)
  199. elif hasattr(profile, "read"):
  200. self._set(core.profile_frombytes(profile.read()))
  201. elif isinstance(profile, core.CmsProfile):
  202. self._set(profile)
  203. else:
  204. msg = "Invalid type for Profile" # type: ignore[unreachable]
  205. raise TypeError(msg)
  206. def _set(self, profile: core.CmsProfile, filename: str | None = None) -> None:
  207. self.profile = profile
  208. self.filename = filename
  209. self.product_name = None # profile.product_name
  210. self.product_info = None # profile.product_info
  211. def tobytes(self) -> bytes:
  212. """
  213. Returns the profile in a format suitable for embedding in
  214. saved images.
  215. :returns: a bytes object containing the ICC profile.
  216. """
  217. return core.profile_tobytes(self.profile)
  218. class ImageCmsTransform(Image.ImagePointHandler):
  219. """
  220. Transform. This can be used with the procedural API, or with the standard
  221. :py:func:`~PIL.Image.Image.point` method.
  222. Will return the output profile in the ``output.info['icc_profile']``.
  223. """
  224. def __init__(
  225. self,
  226. input: ImageCmsProfile,
  227. output: ImageCmsProfile,
  228. input_mode: str,
  229. output_mode: str,
  230. intent: Intent = Intent.PERCEPTUAL,
  231. proof: ImageCmsProfile | None = None,
  232. proof_intent: Intent = Intent.ABSOLUTE_COLORIMETRIC,
  233. flags: Flags = Flags.NONE,
  234. ):
  235. supported_modes = (
  236. "RGB",
  237. "RGBA",
  238. "RGBX",
  239. "CMYK",
  240. "I;16",
  241. "I;16L",
  242. "I;16B",
  243. "YCbCr",
  244. "LAB",
  245. "L",
  246. "1",
  247. )
  248. for mode in (input_mode, output_mode):
  249. if mode not in supported_modes:
  250. deprecate(
  251. mode,
  252. 12,
  253. {
  254. "L;16": "I;16 or I;16L",
  255. "L:16B": "I;16B",
  256. "YCCA": "YCbCr",
  257. "YCC": "YCbCr",
  258. }.get(mode),
  259. )
  260. if proof is None:
  261. self.transform = core.buildTransform(
  262. input.profile, output.profile, input_mode, output_mode, intent, flags
  263. )
  264. else:
  265. self.transform = core.buildProofTransform(
  266. input.profile,
  267. output.profile,
  268. proof.profile,
  269. input_mode,
  270. output_mode,
  271. intent,
  272. proof_intent,
  273. flags,
  274. )
  275. # Note: inputMode and outputMode are for pyCMS compatibility only
  276. self.input_mode = self.inputMode = input_mode
  277. self.output_mode = self.outputMode = output_mode
  278. self.output_profile = output
  279. def point(self, im: Image.Image) -> Image.Image:
  280. return self.apply(im)
  281. def apply(self, im: Image.Image, imOut: Image.Image | None = None) -> Image.Image:
  282. im.load()
  283. if imOut is None:
  284. imOut = Image.new(self.output_mode, im.size, None)
  285. self.transform.apply(im.im.id, imOut.im.id)
  286. imOut.info["icc_profile"] = self.output_profile.tobytes()
  287. return imOut
  288. def apply_in_place(self, im: Image.Image) -> Image.Image:
  289. im.load()
  290. if im.mode != self.output_mode:
  291. msg = "mode mismatch"
  292. raise ValueError(msg) # wrong output mode
  293. self.transform.apply(im.im.id, im.im.id)
  294. im.info["icc_profile"] = self.output_profile.tobytes()
  295. return im
  296. def get_display_profile(handle: SupportsInt | None = None) -> ImageCmsProfile | None:
  297. """
  298. (experimental) Fetches the profile for the current display device.
  299. :returns: ``None`` if the profile is not known.
  300. """
  301. if sys.platform != "win32":
  302. return None
  303. from . import ImageWin # type: ignore[unused-ignore, unreachable]
  304. if isinstance(handle, ImageWin.HDC):
  305. profile = core.get_display_profile_win32(int(handle), 1)
  306. else:
  307. profile = core.get_display_profile_win32(int(handle or 0))
  308. if profile is None:
  309. return None
  310. return ImageCmsProfile(profile)
  311. # --------------------------------------------------------------------.
  312. # pyCMS compatible layer
  313. # --------------------------------------------------------------------.
  314. _CmsProfileCompatible = Union[
  315. str, SupportsRead[bytes], core.CmsProfile, ImageCmsProfile
  316. ]
  317. class PyCMSError(Exception):
  318. """(pyCMS) Exception class.
  319. This is used for all errors in the pyCMS API."""
  320. pass
  321. def profileToProfile(
  322. im: Image.Image,
  323. inputProfile: _CmsProfileCompatible,
  324. outputProfile: _CmsProfileCompatible,
  325. renderingIntent: Intent = Intent.PERCEPTUAL,
  326. outputMode: str | None = None,
  327. inPlace: bool = False,
  328. flags: Flags = Flags.NONE,
  329. ) -> Image.Image | None:
  330. """
  331. (pyCMS) Applies an ICC transformation to a given image, mapping from
  332. ``inputProfile`` to ``outputProfile``.
  333. If the input or output profiles specified are not valid filenames, a
  334. :exc:`PyCMSError` will be raised. If ``inPlace`` is ``True`` and
  335. ``outputMode != im.mode``, a :exc:`PyCMSError` will be raised.
  336. If an error occurs during application of the profiles,
  337. a :exc:`PyCMSError` will be raised.
  338. If ``outputMode`` is not a mode supported by the ``outputProfile`` (or by pyCMS),
  339. a :exc:`PyCMSError` will be raised.
  340. This function applies an ICC transformation to im from ``inputProfile``'s
  341. color space to ``outputProfile``'s color space using the specified rendering
  342. intent to decide how to handle out-of-gamut colors.
  343. ``outputMode`` can be used to specify that a color mode conversion is to
  344. be done using these profiles, but the specified profiles must be able
  345. to handle that mode. I.e., if converting im from RGB to CMYK using
  346. profiles, the input profile must handle RGB data, and the output
  347. profile must handle CMYK data.
  348. :param im: An open :py:class:`~PIL.Image.Image` object (i.e. Image.new(...)
  349. or Image.open(...), etc.)
  350. :param inputProfile: String, as a valid filename path to the ICC input
  351. profile you wish to use for this image, or a profile object
  352. :param outputProfile: String, as a valid filename path to the ICC output
  353. profile you wish to use for this image, or a profile object
  354. :param renderingIntent: Integer (0-3) specifying the rendering intent you
  355. wish to use for the transform
  356. ImageCms.Intent.PERCEPTUAL = 0 (DEFAULT)
  357. ImageCms.Intent.RELATIVE_COLORIMETRIC = 1
  358. ImageCms.Intent.SATURATION = 2
  359. ImageCms.Intent.ABSOLUTE_COLORIMETRIC = 3
  360. see the pyCMS documentation for details on rendering intents and what
  361. they do.
  362. :param outputMode: A valid PIL mode for the output image (i.e. "RGB",
  363. "CMYK", etc.). Note: if rendering the image "inPlace", outputMode
  364. MUST be the same mode as the input, or omitted completely. If
  365. omitted, the outputMode will be the same as the mode of the input
  366. image (im.mode)
  367. :param inPlace: Boolean. If ``True``, the original image is modified in-place,
  368. and ``None`` is returned. If ``False`` (default), a new
  369. :py:class:`~PIL.Image.Image` object is returned with the transform applied.
  370. :param flags: Integer (0-...) specifying additional flags
  371. :returns: Either None or a new :py:class:`~PIL.Image.Image` object, depending on
  372. the value of ``inPlace``
  373. :exception PyCMSError:
  374. """
  375. if outputMode is None:
  376. outputMode = im.mode
  377. if not isinstance(renderingIntent, int) or not (0 <= renderingIntent <= 3):
  378. msg = "renderingIntent must be an integer between 0 and 3"
  379. raise PyCMSError(msg)
  380. if not isinstance(flags, int) or not (0 <= flags <= _MAX_FLAG):
  381. msg = f"flags must be an integer between 0 and {_MAX_FLAG}"
  382. raise PyCMSError(msg)
  383. try:
  384. if not isinstance(inputProfile, ImageCmsProfile):
  385. inputProfile = ImageCmsProfile(inputProfile)
  386. if not isinstance(outputProfile, ImageCmsProfile):
  387. outputProfile = ImageCmsProfile(outputProfile)
  388. transform = ImageCmsTransform(
  389. inputProfile,
  390. outputProfile,
  391. im.mode,
  392. outputMode,
  393. renderingIntent,
  394. flags=flags,
  395. )
  396. if inPlace:
  397. transform.apply_in_place(im)
  398. imOut = None
  399. else:
  400. imOut = transform.apply(im)
  401. except (OSError, TypeError, ValueError) as v:
  402. raise PyCMSError(v) from v
  403. return imOut
  404. def getOpenProfile(
  405. profileFilename: str | SupportsRead[bytes] | core.CmsProfile,
  406. ) -> ImageCmsProfile:
  407. """
  408. (pyCMS) Opens an ICC profile file.
  409. The PyCMSProfile object can be passed back into pyCMS for use in creating
  410. transforms and such (as in ImageCms.buildTransformFromOpenProfiles()).
  411. If ``profileFilename`` is not a valid filename for an ICC profile,
  412. a :exc:`PyCMSError` will be raised.
  413. :param profileFilename: String, as a valid filename path to the ICC profile
  414. you wish to open, or a file-like object.
  415. :returns: A CmsProfile class object.
  416. :exception PyCMSError:
  417. """
  418. try:
  419. return ImageCmsProfile(profileFilename)
  420. except (OSError, TypeError, ValueError) as v:
  421. raise PyCMSError(v) from v
  422. def buildTransform(
  423. inputProfile: _CmsProfileCompatible,
  424. outputProfile: _CmsProfileCompatible,
  425. inMode: str,
  426. outMode: str,
  427. renderingIntent: Intent = Intent.PERCEPTUAL,
  428. flags: Flags = Flags.NONE,
  429. ) -> ImageCmsTransform:
  430. """
  431. (pyCMS) Builds an ICC transform mapping from the ``inputProfile`` to the
  432. ``outputProfile``. Use applyTransform to apply the transform to a given
  433. image.
  434. If the input or output profiles specified are not valid filenames, a
  435. :exc:`PyCMSError` will be raised. If an error occurs during creation
  436. of the transform, a :exc:`PyCMSError` will be raised.
  437. If ``inMode`` or ``outMode`` are not a mode supported by the ``outputProfile``
  438. (or by pyCMS), a :exc:`PyCMSError` will be raised.
  439. This function builds and returns an ICC transform from the ``inputProfile``
  440. to the ``outputProfile`` using the ``renderingIntent`` to determine what to do
  441. with out-of-gamut colors. It will ONLY work for converting images that
  442. are in ``inMode`` to images that are in ``outMode`` color format (PIL mode,
  443. i.e. "RGB", "RGBA", "CMYK", etc.).
  444. Building the transform is a fair part of the overhead in
  445. ImageCms.profileToProfile(), so if you're planning on converting multiple
  446. images using the same input/output settings, this can save you time.
  447. Once you have a transform object, it can be used with
  448. ImageCms.applyProfile() to convert images without the need to re-compute
  449. the lookup table for the transform.
  450. The reason pyCMS returns a class object rather than a handle directly
  451. to the transform is that it needs to keep track of the PIL input/output
  452. modes that the transform is meant for. These attributes are stored in
  453. the ``inMode`` and ``outMode`` attributes of the object (which can be
  454. manually overridden if you really want to, but I don't know of any
  455. time that would be of use, or would even work).
  456. :param inputProfile: String, as a valid filename path to the ICC input
  457. profile you wish to use for this transform, or a profile object
  458. :param outputProfile: String, as a valid filename path to the ICC output
  459. profile you wish to use for this transform, or a profile object
  460. :param inMode: String, as a valid PIL mode that the appropriate profile
  461. also supports (i.e. "RGB", "RGBA", "CMYK", etc.)
  462. :param outMode: String, as a valid PIL mode that the appropriate profile
  463. also supports (i.e. "RGB", "RGBA", "CMYK", etc.)
  464. :param renderingIntent: Integer (0-3) specifying the rendering intent you
  465. wish to use for the transform
  466. ImageCms.Intent.PERCEPTUAL = 0 (DEFAULT)
  467. ImageCms.Intent.RELATIVE_COLORIMETRIC = 1
  468. ImageCms.Intent.SATURATION = 2
  469. ImageCms.Intent.ABSOLUTE_COLORIMETRIC = 3
  470. see the pyCMS documentation for details on rendering intents and what
  471. they do.
  472. :param flags: Integer (0-...) specifying additional flags
  473. :returns: A CmsTransform class object.
  474. :exception PyCMSError:
  475. """
  476. if not isinstance(renderingIntent, int) or not (0 <= renderingIntent <= 3):
  477. msg = "renderingIntent must be an integer between 0 and 3"
  478. raise PyCMSError(msg)
  479. if not isinstance(flags, int) or not (0 <= flags <= _MAX_FLAG):
  480. msg = f"flags must be an integer between 0 and {_MAX_FLAG}"
  481. raise PyCMSError(msg)
  482. try:
  483. if not isinstance(inputProfile, ImageCmsProfile):
  484. inputProfile = ImageCmsProfile(inputProfile)
  485. if not isinstance(outputProfile, ImageCmsProfile):
  486. outputProfile = ImageCmsProfile(outputProfile)
  487. return ImageCmsTransform(
  488. inputProfile, outputProfile, inMode, outMode, renderingIntent, flags=flags
  489. )
  490. except (OSError, TypeError, ValueError) as v:
  491. raise PyCMSError(v) from v
  492. def buildProofTransform(
  493. inputProfile: _CmsProfileCompatible,
  494. outputProfile: _CmsProfileCompatible,
  495. proofProfile: _CmsProfileCompatible,
  496. inMode: str,
  497. outMode: str,
  498. renderingIntent: Intent = Intent.PERCEPTUAL,
  499. proofRenderingIntent: Intent = Intent.ABSOLUTE_COLORIMETRIC,
  500. flags: Flags = Flags.SOFTPROOFING,
  501. ) -> ImageCmsTransform:
  502. """
  503. (pyCMS) Builds an ICC transform mapping from the ``inputProfile`` to the
  504. ``outputProfile``, but tries to simulate the result that would be
  505. obtained on the ``proofProfile`` device.
  506. If the input, output, or proof profiles specified are not valid
  507. filenames, a :exc:`PyCMSError` will be raised.
  508. If an error occurs during creation of the transform,
  509. a :exc:`PyCMSError` will be raised.
  510. If ``inMode`` or ``outMode`` are not a mode supported by the ``outputProfile``
  511. (or by pyCMS), a :exc:`PyCMSError` will be raised.
  512. This function builds and returns an ICC transform from the ``inputProfile``
  513. to the ``outputProfile``, but tries to simulate the result that would be
  514. obtained on the ``proofProfile`` device using ``renderingIntent`` and
  515. ``proofRenderingIntent`` to determine what to do with out-of-gamut
  516. colors. This is known as "soft-proofing". It will ONLY work for
  517. converting images that are in ``inMode`` to images that are in outMode
  518. color format (PIL mode, i.e. "RGB", "RGBA", "CMYK", etc.).
  519. Usage of the resulting transform object is exactly the same as with
  520. ImageCms.buildTransform().
  521. Proof profiling is generally used when using an output device to get a
  522. good idea of what the final printed/displayed image would look like on
  523. the ``proofProfile`` device when it's quicker and easier to use the
  524. output device for judging color. Generally, this means that the
  525. output device is a monitor, or a dye-sub printer (etc.), and the simulated
  526. device is something more expensive, complicated, or time consuming
  527. (making it difficult to make a real print for color judgement purposes).
  528. Soft-proofing basically functions by adjusting the colors on the
  529. output device to match the colors of the device being simulated. However,
  530. when the simulated device has a much wider gamut than the output
  531. device, you may obtain marginal results.
  532. :param inputProfile: String, as a valid filename path to the ICC input
  533. profile you wish to use for this transform, or a profile object
  534. :param outputProfile: String, as a valid filename path to the ICC output
  535. (monitor, usually) profile you wish to use for this transform, or a
  536. profile object
  537. :param proofProfile: String, as a valid filename path to the ICC proof
  538. profile you wish to use for this transform, or a profile object
  539. :param inMode: String, as a valid PIL mode that the appropriate profile
  540. also supports (i.e. "RGB", "RGBA", "CMYK", etc.)
  541. :param outMode: String, as a valid PIL mode that the appropriate profile
  542. also supports (i.e. "RGB", "RGBA", "CMYK", etc.)
  543. :param renderingIntent: Integer (0-3) specifying the rendering intent you
  544. wish to use for the input->proof (simulated) transform
  545. ImageCms.Intent.PERCEPTUAL = 0 (DEFAULT)
  546. ImageCms.Intent.RELATIVE_COLORIMETRIC = 1
  547. ImageCms.Intent.SATURATION = 2
  548. ImageCms.Intent.ABSOLUTE_COLORIMETRIC = 3
  549. see the pyCMS documentation for details on rendering intents and what
  550. they do.
  551. :param proofRenderingIntent: Integer (0-3) specifying the rendering intent
  552. you wish to use for proof->output transform
  553. ImageCms.Intent.PERCEPTUAL = 0 (DEFAULT)
  554. ImageCms.Intent.RELATIVE_COLORIMETRIC = 1
  555. ImageCms.Intent.SATURATION = 2
  556. ImageCms.Intent.ABSOLUTE_COLORIMETRIC = 3
  557. see the pyCMS documentation for details on rendering intents and what
  558. they do.
  559. :param flags: Integer (0-...) specifying additional flags
  560. :returns: A CmsTransform class object.
  561. :exception PyCMSError:
  562. """
  563. if not isinstance(renderingIntent, int) or not (0 <= renderingIntent <= 3):
  564. msg = "renderingIntent must be an integer between 0 and 3"
  565. raise PyCMSError(msg)
  566. if not isinstance(flags, int) or not (0 <= flags <= _MAX_FLAG):
  567. msg = f"flags must be an integer between 0 and {_MAX_FLAG}"
  568. raise PyCMSError(msg)
  569. try:
  570. if not isinstance(inputProfile, ImageCmsProfile):
  571. inputProfile = ImageCmsProfile(inputProfile)
  572. if not isinstance(outputProfile, ImageCmsProfile):
  573. outputProfile = ImageCmsProfile(outputProfile)
  574. if not isinstance(proofProfile, ImageCmsProfile):
  575. proofProfile = ImageCmsProfile(proofProfile)
  576. return ImageCmsTransform(
  577. inputProfile,
  578. outputProfile,
  579. inMode,
  580. outMode,
  581. renderingIntent,
  582. proofProfile,
  583. proofRenderingIntent,
  584. flags,
  585. )
  586. except (OSError, TypeError, ValueError) as v:
  587. raise PyCMSError(v) from v
  588. buildTransformFromOpenProfiles = buildTransform
  589. buildProofTransformFromOpenProfiles = buildProofTransform
  590. def applyTransform(
  591. im: Image.Image, transform: ImageCmsTransform, inPlace: bool = False
  592. ) -> Image.Image | None:
  593. """
  594. (pyCMS) Applies a transform to a given image.
  595. If ``im.mode != transform.input_mode``, a :exc:`PyCMSError` is raised.
  596. If ``inPlace`` is ``True`` and ``transform.input_mode != transform.output_mode``, a
  597. :exc:`PyCMSError` is raised.
  598. If ``im.mode``, ``transform.input_mode`` or ``transform.output_mode`` is not
  599. supported by pyCMSdll or the profiles you used for the transform, a
  600. :exc:`PyCMSError` is raised.
  601. If an error occurs while the transform is being applied,
  602. a :exc:`PyCMSError` is raised.
  603. This function applies a pre-calculated transform (from
  604. ImageCms.buildTransform() or ImageCms.buildTransformFromOpenProfiles())
  605. to an image. The transform can be used for multiple images, saving
  606. considerable calculation time if doing the same conversion multiple times.
  607. If you want to modify im in-place instead of receiving a new image as
  608. the return value, set ``inPlace`` to ``True``. This can only be done if
  609. ``transform.input_mode`` and ``transform.output_mode`` are the same, because we
  610. can't change the mode in-place (the buffer sizes for some modes are
  611. different). The default behavior is to return a new :py:class:`~PIL.Image.Image`
  612. object of the same dimensions in mode ``transform.output_mode``.
  613. :param im: An :py:class:`~PIL.Image.Image` object, and ``im.mode`` must be the same
  614. as the ``input_mode`` supported by the transform.
  615. :param transform: A valid CmsTransform class object
  616. :param inPlace: Bool. If ``True``, ``im`` is modified in place and ``None`` is
  617. returned, if ``False``, a new :py:class:`~PIL.Image.Image` object with the
  618. transform applied is returned (and ``im`` is not changed). The default is
  619. ``False``.
  620. :returns: Either ``None``, or a new :py:class:`~PIL.Image.Image` object,
  621. depending on the value of ``inPlace``. The profile will be returned in
  622. the image's ``info['icc_profile']``.
  623. :exception PyCMSError:
  624. """
  625. try:
  626. if inPlace:
  627. transform.apply_in_place(im)
  628. imOut = None
  629. else:
  630. imOut = transform.apply(im)
  631. except (TypeError, ValueError) as v:
  632. raise PyCMSError(v) from v
  633. return imOut
  634. def createProfile(
  635. colorSpace: Literal["LAB", "XYZ", "sRGB"], colorTemp: SupportsFloat = 0
  636. ) -> core.CmsProfile:
  637. """
  638. (pyCMS) Creates a profile.
  639. If colorSpace not in ``["LAB", "XYZ", "sRGB"]``,
  640. a :exc:`PyCMSError` is raised.
  641. If using LAB and ``colorTemp`` is not a positive integer,
  642. a :exc:`PyCMSError` is raised.
  643. If an error occurs while creating the profile,
  644. a :exc:`PyCMSError` is raised.
  645. Use this function to create common profiles on-the-fly instead of
  646. having to supply a profile on disk and knowing the path to it. It
  647. returns a normal CmsProfile object that can be passed to
  648. ImageCms.buildTransformFromOpenProfiles() to create a transform to apply
  649. to images.
  650. :param colorSpace: String, the color space of the profile you wish to
  651. create.
  652. Currently only "LAB", "XYZ", and "sRGB" are supported.
  653. :param colorTemp: Positive number for the white point for the profile, in
  654. degrees Kelvin (i.e. 5000, 6500, 9600, etc.). The default is for D50
  655. illuminant if omitted (5000k). colorTemp is ONLY applied to LAB
  656. profiles, and is ignored for XYZ and sRGB.
  657. :returns: A CmsProfile class object
  658. :exception PyCMSError:
  659. """
  660. if colorSpace not in ["LAB", "XYZ", "sRGB"]:
  661. msg = (
  662. f"Color space not supported for on-the-fly profile creation ({colorSpace})"
  663. )
  664. raise PyCMSError(msg)
  665. if colorSpace == "LAB":
  666. try:
  667. colorTemp = float(colorTemp)
  668. except (TypeError, ValueError) as e:
  669. msg = f'Color temperature must be numeric, "{colorTemp}" not valid'
  670. raise PyCMSError(msg) from e
  671. try:
  672. return core.createProfile(colorSpace, colorTemp)
  673. except (TypeError, ValueError) as v:
  674. raise PyCMSError(v) from v
  675. def getProfileName(profile: _CmsProfileCompatible) -> str:
  676. """
  677. (pyCMS) Gets the internal product name for the given profile.
  678. If ``profile`` isn't a valid CmsProfile object or filename to a profile,
  679. a :exc:`PyCMSError` is raised If an error occurs while trying
  680. to obtain the name tag, a :exc:`PyCMSError` is raised.
  681. Use this function to obtain the INTERNAL name of the profile (stored
  682. in an ICC tag in the profile itself), usually the one used when the
  683. profile was originally created. Sometimes this tag also contains
  684. additional information supplied by the creator.
  685. :param profile: EITHER a valid CmsProfile object, OR a string of the
  686. filename of an ICC profile.
  687. :returns: A string containing the internal name of the profile as stored
  688. in an ICC tag.
  689. :exception PyCMSError:
  690. """
  691. try:
  692. # add an extra newline to preserve pyCMS compatibility
  693. if not isinstance(profile, ImageCmsProfile):
  694. profile = ImageCmsProfile(profile)
  695. # do it in python, not c.
  696. # // name was "%s - %s" (model, manufacturer) || Description ,
  697. # // but if the Model and Manufacturer were the same or the model
  698. # // was long, Just the model, in 1.x
  699. model = profile.profile.model
  700. manufacturer = profile.profile.manufacturer
  701. if not (model or manufacturer):
  702. return (profile.profile.profile_description or "") + "\n"
  703. if not manufacturer or (model and len(model) > 30):
  704. return f"{model}\n"
  705. return f"{model} - {manufacturer}\n"
  706. except (AttributeError, OSError, TypeError, ValueError) as v:
  707. raise PyCMSError(v) from v
  708. def getProfileInfo(profile: _CmsProfileCompatible) -> str:
  709. """
  710. (pyCMS) Gets the internal product information for the given profile.
  711. If ``profile`` isn't a valid CmsProfile object or filename to a profile,
  712. a :exc:`PyCMSError` is raised.
  713. If an error occurs while trying to obtain the info tag,
  714. a :exc:`PyCMSError` is raised.
  715. Use this function to obtain the information stored in the profile's
  716. info tag. This often contains details about the profile, and how it
  717. was created, as supplied by the creator.
  718. :param profile: EITHER a valid CmsProfile object, OR a string of the
  719. filename of an ICC profile.
  720. :returns: A string containing the internal profile information stored in
  721. an ICC tag.
  722. :exception PyCMSError:
  723. """
  724. try:
  725. if not isinstance(profile, ImageCmsProfile):
  726. profile = ImageCmsProfile(profile)
  727. # add an extra newline to preserve pyCMS compatibility
  728. # Python, not C. the white point bits weren't working well,
  729. # so skipping.
  730. # info was description \r\n\r\n copyright \r\n\r\n K007 tag \r\n\r\n whitepoint
  731. description = profile.profile.profile_description
  732. cpright = profile.profile.copyright
  733. elements = [element for element in (description, cpright) if element]
  734. return "\r\n\r\n".join(elements) + "\r\n\r\n"
  735. except (AttributeError, OSError, TypeError, ValueError) as v:
  736. raise PyCMSError(v) from v
  737. def getProfileCopyright(profile: _CmsProfileCompatible) -> str:
  738. """
  739. (pyCMS) Gets the copyright for the given profile.
  740. If ``profile`` isn't a valid CmsProfile object or filename to a profile, a
  741. :exc:`PyCMSError` is raised.
  742. If an error occurs while trying to obtain the copyright tag,
  743. a :exc:`PyCMSError` is raised.
  744. Use this function to obtain the information stored in the profile's
  745. copyright tag.
  746. :param profile: EITHER a valid CmsProfile object, OR a string of the
  747. filename of an ICC profile.
  748. :returns: A string containing the internal profile information stored in
  749. an ICC tag.
  750. :exception PyCMSError:
  751. """
  752. try:
  753. # add an extra newline to preserve pyCMS compatibility
  754. if not isinstance(profile, ImageCmsProfile):
  755. profile = ImageCmsProfile(profile)
  756. return (profile.profile.copyright or "") + "\n"
  757. except (AttributeError, OSError, TypeError, ValueError) as v:
  758. raise PyCMSError(v) from v
  759. def getProfileManufacturer(profile: _CmsProfileCompatible) -> str:
  760. """
  761. (pyCMS) Gets the manufacturer for the given profile.
  762. If ``profile`` isn't a valid CmsProfile object or filename to a profile, a
  763. :exc:`PyCMSError` is raised.
  764. If an error occurs while trying to obtain the manufacturer tag, a
  765. :exc:`PyCMSError` is raised.
  766. Use this function to obtain the information stored in the profile's
  767. manufacturer tag.
  768. :param profile: EITHER a valid CmsProfile object, OR a string of the
  769. filename of an ICC profile.
  770. :returns: A string containing the internal profile information stored in
  771. an ICC tag.
  772. :exception PyCMSError:
  773. """
  774. try:
  775. # add an extra newline to preserve pyCMS compatibility
  776. if not isinstance(profile, ImageCmsProfile):
  777. profile = ImageCmsProfile(profile)
  778. return (profile.profile.manufacturer or "") + "\n"
  779. except (AttributeError, OSError, TypeError, ValueError) as v:
  780. raise PyCMSError(v) from v
  781. def getProfileModel(profile: _CmsProfileCompatible) -> str:
  782. """
  783. (pyCMS) Gets the model for the given profile.
  784. If ``profile`` isn't a valid CmsProfile object or filename to a profile, a
  785. :exc:`PyCMSError` is raised.
  786. If an error occurs while trying to obtain the model tag,
  787. a :exc:`PyCMSError` is raised.
  788. Use this function to obtain the information stored in the profile's
  789. model tag.
  790. :param profile: EITHER a valid CmsProfile object, OR a string of the
  791. filename of an ICC profile.
  792. :returns: A string containing the internal profile information stored in
  793. an ICC tag.
  794. :exception PyCMSError:
  795. """
  796. try:
  797. # add an extra newline to preserve pyCMS compatibility
  798. if not isinstance(profile, ImageCmsProfile):
  799. profile = ImageCmsProfile(profile)
  800. return (profile.profile.model or "") + "\n"
  801. except (AttributeError, OSError, TypeError, ValueError) as v:
  802. raise PyCMSError(v) from v
  803. def getProfileDescription(profile: _CmsProfileCompatible) -> str:
  804. """
  805. (pyCMS) Gets the description for the given profile.
  806. If ``profile`` isn't a valid CmsProfile object or filename to a profile, a
  807. :exc:`PyCMSError` is raised.
  808. If an error occurs while trying to obtain the description tag,
  809. a :exc:`PyCMSError` is raised.
  810. Use this function to obtain the information stored in the profile's
  811. description tag.
  812. :param profile: EITHER a valid CmsProfile object, OR a string of the
  813. filename of an ICC profile.
  814. :returns: A string containing the internal profile information stored in an
  815. ICC tag.
  816. :exception PyCMSError:
  817. """
  818. try:
  819. # add an extra newline to preserve pyCMS compatibility
  820. if not isinstance(profile, ImageCmsProfile):
  821. profile = ImageCmsProfile(profile)
  822. return (profile.profile.profile_description or "") + "\n"
  823. except (AttributeError, OSError, TypeError, ValueError) as v:
  824. raise PyCMSError(v) from v
  825. def getDefaultIntent(profile: _CmsProfileCompatible) -> int:
  826. """
  827. (pyCMS) Gets the default intent name for the given profile.
  828. If ``profile`` isn't a valid CmsProfile object or filename to a profile, a
  829. :exc:`PyCMSError` is raised.
  830. If an error occurs while trying to obtain the default intent, a
  831. :exc:`PyCMSError` is raised.
  832. Use this function to determine the default (and usually best optimized)
  833. rendering intent for this profile. Most profiles support multiple
  834. rendering intents, but are intended mostly for one type of conversion.
  835. If you wish to use a different intent than returned, use
  836. ImageCms.isIntentSupported() to verify it will work first.
  837. :param profile: EITHER a valid CmsProfile object, OR a string of the
  838. filename of an ICC profile.
  839. :returns: Integer 0-3 specifying the default rendering intent for this
  840. profile.
  841. ImageCms.Intent.PERCEPTUAL = 0 (DEFAULT)
  842. ImageCms.Intent.RELATIVE_COLORIMETRIC = 1
  843. ImageCms.Intent.SATURATION = 2
  844. ImageCms.Intent.ABSOLUTE_COLORIMETRIC = 3
  845. see the pyCMS documentation for details on rendering intents and what
  846. they do.
  847. :exception PyCMSError:
  848. """
  849. try:
  850. if not isinstance(profile, ImageCmsProfile):
  851. profile = ImageCmsProfile(profile)
  852. return profile.profile.rendering_intent
  853. except (AttributeError, OSError, TypeError, ValueError) as v:
  854. raise PyCMSError(v) from v
  855. def isIntentSupported(
  856. profile: _CmsProfileCompatible, intent: Intent, direction: Direction
  857. ) -> Literal[-1, 1]:
  858. """
  859. (pyCMS) Checks if a given intent is supported.
  860. Use this function to verify that you can use your desired
  861. ``intent`` with ``profile``, and that ``profile`` can be used for the
  862. input/output/proof profile as you desire.
  863. Some profiles are created specifically for one "direction", can cannot
  864. be used for others. Some profiles can only be used for certain
  865. rendering intents, so it's best to either verify this before trying
  866. to create a transform with them (using this function), or catch the
  867. potential :exc:`PyCMSError` that will occur if they don't
  868. support the modes you select.
  869. :param profile: EITHER a valid CmsProfile object, OR a string of the
  870. filename of an ICC profile.
  871. :param intent: Integer (0-3) specifying the rendering intent you wish to
  872. use with this profile
  873. ImageCms.Intent.PERCEPTUAL = 0 (DEFAULT)
  874. ImageCms.Intent.RELATIVE_COLORIMETRIC = 1
  875. ImageCms.Intent.SATURATION = 2
  876. ImageCms.Intent.ABSOLUTE_COLORIMETRIC = 3
  877. see the pyCMS documentation for details on rendering intents and what
  878. they do.
  879. :param direction: Integer specifying if the profile is to be used for
  880. input, output, or proof
  881. INPUT = 0 (or use ImageCms.Direction.INPUT)
  882. OUTPUT = 1 (or use ImageCms.Direction.OUTPUT)
  883. PROOF = 2 (or use ImageCms.Direction.PROOF)
  884. :returns: 1 if the intent/direction are supported, -1 if they are not.
  885. :exception PyCMSError:
  886. """
  887. try:
  888. if not isinstance(profile, ImageCmsProfile):
  889. profile = ImageCmsProfile(profile)
  890. # FIXME: I get different results for the same data w. different
  891. # compilers. Bug in LittleCMS or in the binding?
  892. if profile.profile.is_intent_supported(intent, direction):
  893. return 1
  894. else:
  895. return -1
  896. except (AttributeError, OSError, TypeError, ValueError) as v:
  897. raise PyCMSError(v) from v
  898. def versions() -> tuple[str, str | None, str, str]:
  899. """
  900. (pyCMS) Fetches versions.
  901. """
  902. deprecate(
  903. "PIL.ImageCms.versions()",
  904. 12,
  905. '(PIL.features.version("littlecms2"), sys.version, PIL.__version__)',
  906. )
  907. return _VERSION, core.littlecms_version, sys.version.split()[0], __version__