-
Notifications
You must be signed in to change notification settings - Fork 1
/
ColorChannelInst.py
95 lines (79 loc) · 3.22 KB
/
ColorChannelInst.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import PIL
from PIL import Image
WPD65 = 'D65'
WPD50 = 'D50'
WORKSPACE_CIEXYZ = 'CIEXYZ'
WORKSPACE_sRGB = 'sRGB'
WORKSPACE_AdobeRGB = 'AdobeRGB'
WORKSPACE_ProPhotoRGB = 'ProPhotoRGB'
def displayCCInst(ccInst):
img = Image.new('RGB', ccInst.size)
img.putdata(ccInst.data)
img.show()
def loadImage(path):
img = Image.open(path)
print "-" * 20
print "Image Information : Format(%s), Size(%s), Mode(%s)\n"\
%(img.format, img.size, img.mode)
print "-" * 20
return img
def createCCInstTemp(fPath):
return createColorChannelInstance(fPath=fPath)
def createColorChannelInstance(fPath=None, imgData=None, package={}):
assert not (fPath == None and imgData == None and package == {})
ccInst = None
if fPath and not imgData and not package:
from EXIF_ColorProfileParser import get_metadata_by_exiftool,\
getWPByWPMatrix, LIST_sRGB_D65_WHITE_POINT
img = loadImage(fPath)
meta = get_metadata_by_exiftool(fPath)
ws = meta.get('Color Space', None)
if meta.get('Color Space', None) == 0:
ws = WORKSPACE_sRGB
elif meta.get('Color Space', None) == "Uncalibrated":
pDesc = meta.get('Profile Description', None)
iccProfileName = meta.get('ICC Profile Name', None)
if pDesc.find('Adobe RGB') >=0 or iccProfileName.find('Adobe RGB') >= 0:
ws = WORKSPACE_AdobeRGB
strMWP = meta.get('Media White Point',\
' '.join(str(item) for item in LIST_sRGB_D65_WHITE_POINT))
mwp = getWPByWPMatrix(strMWP)
ccInst = ColorChannelInstance(img.size[0], img.size[1], img, ws=ws, wp=mwp)
elif imgData and not fPath and not package:
ccInst = ColorChannelInstance(imgData.size[0], imgData.size[1], imgData)
elif package and not imgData and not fPath:
w = package.get('w', 0)
h = package.get('h', 0)
comp = package.get('comp', 'RGB')
wp = package.get('wp', WPD65)
ws = package.get('ws', WORKSPACE_sRGB)
ccInst = ColorChannelInstance(w, h, None, comp=comp, ws=ws, wp=wp)
pass
assert ccInst, "ColorChannelInstance can't be created."
return ccInst
class ColorChannelInstance:
def __init__(self, width, height, img=None, comp='RGB', ws='sRGB', wp=WPD65):
self.data = []
if img and img.__class__.__name__ in ["JpegImageFile", "PngImageFile"]:
assert ((width, height)==img.size)
self.data = list(img.getdata())
self.__width = width
self.__height = height
self.__whitePoint = wp
self.size = self.__width, self.__height
self.mode = comp
self.__workSpace = ws
if len(self.data)==0 and not img:
self.data = [(0,0,0)]*width*height
def getWhitePoint(self):
return self.__whitePoint
def getWorkSpace(self):
return self.__workSpace
def __getitem__(self, tupIndex):
assert (type(tupIndex)==tuple and len(tupIndex)==2)
x, y = tupIndex
return self.data[y*self.__width+x]
def __setitem__(self, tupIndex, value):
assert (type(tupIndex)==tuple and len(tupIndex)==2)
x, y = tupIndex
self.data[y*self.__width+x] = value