-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimroute.py
More file actions
executable file
·616 lines (476 loc) · 18.9 KB
/
animroute.py
File metadata and controls
executable file
·616 lines (476 loc) · 18.9 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#!/usr/bin/python
# by Roman Hoog Antink, (c) 2012
#
# Animated route generator
#
# constructs each frame using python PIL (image library) and merges them into
# an AVI file using mencoder
import os, sys
import re
import shlex
from time import time
from shutil import rmtree
import Image, ImageDraw
from math import pi,asin
from geometry import *
# populate global config settings in dictionary params and
# return stack of animation operations
def parse_config(config):
global params
token_list = shlex.split(config, True)
operator_stack = list()
while len(token_list) > 0:
token = token_list.pop(0)
if token == "set":
name = token_list.pop(0)
value = token_list.pop(0)
params[name] = convert_varlist(value)
#print("global conf " + name + "=" + str(params[name]))
elif token == "anim":
duration = convert_timespec(token_list.pop(0))
operator = token_list.pop(0)
argc = peek_args(token_list)
if argc > 0:
args = token_list[:argc]
del token_list[:argc]
args = convert_nested(args)
else:
args = list()
operator_stack.append((operator, duration, args))
#print("anim_op_" + operator + " [" + str(duration) + "] " + str(args))
else:
abort("unknown config instruction " + token)
return operator_stack
# count how many tokens until the next instruction (identified by 'anim' or end of file)
def peek_args(token_list):
count = 0
for t in token_list:
if t == 'anim':
return count
count+=1
return count
# convert '[12.5]' into float
def convert_timespec(t):
re_timespec = re.compile('^\[-?[0-9.]+\]$');
if not re_timespec.match(t):
abort("invalid timespec: " + t)
return float(t[1:-1])
# convert '(3,-4,5.9)' into list of floats
def convert_varlist(string_value):
re_tuple = re.compile('^\(-?[0-9.]+(,-?[0-9.]+)+\)$')
re_int = re.compile('^-?[0-9]+$')
re_float = re.compile('^-?[0-9.]+$')
if re_tuple.match(string_value):
values = string_value[1:-1].split(',')
values = map(lambda v: int(v), values)
return values
elif re_int.match(string_value):
return int(string_value)
elif re_float.match(string_value):
return float(string_value)
return string_value
# convert ('(2,3)', '4,5') into nested list ((2,3),(4,5))
def convert_nested(arg_list):
arg_list = map(lambda element: convert_varlist(element), arg_list)
return arg_list
# fatal error occured
def abort(msg):
print(msg)
sys.exit(1)
# copy frame file by symlinking
# args: source_frame_index, target_frame_index
def copy_frame(orig_i, target_i):
global params
global last_frame_no
if last_frame_no+1 != target_i:
abort("copy_frame: invalid frame_no %d, expected %d" % (target_i, last_frame_no+1))
frame1_filename = '%s/frame_%06d.png' % (params['tmpdir'], orig_i)
frame1_symlink = 'frame_%06d.png' % (orig_i)
frame2_filename = '%s/frame_%06d.png' % (params['tmpdir'], target_i)
# try creating a symlink
if not os.path.exists(frame1_filename):
abort("copy_frame: file not found '%s', last_frame_no=%d, new_frame_no=%d" % (frame1_filename, last_frame_no, target_i))
if os.path.exists(frame2_filename):
abort("copy_frame: file exists '" + frame2_filename + "'")
last_frame_no = target_i
try:
os.symlink(frame1_symlink, frame2_filename)
except:
print("copy_frame: os.symlink() failed. Trying to copy.")
shutil.copy(frame1_filename, frame2_filename)
# write out a single frame, scaling it down
def write_frame(frame_no, image):
global params
global last_frame_no
frame_filename = '%s/frame_%06d.png' % (params['tmpdir'], frame_no)
if last_frame_no+1 != frame_no:
abort("write_frame: invalid frame_no %d, expected %d" % (frame_no, last_frame_no+1))
if os.path.exists(frame_filename):
abort("write_frame: duplicate frame written: " % (frame_no))
last_frame_no = frame_no
scaled_copy = image.copy()
# thumbnail() maintains the aspect ratio. See also http://stackoverflow.com/questions/273946/how-do-i-resize-an-image-using-pil-and-maintain-its-aspect-ratio
scaled_copy.thumbnail(params['resolution'], Image.ANTIALIAS)
scaled_copy.save(frame_filename)
# print out a line indicating overall and local progress
def progress_update(local_frame_no, local_frame_sum, name):
global frame_no
global frame_sum
global start_time
now = time()
total_frames_done = frame_no + local_frame_no
total_progress = float(total_frames_done) / frame_sum * 100
local_progress = float(local_frame_no) / local_frame_sum * 100
remaining = (now-start_time) / total_frames_done * float(frame_sum-total_frames_done)
if remaining >= 60:
remaining = '%d:%02d' % (remaining//60, remaining % 60)
else:
remaining = '%ds' % (remaining)
print('Progress: %.1f%%, Current task: %.1f%% %s, Remaining: %s' % (total_progress, local_progress, name, remaining))
# anim operation pause
# keeps the image still for a while
def anim_op_pause(duration):
global frame
global frame_no
global params
frame_count = int(duration * params['fps'])
for i in range(frame_no+1, frame_no+frame_count+1):
copy_frame(frame_no, i)
if i % (frame_count/20+1) == 0:
progress_update(i-frame_no, frame_count, 'pause')
frame_no += frame_count
# anim operation bars
# animate four bars closing in on the given area
# args: (x1,y1) (x2,y2)
def anim_op_bars(duration, args):
global frame
global frame_no
global params
frame_count = int(duration * params['fps'])
# pick up arguments
if len(args) != 4:
abort("bars: invalid number of arguments")
# color_triple is the color to paint, thickness is thickness of the bars
# upper_left and lower_right are 2-tuples of the corner coordinates of the
# inner region
(color_triple, thickness, upper_left, lower_right) = args
# convert color tuple to string "str(23, 54, 0)"
color_triple='rgb(' + str(color_triple)[1:-1] + ')'
if len(upper_left) != 2 or len(lower_right) != 2:
abort("bars: coordinate parameter error")
for step in range(1,frame_count+1):
frame_i = frame_no + step
# copy original image, to draw the bars of each sub frame onto
my_frame = frame.copy()
draw = ImageDraw.Draw(my_frame)
# draw upper horizontal bar
draw.line((map (lambda v: int(v), (
my_frame.size[0] - (my_frame.size[0]-upper_left[0]+thickness) * float(step)/frame_count,
upper_left[1] - thickness,
my_frame.size[0],
upper_left[1] - thickness
))), fill=color_triple, width=thickness)
# draw lower horizontal bar
draw.line(map (lambda v: int(v), (
0,
lower_right[1] + thickness,
(lower_right[0] + thickness) * float(step)/frame_count,
lower_right[1] + thickness
)), fill=color_triple, width=thickness)
# draw left vertical bar
draw.line(map (lambda v: int(v), (
upper_left[0] - thickness,
0,
upper_left[0] - thickness,
(lower_right[1] + thickness) * float(step)/frame_count
)), fill=color_triple, width=thickness)
# draw right vertical bar
draw.line(map (lambda v: int(v), (
lower_right[0] + thickness,
my_frame.size[1] - (my_frame.size[1]-upper_left[1]+thickness) * float(step)/frame_count,
lower_right[0] + thickness,
my_frame.size[1]
)), fill=color_triple, width=thickness)
del draw
write_frame(frame_i, my_frame)
if step % (frame_count/20+1) == 0:
progress_update(step, frame_count, 'bars')
frame_no += frame_count
# leave bars on final position for next animation operations
frame = my_frame
# anim operation outer blue shadow
# animate shadowing outer region
# args: duration (x1,y1) (x2,y2)
def anim_op_outer_shadow(duration, args):
global frame
global frame_no
global params
frame_count = int(duration * params['fps'])
# pick up arguments
if len(args) != 2:
abort("outer_shadow: invalid number of arguments")
# upper_left and lower_right are 2-tuples of the corner coordinates of the
# inner region
(upper_left, lower_right) = args
if len(upper_left) != 2 or len(lower_right) != 2:
abort("outer_shadow: coordinate parameter error")
# save inner region (has to be pasted over running frame)
inner = frame.copy().crop(upper_left + lower_right)
for step in range(1,frame_count+1):
frame_i = frame_no + step
my_frame = frame.copy()
(width,height) = my_frame.size
pix = my_frame.load()
for row in range(height):
for col in range(width):
# skip inner region
if col > upper_left[0] and col < lower_right[0] and \
row > upper_left[1] and row < lower_right[1]:
continue
p = pix[col,row]
(r,g,b) = p
# compute average color value over all channels
avg = (float(r) + g + b)/3
# target factor goes from 0 to 1
tf = float(step)/frame_count
# source factor goes from 1 to 0, inversion of target factor
sf = 1 - tf
# blue channel goes towards average value
b = int( (avg * tf + b * sf) / 2 )
# other channels only go 2/3 way to 0
r = int(r * (2*sf + 1)/3)
g = int(g * (2*sf + 1)/3)
pix[col,row] = (r,g,b)
my_frame.paste(inner, upper_left + lower_right)
write_frame(frame_i, my_frame)
if step % (frame_count/20+1) == 0:
progress_update(step, frame_count, 'outer_shadow')
frame_no += frame_count
# leave image in final state for next animation operations
frame = my_frame
# anim operation zoom in
# args: duration (x1,y1) (x2,y2)
def anim_op_zoom_in(duration, args):
global frame
global frame_no
global params
frame_count = int(duration * params['fps'])
# pick up arguments
if len(args) != 2:
abort("zoom_in: invalid number of arguments")
# upper_left and lower_right are 2-tuples of the corner coordinates of the
# inner region
(upper_left, lower_right) = args
if len(upper_left) != 2 or len(lower_right) != 2:
abort("zoom_in: coordinate parameter error")
(width, height) = frame.size
for step in range(1,frame_count+1):
frame_i = frame_no + step
from_x = int(float(step)/frame_count * upper_left[0])
from_y = int(float(step)/frame_count * upper_left[1])
to_x = int((1-float(step)/frame_count) * (width-lower_right[0]) + lower_right[0])
to_y = int((1-float(step)/frame_count) * (height-lower_right[1]) + lower_right[1])
my_frame = frame.crop((from_x, from_y, to_x, to_y))
write_frame(frame_i, my_frame)
if step % (frame_count/20+1) == 0:
progress_update(step, frame_count, 'zoom_in')
frame_no += frame_count
# leave image in final state for next animation operations
frame = my_frame
def anim_op_route(duration, args):
global frame
global frame_no
global params
if len(args) < 4:
abort("route: not enough arguments (color, thickness, pt1, pt2, ...)")
frames_total = int(duration * params['fps'])
start_frame_no = frame_no
color = args.pop(0)
color_triple='rgb(' + str(color)[1:-1] + ')'
thickness = args.pop(0)
if len(args) < 2:
abort("route: requires at least 2 points")
if len(args[0]) != 2:
abort("route: third and later argument needs to be a point (x,y)")
args[0] = map(lambda v: float(v), args[0])
# STEP 1: compute pixels
pixels = list()
# initialize run values
pos = list(args[0])
heading = direction(args[0], args[1])
pixels.append(list(pos))
base_inertia = 2 * pi / 360 * 5 # rotational inertia in radiant/frame
# the distance for each iteration
distance_per_step = 2.2
# loop over route points
for i in range(1,len(args)):
if len(args[i]) != 2:
abort("route: third and later argument needs to be a point (x,y)")
args[i] = map(lambda v: float(v), args[i])
step_i = 0
# walk towards next route point
while distance(pos,args[i]) >= distance_per_step:
# turn direction towards next point, considering inertia
target_heading = cartesian2polar(direction(pos, args[i]))[1]
current_heading = cartesian2polar(heading)[1]
if target_heading != current_heading:
bearing = angle(current_heading, target_heading)
# start slow, then increase until the angle per frame is 2.5 fold
inertia = base_inertia * (1 + 1.5 * min(step_i, 50) / 50)
# in case of near target, assure minimum agility
inertia = max(inertia, asin(distance_per_step / distance(pos,args[i])))
if abs(bearing) <= inertia:
current_heading += bearing
elif bearing > 0:
current_heading += inertia
else:
current_heading -= inertia
heading = polar2cartesian(current_heading)
# rescale direction to correct step length
heading = scale(heading, distance_per_step)
# move towards next point
pos[0] += heading[0]
pos[1] += heading[1]
# store current position into pixel list
pixels.append(list(pos))
# append index of current waypoint to position vector for debugging
pixels[-1].append(i)
step_i += 1
# STEP 2: draw
draw = ImageDraw.Draw(frame)
frames_per_px = float(frames_total) / len(pixels)
#print "route debug: %d pixels, %d frames_total, %d frames per pixel" % (len(pixels), frames_total, frames_per_px)
frame_i = 1
for i in range(1,len(pixels)):
last_pos = pixels[i-1]
pos = pixels[i]
# draw
draw.line((
(int(last_pos[0]), int(last_pos[1])),
(int(pos[0]), int(pos[1]))
), fill=color_triple, width=thickness)
#print("anim_op_route: p(%d,%d->%d,%d) t(%d,%d [%d]) dis(%.2f)" % \
# (last_pos[0], last_pos[1], pos[0], pos[1], args[pos[2]][0], args[pos[2]][1], pos[2], \
# distance(pos, args[-1])))
while float(frame_i)/i <= frames_per_px:
if frame_i % 20 == 0:
progress_update(frame_i, frames_total, 'route')
write_frame(frame_no+frame_i, frame)
frame_i += 1
progress_update(frame_i, frames_total, 'route')
del(draw)
frame_no += frame_i-1
# ===============================================================
# MAIN PROGRAM
# ===============================================================
# global python variables
# params: global configuration settings
# frame: an image object containing the current frame
# frame_no: the number of the last frame written to disk (not always up-to-date during anim operations)
# last_frame_no: the number of the last frame written to disk
# phase: what processing steps to perform
# 1: parsing of config file
# 2: process animation operators
# 3: compile AVI file
frame_no = 0
last_frame_no = -1
params = dict()
phase = 3
# check for config file
if len(sys.argv) < 2:
abort("missing argument: configuration filename")
while len(sys.argv) > 1:
a = sys.argv.pop(1)
if a == '-p':
phase = int(sys.argv.pop(1))
elif a == '-c':
params['configfile'] = sys.argv.pop(1)
elif a == '-h':
help()
sys.exit(0)
elif a[0] == '-':
abort("unknown parameter: " + a)
else:
params['configfile'] = a
if not 'configfile' in params:
abort("missing argument: configuration filename")
if not os.path.exists(params['configfile']):
abort("config file not found'" + params['configfile'] + "'")
# parse configuration file
config_content = file(params['configfile'], 'rt').read()
ops = parse_config(config_content)
# check for mandatory settings
for par in ('resolution', 'mapfile', 'fps'):
if not par in params:
abort("'set " + par + " VALUE' is missing in configuration file: ")
# deduce output file name from config file name
if not 'outfile' in params:
re_basename = re.compile('^(.*)\.[a-zA-Z]+$')
m = re_basename.match(params['configfile'])
if m:
params['outfile'] = m.group(1) + '.avi'
else:
params['outfile'] = params['configfile'] + '.avi'
print("output filename", params['outfile'])
# set default values
if not 'tmpdir' in params:
params['tmpdir'] = 'tmp'
# open map image
print("opening mapfile", params['mapfile'])
try:
frame = Image.open(params['mapfile'])
except IOError:
print("can not open map image", params['mapfile'])
# create temporary output directory
if os.path.exists(params['tmpdir']):
rmtree(params['tmpdir'])
os.mkdir(params['tmpdir'])
# initialize progress indication
start_time = time()
frame_sum = 0
for op in ops:
(name, duration, args) = op
frame_sum += int(duration * params['fps'])
# =================================================
# MAIN LOOP
# =================================================
if phase < 2:
abort("last phase reached: config file parsed")
# write first frame (required in case first op is pause)
write_frame(frame_no, frame);
# process operators
for op in ops:
(name, duration, args) = op
print("processing animation " + name + " [" + str(duration) + "] " + str(args))
if name == 'pause':
anim_op_pause(duration)
elif name == 'bars':
anim_op_bars(duration, args)
elif name == 'outer_shadow':
anim_op_outer_shadow(duration, args)
elif name == 'zoom_in':
anim_op_zoom_in(duration, args)
elif name == 'route':
anim_op_route(duration, args)
else:
abort("unknown operation " + name)
if phase < 3:
abort("last phase reached: animation operators applied")
# complete progress line
print("")
print("running mencoder to produce", params['outfile'])
# build avi file from frames
command = ('mencoder',
"mf://" + params['tmpdir'] + "/*.png",
'-mf',
"type=png:w=%d:h=%d:fps=%d" % (params['resolution'][0],
params['resolution'][1], params['fps']),
'-ovc',
'lavc',
'-lavcopts',
'vcodec=mpeg4',
'-oac',
'copy',
'-o',
params['outfile'])
os.spawnvp(os.P_WAIT, 'mencoder', command)