/
imgycc.py
217 lines (160 loc) · 6.38 KB
/
imgycc.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env python3
# Convert image to YCC.
# davep Sep? Oct? 2011
#
# Update to Python3
# davep 11-Apr-2014
import sys
import os
import math
import PIL.Image as Image
import numpy as np
rgb_to_ycc = np.array(
(0.2990, 0.5870, 0.1140,
-0.1687, -0.3313, 0.5000,
0.5000, -0.4187, -0.0813,)
).reshape( 3,3 )
ycc_to_rgb = np.array(
( 1.0, 0.0, 1.4022,
1.0, -0.3456, -0.7145,
1.0, 1.7710, 0, )
).reshape( 3, 3 )
ycc_to_bgr = np.array(
( 1.0, 1.7710, 0,
1.0, -0.3456, -0.7145,
1.0, 0.0, 1.4022,)
).reshape( 3, 3 )
def floatstr( floatnum ) :
# the [:4] takes at most 4 chars of the float
# change the '.' in floats to '_' to make filename easier
return str(floatnum)[:4].replace(".","_")
def contrast_enhance( ycc, contrast=1.4 ):
ycc_enhanced = np.copy( ycc )
ycc_enhanced[:,:,1] *= contrast
ycc_enhanced[:,:,2] *= contrast
return ycc_enhanced
def brightness_enhance( ycc, brightness=20 ) :
ycc_enhanced = np.copy( ycc )
ycc_enhanced[:,:,0] += brightness
return ycc_enhanced
def gamma_enhance( ycc, gamma=2.2) :
# precalc a constant
gamma_exp = 1.0/gamma
ycc_enhanced = np.copy( ycc )
# only Gamma the Y channel
ycc_enhanced[:,:,0] = 255.0 * np.power( ycc_enhanced[:,:,0] / 255.0, gamma_exp )
return ycc_enhanced
def convert_ycc_to_rgb( ycc ) :
return np.dot( ycc, ycc_to_rgb.T )
def convert_ycc_to_bgr( ycc ) :
return np.dot( ycc, ycc_to_bgr.T )
def convert_rgb_to_ycc( rgb ) :
return np.dot( rgb, rgb_to_ycc.T )
def save_ycc_as_image( ycc, basename ) :
# move Cb,Cr planes from [-128,127] to [0,255]
img_ycc = np.copy(ycc)
img_ycc[:,:,1] += 128
img_ycc[:,:,2] += 128
img_ycc_uint8 = np.asarray( np.clip( img_ycc, 0, 255 ), dtype="uint8" )
outimg = Image.fromarray( img_ycc_uint8[:,:,0], "L" )
outimg.save( "{0}_y.tif".format(basename) )
outimg = Image.fromarray( img_ycc_uint8[:,:,1], "L" )
outimg.save( "{0}_cb.tif".format(basename) )
outimg = Image.fromarray( img_ycc_uint8[:,:,2], "L" )
outimg.save( "{0}_cr.tif".format(basename) )
outimg = Image.fromarray( img_ycc_uint8, "RGB" )
outimg.save( "{0}_ycc.tif".format(basename) )
def clip_and_save( rgb, outfilename ) :
rgb_uint8 = np.asarray( np.clip( rgb, 0, 255 ), dtype="uint8" )
outimg = Image.fromarray( rgb_uint8, "RGB" )
outimg.save( outfilename )
#def image_enhance( ycc, basename, contrast=1.4, brightness=20 ) :
# rgb_enhanced = convert_ycc_to_rgb( ycc_contrast_enhance( ycc, contrast ) )
# rgb_uint8 = np.asarray( np.clip( rgb_enhanced, 0, 255 ), dtype="uint8" )
# outimg = Image.fromarray( rgb_uint8, "RGB" )
#
# # the [:4] takes at most 4 chars of the float
# # change the '.' in floats to '_' to make filename easier
# contrast_str = str(contrast)[:4].replace(".","_")
# brightness_str = str(brightness)[:4].replace(".","_")
#
# outimg.save( "{0}_enhanced_{1}.tif".format(basename,contrast_str) )
#
# rgb_brightened = convert_ycc_to_rgb( ycc_brightness_enhance( ycc, brightness ) )
# rgb_uint8 = np.asarray( np.clip( rgb_brightened, 0, 255 ), dtype="uint8" )
# outimg = Image.fromarray( rgb_uint8, "RGB" )
# outimg.save( "{0}_brightened_{1}.tif".format(basename,brightness_str) )
def run_brightness( ycc, basename, brightness=20 ) :
rgb_brightened = convert_ycc_to_rgb( ycc_brightness_enhance( ycc, brightness ) )
# the [:4] takes at most 4 chars of the float
# change the '.' in floats to '_' to make filename easier
brightness_str = str(brightness)[:4].replace(".","_")
outfilename = "{0}_brightened_{1}.tif".format(basename,brightness_str)
clip_and_save( rgb_brightened, outfilename )
def run_contrast( ycc, basename, contrast=1.4 ) :
rgb_enhanced = convert_ycc_to_rgb( ycc_contrast_enhance( ycc, contrast ) )
outfilename = "{0}_enhanced_{1}.tif".format(basename, floatstr( contrast ))
clip_and_save( rgb_enhanced, outfilename )
def gamma_lambda( gamma, maxvalue=255 ) :
# precalc a constant
gamma_exp = 1.0/gamma
# make sure y/maxvalue is a float
maxf = float(maxvalue)
return lambda y : maxf * math.pow( y/maxf, gamma_exp )
def old_enhance_gamma( ycc, basename, gamma=2.2 ) :
# # precalc a constant
# gamma_exp = 1.0/gamma
# g = lambda y : 255 * math.pow( y/255.0, gamma_exp )
g = gamma_lambda( gamma )
npg = np.frompyfunc( g, 1, 1 )
ycc_enhanced = np.copy(ycc)
y_gamma = npg( ycc[:,:,0] )
ycc_enhanced[:,:,0] = y_gamma
return ycc_enhanced
def run_gamma( ycc, basename, gamma=2.2 ) :
rgb_enhanced = convert_ycc_to_rgb( gamma_enhance( ycc, gamma ) )
outfilename = "{0}_gamma_{1}.tif".format(basename,floatstr(gamma))
clip_and_save( rgb_enhanced, outfilename )
def convert_image_to_ycc( infilename ) :
basename = os.path.splitext( os.path.split( infilename )[1] )[0]
img = Image.open( infilename )
img.load()
if img.mode != "RGB" :
print(( "{0} is not an RGB image.".format( infilename ) ))
sys.exit(1)
rgb = np.asarray( img, dtype="float" )
# convert RGB -> YCC
# ycc = np.zeros_like( rgb )
# for row in range(rgb.shape[0]):
# ycc[row] = rgb[row] * rgb_to_ycc.T
ycc = convert_rgb_to_ycc( rgb )
np.save( basename+"_ycc", ycc )
save_ycc_as_image( ycc, basename )
return ycc
def main( infilename ) :
basename = os.path.splitext( os.path.split( infilename )[1] )[0]
# try to load previous file for speed
try :
ycc = np.load( basename+"_ycc.npy" )
except IOError as err :
# convert image to ycc
ycc = convert_image_to_ycc( infilename )
clip_and_save( convert_ycc_to_bgr(ycc),"out.tif")
# davep 09-Jan-2013 ; XXX stop after the conversion
return
gamma = 2.2
run_gamma( ycc, basename, gamma )
ycc_gamma = gamma_enhance( ycc, gamma )
ycc_contrast = contrast_enhance( ycc_gamma, 1.4 )
outfilename = "{0}_gamma_contrast.tif".format( basename )
clip_and_save( convert_ycc_to_rgb( ycc_contrast ), outfilename )
# run_contrast( ycc, basename, 2.2 )
# for contrast in np.linspace( 0, 2, num=30 ) :
# run_contrast( ycc, basename, contrast )
for b in np.linspace(-1,1) :
contrast = 1.0
brightness = int(b*127)
# run_enhance( ycc, basename, contrast, brightness )
if __name__== '__main__' :
infilename = sys.argv[1]
main( infilename )