This repository has been archived by the owner on May 7, 2024. It is now read-only.
/
srgb.py
61 lines (48 loc) · 1.79 KB
/
srgb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from logging import warning
from PIL import Image, ImageCms
from tempfile import NamedTemporaryFile
import os
from PIL.ImageCms import PyCMSError
__location__ = os.path.realpath(
os.path.join(os.getcwd(), os.path.dirname(__file__)))
# Pillow needs to be built with Color Management
# Install lcms with e.g.
# $ sudo apt-get install liblcms2-dev
# then
# pip install pillow
# TODO: fail gracefully if lcms isn't available
with open(os.path.join(__location__, 'srgb.icc')) as srgb:
SRGB_BYTES = srgb.read()
try:
SRGB_PROFILE = ImageCms.createProfile("sRGB")
except ImportError: # probably
warning("Couldn't create sRGB profile. Try compiling Pillow with little-cms support.")
SRGB_PROFILE = None
def convert_to_srgb(infile, outfile):
"""
Process an image file of unknown color profile.
Transform to sRGB profile, convert the color space to RGB, write the image
and return the path to the result.
:param infile: the path to a file with arbitrary colorspace and color profile
:param outfile: the destination path to write to
:return: None
"""
# TODO: issue a warning and just copy the file if lcms isn't available.
if not SRGB_PROFILE:
return infile # no-op
img = Image.open(infile)
# get its color profile, write to a temp file
# icc_file, icc_path = tempfile.mkstemp(suffix='.icc')
icc_file = NamedTemporaryFile(suffix='.icc', delete=True)
# with open(icc_path, 'w') as f:
profile = img.info.get('icc_profile')
if profile:
icc_file.write(profile)
icc_file.flush()
try:
img = ImageCms.profileToProfile(img, icc_file.name, SRGB_PROFILE, outputMode="RGB")
except PyCMSError:
pass # no-op
img.save(outfile, icc_profile=SRGB_BYTES)
icc_file.close()
return outfile