-
Notifications
You must be signed in to change notification settings - Fork 25
/
hmap_c.py
286 lines (233 loc) · 8.05 KB
/
hmap_c.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# image histogram remapping
from __future__ import print_function
from sys import argv
from PIL import Image
import collections
import random
script, source_image, target_image = argv
#load our source and target images
srcImg = Image.open(source_image)
tgtImg = Image.open(target_image)
srcImg.show()
tgtImg.show()
#image data
width, height = srcImg.size
#load pixel maps
srcPix = srcImg.load()
tgtPix = tgtImg.load()
#Get histograms of the images
#Now taking 3 different histograms for each image; 1 for each color channel
srcHist_R = srcImg.histogram()[:256]
tgtHist_R = tgtImg.histogram()[:256]
srcHist_G = srcImg.histogram()[256:512]
tgtHist_G = tgtImg.histogram()[256:512]
srcHist_B = srcImg.histogram()[512:]
tgtHist_B = tgtImg.histogram()[512:]
#make value lists
pxlsByVal_R = [set() for _ in range(256)]
for i in range(width):
for j in range(height):
value = srcPix[i, j][0]
pxlsByVal_R[value].add((i,j))
pxlsByVal_G = [set() for _ in range(256)]
for i in range(width):
for j in range(height):
value = srcPix[i, j][1]
pxlsByVal_G[value].add((i,j))
pxlsByVal_B = [set() for _ in range(256)]
for i in range(width):
for j in range(height):
value = srcPix[i, j][2]
pxlsByVal_B[value].add((i,j))
print("pxlsByVal created...")
equalBins_R = collections.deque()
excessBins_R = collections.deque()
deficitBins_R = collections.deque()
equalBins_G = collections.deque()
excessBins_G = collections.deque()
deficitBins_G = collections.deque()
equalBins_B = collections.deque()
excessBins_B = collections.deque()
deficitBins_B = collections.deque()
#sort bins into lists
for i, _ in enumerate(srcHist_R):
src_i = srcHist_R[i]
tgt_i = tgtHist_R[i]
if src_i < tgt_i:
deficitBins_R.append(i)
elif src_i > tgt_i:
excessBins_R.append(i)
else:
equalBins_R.append(i)
for i, _ in enumerate(srcHist_G):
src_i = srcHist_G[i]
tgt_i = tgtHist_G[i]
if src_i < tgt_i:
deficitBins_G.append(i)
elif src_i > tgt_i:
excessBins_G.append(i)
else:
equalBins_G.append(i)
for i, _ in enumerate(srcHist_B):
src_i = srcHist_B[i]
tgt_i = tgtHist_B[i]
if src_i < tgt_i:
deficitBins_B.append(i)
elif src_i > tgt_i:
excessBins_B.append(i)
else:
equalBins_B.append(i)
print("For R\n#equal bins: %s\t#excess bins: %s\t#deficit bins: %s" % tuple(
map(len, (equalBins_R, excessBins_R, deficitBins_R))))
print("For G\n#equal bins: %s\t#excess bins: %s\t#deficit bins: %s" % tuple(
map(len, (equalBins_G, excessBins_G, deficitBins_G))))
print("For B\n#equal bins: %s\t#excess bins: %s\t#deficit bins: %s" % tuple(
map(len, (equalBins_B, excessBins_B, deficitBins_B))))
#change one pixel function - R
def change_n_pixels_R(curVal, tgtVal, nToChange):
#find a pixel to change
candidatePxls = pxlsByVal_R[curVal]
chosenPxls = random.sample(candidatePxls, nToChange)
#change the pixel
for pxl in chosenPxls:
srcPix[pxl] = (tgtVal, srcPix[pxl][1], srcPix[pxl][2])
#update pixel list
pxlsByVal_R[curVal].remove(pxl)
pxlsByVal_R[tgtVal].add(pxl)
#update the histograms
srcHist_R[curVal] -= nToChange
srcHist_R[tgtVal] += nToChange
#change one pixel function - G
def change_n_pixels_G(curVal, tgtVal, nToChange):
#find a pixel to change
candidatePxls = pxlsByVal_G[curVal]
chosenPxls = random.sample(candidatePxls, nToChange)
#change the pixel
for pxl in chosenPxls:
srcPix[pxl] = (srcPix[pxl][0], tgtVal, srcPix[pxl][2])
#update pixel list
pxlsByVal_G[curVal].remove(pxl)
pxlsByVal_G[tgtVal].add(pxl)
#update the histograms
srcHist_G[curVal] -= nToChange
srcHist_G[tgtVal] += nToChange
#change one pixel function - B
def change_n_pixels_B(curVal, tgtVal, nToChange):
#find a pixel to change
candidatePxls = pxlsByVal_B[curVal]
chosenPxls = random.sample(candidatePxls, nToChange)
#change the pixel
for pxl in chosenPxls:
srcPix[pxl] = (srcPix[pxl][0], srcPix[pxl][1], tgtVal)
#update pixel list
pxlsByVal_B[curVal].remove(pxl)
pxlsByVal_B[tgtVal].add(pxl)
#update the histograms
srcHist_B[curVal] -= nToChange
srcHist_B[tgtVal] += nToChange
#change one pixel function - R
def change_n_pixels_smooth_R(curVal, tgtVal, nToChange):
Nincrements = abs(curVal - tgtVal)
for inc in range(Nincrements):
# This part was throwing IndexErrors, so I adjusted it
if tgtVal > curVal:
try:
change_n_pixels_R(curVal+inc, curVal+inc+1, nToChange)
except IndexError:
pass
else:
try:
change_n_pixels_R(curVal-inc, curVal-inc-1, nToChange)
except IndexError:
pass
#change one pixel function - G
def change_n_pixels_smooth_G(curVal, tgtVal, nToChange):
Nincrements = abs(curVal - tgtVal)
for inc in range(Nincrements):
# This part was throwing IndexErrors, so I adjusted it
if tgtVal > curVal:
try:
change_n_pixels_G(curVal+inc, curVal+inc+1, nToChange)
except IndexError:
pass
else:
try:
change_n_pixels_G(curVal-inc, curVal-inc-1, nToChange)
except IndexError:
pass
#change one pixel function - B
def change_n_pixels_smooth_B(curVal, tgtVal, nToChange):
Nincrements = abs(curVal - tgtVal)
for inc in range(Nincrements):
# This part was throwing IndexErrors, so I adjusted it
if tgtVal > curVal:
try:
change_n_pixels_B(curVal+inc, curVal+inc+1, nToChange)
except IndexError:
pass
else:
try:
change_n_pixels_B(curVal-inc, curVal-inc-1, nToChange)
except IndexError:
pass
#move pixels in excess bins to deficit bins - R
for curValue in excessBins_R:
excess = srcHist_R[curValue] - tgtHist_R[curValue]
if curValue % 5 == 0:
print("On R value", curValue, "with", excess, "excess pixels")
while excess > 0:
if deficitBins_R == collections.deque([]):
break
else:
tgtValue = deficitBins_R[0]
deficit = tgtHist_R[tgtValue] - srcHist_R[tgtValue]
if excess > deficit :
nToMove = excess - deficit
deficitBins_R.popleft()
else:
nToMove = excess
if deficit == excess:
deficitBins_R.popleft()
change_n_pixels_smooth_R(curValue,tgtValue,nToMove)
excess -= nToMove
#move pixels in excess bins to deficit bins - G
for curValue in excessBins_G:
excess = srcHist_G[curValue] - tgtHist_G[curValue]
if curValue % 5 == 0:
print("On G value", curValue, "with", excess, "excess pixels")
while excess > 0:
if deficitBins_G == collections.deque([]):
break
else:
tgtValue = deficitBins_G[0]
deficit = tgtHist_G[tgtValue] - srcHist_G[tgtValue]
if excess > deficit :
nToMove = excess - deficit
deficitBins_G.popleft()
else:
nToMove = excess
if deficit == excess:
deficitBins_G.popleft()
change_n_pixels_smooth_G(curValue,tgtValue,nToMove)
excess -= nToMove
#move pixels in excess bins to deficit bins - B
for curValue in excessBins_B:
excess = srcHist_B[curValue] - tgtHist_B[curValue]
if curValue % 5 == 0:
print("On B value", curValue, "with", excess, "excess pixels")
while excess > 0:
if deficitBins_B == collections.deque([]):
break
else:
tgtValue = deficitBins_B[0]
deficit = tgtHist_B[tgtValue] - srcHist_B[tgtValue]
if excess > deficit :
nToMove = excess - deficit
deficitBins_B.popleft()
else:
nToMove = excess
if deficit == excess:
deficitBins_B.popleft()
change_n_pixels_smooth_B(curValue,tgtValue,nToMove)
excess -= nToMove
srcImg.show()