-
Notifications
You must be signed in to change notification settings - Fork 0
/
picmerj.py
385 lines (298 loc) · 10.3 KB
/
picmerj.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env python
#
# picmerj.py
#
# combine 2 input images into a 3rd --> the output image
#
# TO DO:
# 1. resize image to larger of pair
# 2. green screen
# 3. provide offset for smaller image within larger
# 4. masking
#
# 0. go see Joanna & the Wrecking Crew https://www.youtube.com/watch?v=iSwoM7WIy5M
#
import os
from PIL import Image
from PIL import ImageFilter
import sys
import random
import cv2
white = (255,255,255)
black = (0,0,0)
max_red = (255,0,0)
max_green = (0,255,0)
max_blue = (0,0,255)
cyan = (0,255,255)
magenta = (255,0,255)
yellow = (255,255,0)
def viewImage(image, name_of_window):
cv2.namedWindow(name_of_window, cv2.WINDOW_NORMAL)
# 2DO: preserve aspect ratio
cv2.resizeWindow(name_of_window, 403*3, 302*3)
cv2.moveWindow(name_of_window, 100, 50)
cv2.imshow(name_of_window, image)
cv2.waitKey(0)
cv2.destroyAllWindows()
def color(x,y):
red = (x*x+y*y)%255
green = (y*y*2+2*x*x)%255
blue = (y*y*3+3*x*x)%255
return (red, green, blue)
def noise_or_color(col, noise):
"""higher value for noise (a float in [0..1]) means more noise"""
if random.random() < noise:
red = (col[0] + random.randrange(-50,50)) % 256
green = (col[1] + random.randrange(-50,50)) % 256
blue = (col[2] + random.randrange(-50,50)) % 256
return (red,green,blue)
else:
return col
def clamp(val,min,max):
if (val < min): return min
if (val > max): return max
return val
def noisy_color(col, noise, amount):
"""higher value for noise (a float in [0..1]) means more noise"""
if random.random() < noise:
red = (col[0] + random.randrange(-amount,amount))
green = (col[1] + random.randrange(-amount,amount))
blue = (col[2] + random.randrange(-amount,amount))
red = clamp(red,0,255)
green = clamp(green,0,255)
blue = clamp(blue,0,255)
return (red,green,blue)
else:
return col
def sum_channels(pixel):
return pixel[0] + pixel[1] + pixel[2]
def red_channel(pixel):
return pixel[0]
def green_channel(pixel):
return pixel[1]
def blue_channel(pixel):
return pixel[2]
def euclidean_dist(px,px2):
return (px[0] - px2[0])**2 + (px[1] - px2[1])**2 + (px[2] - px2[2])**2
def dist_to_target(px):
return euclidean_dist(px, (target_red, target_green, target_blue))
def average(px1, px2):
return (int((px1[0] + px2[0])/2), int((px1[1] + px2[1])/2), int((px1[2] + px2[2])/2))
def random_source(px1, px2):
if (random.random() < 0.5):
return px1
return px2
def brighter(px1, px2):
if (sum_channels(px1) > sum_channels(px2)):
return px1
return px2
def darker(px1, px2):
if (sum_channels(px1) < sum_channels(px2)):
return px1
return px2
def redder(px1, px2):
if (red_channel(px1) > red_channel(px2)):
return px1
return px2
def greener(px1, px2):
if (green_channel(px1) > green_channel(px2)):
return px1
return px2
def bluer(px1, px2):
if (blue_channel(px1) > blue_channel(px2)):
return px1
return px2
def less_red(px1, px2):
if (red_channel(px1) < red_channel(px2)):
return px1
return px2
def less_green(px1, px2):
if (green_channel(px1) < green_channel(px2)):
return px1
return px2
def less_blue(px1, px2):
if (blue_channel(px1) < blue_channel(px2)):
return px1
return px2
def redder2(px1, px2):
if (red_channel(px1) - (blue_channel(px1) + green_channel(px1)) > red_channel(px2) - (blue_channel(px2) + green_channel(px2))):
return px1
return px2
def greener2(px1, px2):
if (green_channel(px1) - (red_channel(px1) + blue_channel(px1)) > green_channel(px2) - (red_channel(px2) + blue_channel(px2))):
return px1
return px2
def bluer2(px1, px2):
if (blue_channel(px1) - (red_channel(px1) + green_channel(px1)) > blue_channel(px2) - (red_channel(px2) + green_channel(px2))):
return px1
return px2
def closer(px1, px2):
if (dist_to_target(px1) < dist_to_target(px2)):
return px1
return px2
def further(px1, px2):
if (dist_to_target(px1) > dist_to_target(px2)):
return px1
return px2
def below(px1, px2):
if (red_channel(px1) < target_red and green_channel(px1) < target_green and blue_channel(px1) < target_blue):
return px2
return px1
def above(px1, px2):
if (red_channel(px1) > target_red and green_channel(px1) > target_green and blue_channel(px1) > target_blue):
return px2
return px1
def bluer_than_threshold(px1, px2):
if (bluer(px2, (target_red, target_green, target_blue)) == px2):
return px2
return px1
def p1_if_p2_dark(px1, px2):
if (sum_channels(px2) < darkness_threshold):
return px1
return px2
def combine(im1, im2, width, height, combination, out_img):
for i in range(width):
for j in range(height):
out_img[i,j] = combination(im1[i,j], im2[i,j])
def checkerboard(im1, im2, width, height, out_img):
for i in range(width):
if (i % 2) == 0:
for j in range(height):
if (j % 2) == 0:
out_img[i,j] = im1[i,j]
else:
out_img[i,j] = im2[i,j]
else:
for j in range(height):
if (j % 2) == 1:
out_img[i,j] = im1[i,j]
else:
out_img[i,j] = im2[i,j]
def chessboard(im1, im2, width, height, s1, s2, out_img):
for i in range(width):
if (i % s1) < s2:
for j in range(height):
if (j % s1) < s2:
out_img[i,j] = im1[i,j]
else:
out_img[i,j] = im2[i,j]
else:
for j in range(height):
if (j % s1) >= s2:
out_img[i,j] = im1[i,j]
else:
out_img[i,j] = im2[i,j]
# blue channel from p1 other channels from p2
def left_blue(px1, px2):
return red_channel(px2), green_channel(px2), blue_channel(px1)
def printops(ops):
for o in sorted(ops):
print(o, ops[o])
if __name__ == '__main__':
try:
print( 'trying to open ' + sys.argv[1] )
img = Image.open(sys.argv[1]).convert('RGB')
print( 'trying to open ' + sys.argv[2] )
img2 = Image.open(sys.argv[2]).convert('RGB')
except:
print( 'failed to open input file!' )
img_size = (1000, 1000)
img = Image.new('RGB', img_size, white)
try:
img2 = Image.open('images/seven11.png').convert('RGB')
except:
exit()
# 2DO: preserve aspect ratio in case of resize
(x_pixels, y_pixels) = img.size
(width, height) = img2.size
# try to prevent out-of-range errors
# by using the smaller dimensions
if (img.size[0] < img2.size[0]):
width = x_pixels
height = y_pixels
print('shrinking img2')
img3 = img2.resize( (width, height) )
img3.save('test.jpg')
img2 = img3
else:
print('shrinking img')
img3 = img.resize((width, height))
img3.save('test.jpg')
img = img3
img_pix = img.load()
img2_pix = img2.load()
new_image = Image.new(img.mode, (width, height))
print( 'output image will be', width, 'by', height )
print( 'image1 is', img.size[0], 'by', img.size[1] )
print( 'image2 is', img2.size[0], 'by', img2.size[1] )
target_red = darkness_threshold = 11
target_green = 111
target_blue = 255
try:
target_red = darkness_threshold = int(sys.argv[4])
target_green = int(sys.argv[5])
target_blue = int(sys.argv[6])
except:
print('target exception')
print( target_red, target_green, target_blue )
try:
combiner_code = sys.argv[3]
except:
print('combiner code exception -- no argv[3] provided?')
combiner_code = '?'
combiners = {}
combiners['a'] = average
combiners['b'] = bluer
combiners['b2'] = bluer2
combiners['bt'] = bluer_than_threshold
combiners['c'] = closer
combiners['cb'] = checkerboard
combiners['d'] = darker
combiners['d2'] = p1_if_p2_dark # else p2, presumably
combiners['f'] = further
combiners['g'] = greener
combiners['g2'] = greener2
combiners['l'] = brighter
combiners['lb'] = less_blue
combiners['lg'] = less_green
combiners['lr'] = less_red
combiners['r'] = redder
combiners['r2'] = redder2
combiners['ra'] = above
combiners['rb'] = below
combiners['rs'] = random_source
combiners['xb'] = chessboard
combiners['b1'] = left_blue
if combiner_code in combiners:
combiner = combiners[ combiner_code ]
else:
print( 'combiner_code "', combiner_code, '" not recognized, defaulting to "l" (lighter/brighter)' )
printops( combiners )
combiner = brighter # always [default to] the bright side of life [whistling]
print( 'combiner_code is [', combiner_code, ']')
print( 'combiner is [', combiner, ']')
if combiner_code == 'cb':
combiner( img_pix, img2_pix, width, height, new_image.load() )
elif combiner_code == 'xb':
# 2DO: better names than target_xxxx
combiner( img_pix, img2_pix, width, height, target_red, target_green, new_image.load() )
else:
print('e.g.:', combiner(img_pix[0,0], img2_pix[0,0]), '<==combiner(', img_pix[0,0], ',', img2_pix[0,0], ')')
combine( img_pix, img2_pix, width, height, combiner, new_image.load() )
newfile = ''
try:
left_part = sys.argv[1].split('/')[-1]
left_part = left_part[:-4]
if left_part[1] == ':': left_part = left_part[2:]
right_part = sys.argv[2].split('/')[-1]
right_part = right_part[:-4]
if right_part[1] == ':': right_part = right_part[2:]
newfile = 'newimages/' + left_part + '-' + right_part + '-' + combiner_code + '.jpg'
print( 'writing to ' + newfile )
new_image.save(newfile)
except:
print( 'writing to default.jpg' )
new_image.save('default.jpg')
if newfile != '' and 'ix' not in os.name:
image = cv2.imread(newfile)
viewImage(image, newfile)