@thetree.html
001| """The Tree.
001|
001| Commands:
001| ts do_frame_timestamps Apply the timestamp from the filename to the frame.
001| xf do_frame_crossfades Frame crossfades from one image to the next.
001| fi do_movie_fade_in First image fade in.
001| fo do_movie_fade_out Last image fade out.
001| t0 do_title_opening Single opening title (black background).
001| t9 do_title_closing Single closing title (black background).
001| t1 do_title_image Single opening title (image background).
001|
001| mi do_merge_images Merge a series of images.
001|
001| mask do_image_mask Image Mask Sandbox
001| anim do_ani_sandbox Animation sandbox.
001| test do_test General sandbox.
001|
001| Videos:
001| The Tree v0.0 -- the-tree-00.mp4 -- https://youtu.be/1q14aEtREcU
001| The Tree v0.1 -- the-tree-01.mp4 -- https://youtu.be/OcNTBLv8uRI
001| The Tree v0.2 -- the-tree-02.mp4 -- https://youtu.be/R7VNKJMBZqU
001| The Tree v1.0 -- the-tree-10.mp4 --
001| """
002|
003| from sys import argv, exc_info
004| from traceback import extract_tb
005| from os import path, listdir
006| from PIL import Image, ImageDraw, ImageFont
007| from logger import logger,info,debug,trace, LogFilenameTimestamp
008| from webpage import KWARG
009| from animation import dispatch as AniDispatch
010|
011| BasePath = r'C:\CJS\prj\Python\app\arc\thetree'
012| ImgsPath = path.join(BasePath, 'img')
013| AnimPath = path.join(BasePath, 'ani')
014| Log = logger('TheTree')
015|
016| CMND = 'mask'
017| ARGS = {
018|
019|
020|
021|
022|
023| 'fpath': BasePath,
024| 'ipath': ImgsPath,
025| 'apath': AnimPath,
026| }
027|
028|
029|
030| def do_image_mask (**kwargs):
031| """Do Image Edge Detection."""
032| ipath = KWARG('ipath', ImgsPath, str, kwargs)
033| iname = KWARG('iname', 'a-image.jpg', str, kwargs)
034| fpath = KWARG('fpath', BasePath, str, kwargs)
035| fname = KWARG('fname', 'mask-image.png', str, kwargs)
036| xdim = KWARG('xdim', 1024, int, kwargs)
037| ydim = KWARG('ydim', 768, int, kwargs)
038| Log.info('image-mask:: %s' % str(list(kwargs.keys())))
039| Log.debug('ipath="%s"' % ipath)
040| Log.debug('iname="%s"' % iname)
041| Log.debug('fpath="%s"' % fpath)
042| Log.debug('fname="%s"' % fname)
043| Log.debug('dims=(%d, %d)' % (xdim,ydim))
044| Log.info()
045| srcfile = path.join(ipath, iname)
046| dstfile = path.join(fpath, fname)
047|
048| img_in = Image.open(srcfile)
049| pixels = img_in.getdata()
050| Log.debug('name: "%s"' % img_in.filename)
051| Log.debug('mode: "%s"' % img_in.mode)
052| Log.debug('size: (%d x %d)' % (img_in.width, img_in.height))
053| Log.debug('bands: %s' % ''.join(img_in.getbands()))
054| Log.debug('data-type: %s' % type(pixels))
055| Log.debug('data-size: %d' % len(pixels))
056| Log.debug('data-sample: %s' % str(pixels[0]))
057| Log.debug()
058|
059|
060| feed = iter(pixels)
061| grid = [[next(feed) for col in range(xdim)] for row in range(ydim)]
062| Log.debug('grid-sample: %s' % str(grid[0][0:8]))
063| Log.debug()
064|
065| integers = lambda p: (int(p[0]), int(p[1]), int(p[2]))
066| greyscale = lambda p: min(int((0.30*p[0]) + (0.59*p[1]) + (0.11*p[2])), 255)
067|
068|
069|
070| mask = [[0, 0, 0, 0, 0, 0, 0],
071| [0, 0, 0, 0, 0, 0, 0],
072| [1, 1, 1, 1, 1, 1, 1],
073| [0, 0, 0, 0, 0, 0, 0],
074| [0, 0, 0, 0, 0, 0, 0]]
075| width = len(mask[0])
076| height = len(mask)
077| total = sum([mask[r][c] for r in range(height) for c in range(width)])
078| for r in range(height):
079| for c in range(width):
080| mask[r][c] = mask[r][c] / total
081| Log.debug('mask-size: %d x %d' % (width, height))
082| Log.debug('mask-total: %d (%f)' % (total, 1/total))
083| Log.debug('mask: %s' % str(mask))
084| Log.debug()
085|
086|
087| def pixel_function(px_row, px_col):
088| """Given a (row,col) return a weighted pixel value."""
089| buf = [0.0, 0.0, 0.0]
090|
091| for r in range(height):
092| for c in range(width):
093| gr = px_row + r
094| gc = px_col + c
095| if (0 <= gr < ydim) and (0 <= gc < xdim):
096| buf[0] += (grid[gr][gc][0] * mask[r][c])
097| buf[1] += (grid[gr][gc][1] * mask[r][c])
098| buf[2] += (grid[gr][gc][2] * mask[r][c])
099|
100| return buf
101|
102|
103|
104| temp = [[pixel_function(row, col) for col in range(xdim)] for row in range(ydim)]
105| grid = [[integers(temp[row][col]) for col in range(xdim)] for row in range(ydim)]
106|
107|
108|
109|
110|
111|
112|
113|
114|
115|
116|
117|
118|
119|
120|
121|
122|
123|
124|
125|
126|
127|
128| spool = [tuple(grid[row][col]) for row in range(ydim) for col in range(xdim)]
129| Log.debug('spool-sample: %s' % str(spool[0:8]))
130| Log.debug()
131|
132|
133| imgout = Image.new('RGB', size=(xdim,ydim), color=(0,0,0))
134| imgout.putdata(spool)
135|
136| imgout.save(dstfile)
137| Log.info('wrote: %s' % dstfile)
138|
139| Log.info()
140| return 'Done'
141|
142|
143| def do_merge_images (**kwargs):
144| """Do Merge Images."""
145| ipath = KWARG('ipath', ImgsPath, str, kwargs)
146| fpath = KWARG('fpath', BasePath, str, kwargs)
147| fname = KWARG('fname', 'merged-images.png', str, kwargs)
148| xdim = KWARG('xdim', 1024, int, kwargs)
149| ydim = KWARG('ydim', 768, int, kwargs)
150| n_images = KWARG('max', 0, int, kwargs)
151| f_prefix = KWARG('fsw', '', str, kwargs)
152| Log.info('merge-images:: %s' % str(list(kwargs.keys())))
153| Log.debug('ipath="%s"' % ipath)
154| Log.debug('fpath="%s"' % fpath)
155| Log.debug('fname="%s"' % fname)
156| Log.debug('dims=(%d, %d)' % (xdim,ydim))
157| Log.debug('number-of-images: %d' % n_images)
158| Log.debug('file-prefix="%s"' % f_prefix)
159| Log.info()
160| accum = [[[0,0,0] for col in range(xdim)] for row in range(ydim)]
161| image_counter = 0
162| srcpath = path.join(ipath, 'size')
163|
164| for fx,fn in enumerate(listdir(srcpath)):
165| s = '%2d: %s' % (fx+1,fn)
166| Log.info(s)
167| print(s)
168| if f_prefix and not fn.startswith(f_prefix):
169| continue
170|
171| filename = path.join(srcpath, fn)
172| img_in = Image.open(filename)
173| pixels = list(img_in.getdata())
174| Log.debug('size: (%d x %d)' % (img_in.width, img_in.height))
175| Log.debug('bands: %s' % ''.join(img_in.getbands()))
176| Log.debug('data-size: %d' % len(pixels))
177| Log.debug('data-sample: %s' % str(pixels[0:12]))
178| Log.debug()
179|
180| feed = iter(pixels)
181| for row in range(ydim):
182| for col in range(xdim):
183| p = next(feed)
184| accum[row][col][0] += p[0]
185| accum[row][col][1] += p[1]
186| accum[row][col][2] += p[2]
187| image_counter += 1
188| if (0 < n_images) and (n_images <= image_counter):
189| break
190| Log.debug('image-counter: %d' % image_counter)
191| Log.debug('accum: %s' % str(accum[0][0:10]))
192|
193|
194| for row in range(ydim):
195| for col in range(xdim):
196| accum[row][col][0] = int(accum[row][col][0] / image_counter) % 256
197| accum[row][col][1] = int(accum[row][col][1] / image_counter) % 256
198| accum[row][col][2] = int(accum[row][col][2] / image_counter) % 256
199| Log.debug('accum: %s' % str(accum[0][0:10]))
200|
201| spool = [tuple(accum[row][col]) for row in range(ydim) for col in range(xdim)]
202| Log.debug('spool: %s' % str(spool[0:10]))
203|
204|
205| imgout = Image.new('RGB', size=(xdim,ydim), color=(0,0,0))
206| imgout.putdata(spool)
207|
208| fn = path.join(fpath, fname)
209| imgout.save(fn)
210| Log.info('wrote: %s' % fn)
211|
212| Log.info()
213| return 'Done'
214|
215|
216| def do_frame_timestamps (**kwargs):
217| """Do frame timestamps. Apply the timestamp from the filename to the frame."""
218| ipath = KWARG('ipath', ImgsPath, str, kwargs)
219| apath = KWARG('apath', AnimPath, str, kwargs)
220| Log.info('frame-timestamps:: %s' % str(list(kwargs.keys())))
221| Log.debug('ipath="%s"' % ipath)
222| Log.debug('apath="%s"' % apath)
223| Log.info()
224| srcpath = path.join(ipath, 'size')
225| dstpath = path.join(ipath, 'ts')
226| cfg1 = 'f:courier,b/s:40/a:left/xy:0,0/fg:white/bg:black,1.0/pad:5/'
227|
228| for fx,fn in enumerate(listdir(srcpath)):
229| fname = path.join(srcpath, fn)
230| Log.info('%2d: %s' % (fx,fname))
231| ts = '%s/%s/%s %s:%s:%s' % (fn[0:4],fn[5:7],fn[8:10],fn[11:13],fn[14:16],fn[17:19])
232| props = {
233| 'spath': srcpath, 'sname':fn,
234| 'dpath': dstpath, 'dname':'img_%04d.png', 'start':str(fx),
235| 'count':'1',
236| 'codes':'/fi:0/fo:0//ti:0/to:0/',
237| 'title':'%s/:/%s' % (ts, cfg1),
238| }
239| Log.debug(props)
240| rv = AniDispatch('ttls', **props)
241| Log.debug(rv)
242| Log.debug()
243|
244| Log.info()
245| return 'Done'
246|
247|
248| def do_frame_crossfades (**kwargs):
249| """Do frame crossfades from one image to the next."""
250| ipath = KWARG('ipath', ImgsPath, str, kwargs)
251| apath = KWARG('apath', AnimPath, str, kwargs)
252| Log.info('frame-crossfades:: %s' % str(list(kwargs.keys())))
253| Log.debug('ipath="%s"' % ipath)
254| Log.debug('apath="%s"' % apath)
255| Log.info()
256| srcpath = path.join(ipath, 'ts')
257| dstpath = path.join(apath, '2')
258| start = 0
259|
260| fn0 = ''
261| for fx,fn in enumerate(listdir(srcpath)):
262| fname = path.join(srcpath, fn)
263| Log.info('%2d: %s' % (fx,fname))
264| if fn0:
265| props = {
266| 's1path':srcpath, 's1name':fn0,
267| 's2path':srcpath, 's2name':fn,
268| 'dpath': dstpath, 'dname':'img_%04d.png', 'start':str(start),
269| 'count':'6', 'head':'12', 'tail':'12',
270| }
271| Log.debug(props)
272| rv = AniDispatch('xfs', **props)
273| Log.debug(rv)
274| start += 30
275| fn0 = fn
276| Log.debug()
277|
278| Log.info()
279| return 'Done'
280|
281|
282| def do_movie_fade_in (**kwargs):
283| """Do first image fade in."""
284| ipath = KWARG('ipath', ImgsPath, str, kwargs)
285| apath = KWARG('apath', AnimPath, str, kwargs)
286| fname = KWARG('fname', 'img_0000.png', str, kwargs)
287| Log.info('movie-fade-in:: %s' % str(list(kwargs.keys())))
288| Log.debug('ipath="%s"' % ipath)
289| Log.debug('apath="%s"' % apath)
290| Log.debug('fname="%s"' % fname)
291| Log.info()
292| srcpath = path.join(ipath, 'ts')
293| dstpath = path.join(apath, '1')
294|
295| props = {
296| 'spath':srcpath, 'sname':fname,
297| 'dpath':dstpath, 'dname':'img_%04d.png', 'start':'0',
298| 'head':'15', 'count':'15', 'tail':'20',
299| }
300| rv = AniDispatch('fibs', **props)
301| Log.debug(rv)
302|
303| Log.info()
304| return 'Done'
305|
306| def do_movie_fade_out (**kwargs):
307| """Do last image fade out."""
308| ipath = KWARG('ipath', ImgsPath, str, kwargs)
309| apath = KWARG('apath', AnimPath, str, kwargs)
310| fname = KWARG('fname', 'img_9999.png', str, kwargs)
311| Log.info('movie-fade-out:: %s' % str(list(kwargs.keys())))
312| Log.debug('ipath="%s"' % ipath)
313| Log.debug('apath="%s"' % apath)
314| Log.debug('fname="%s"' % fname)
315| Log.info()
316| srcpath = path.join(ipath, 'ts')
317| dstpath = path.join(apath, '3')
318|
319| props = {
320| 'spath':srcpath, 'sname':fname,
321| 'dpath':dstpath, 'dname':'img_%04d.png', 'start':'0',
322| 'head':'20', 'count':'15', 'tail':'50',
323| }
324| rv = AniDispatch('fobs', **props)
325| Log.debug(rv)
326|
327| Log.info()
328| return 'Done'
329|
330|
331| def do_title_opening (**kwargs):
332| """Do opening title (solid color background)."""
333| ipath = KWARG('ipath', ImgsPath, str, kwargs)
334| apath = KWARG('apath', AnimPath, str, kwargs)
335| fname = KWARG('fname', 'black.png', str, kwargs)
336| Log.info('title-opening:: %s' % str(list(kwargs.keys())))
337| Log.debug('ipath="%s"' % ipath)
338| Log.debug('apath="%s"' % apath)
339| Log.debug('fname="%s"' % fname)
340| Log.info()
341| srcpath = path.join(ipath, 'etc')
342| dstpath = path.join(apath, '0')
343|
344| ttl1 = 'The Tree'
345| ttl2 = '2023 Jan-June'
346| cfg1 = 'f:arial,b/s:120/ha:0.5/va:0.3/fg:white/bg:95,128,171,191/pad:5,33,150,33/'
347| cfg2 = 'f:arial,i/s:80/ha:0.5/va:0.5/fg:white/bg:95,128,171,191/pad:0/'
348| props = {
349| 'spath': srcpath, 'sname':fname,
350| 'dpath': dstpath, 'dname':'img_%04d.png', 'start':'0',
351| 'count':'75',
352| 'codes':'/fi:0/fo:0//ti:10,15/to:50,25/',
353|
354| 'title':'%s/:/%s' % (ttl1, cfg1),
355| 'title2':'%s/:/%s' % (ttl2, cfg2),
356| }
357| rv = AniDispatch('ttls', **props)
358| Log.debug(rv)
359|
360| Log.info()
361| return 'Done'
362|
363| def do_title_closing (**kwargs):
364| """Do closing title (solid color background)."""
365| ipath = KWARG('ipath', ImgsPath, str, kwargs)
366| apath = KWARG('apath', AnimPath, str, kwargs)
367| fname = KWARG('fname', 'black.png', str, kwargs)
368| Log.info('title-ending:: %s' % str(list(kwargs.keys())))
369| Log.debug('ipath="%s"' % ipath)
370| Log.debug('apath="%s"' % apath)
371| Log.debug('fname="%s"' % fname)
372| Log.info()
373| srcpath = path.join(ipath, 'etc')
374| dstpath = path.join(apath, '9')
375|
376| ttl1 = 'The End'
377| cfg1 = 'f:comic/s:120/ha:0.5/va:0.5/fg:white/bg:black/pad:0/'
378| props = {
379| 'spath': srcpath, 'sname':fname,
380| 'dpath': dstpath, 'dname':'img_%04d.png', 'start':'0',
381| 'count':'75',
382| 'codes':'/fi:0/fo:0//ti:10,10/to:50,25/',
383|
384| 'title':'%s/:/%s' % (ttl1, cfg1),
385| }
386| rv = AniDispatch('ttls', **props)
387| Log.debug(rv)
388|
389| Log.info()
390| return 'Done'
391|
392| def do_title_image (**kwargs):
393| """Do title (image background)."""
394| ipath = KWARG('ipath', ImgsPath, str, kwargs)
395| apath = KWARG('apath', AnimPath, str, kwargs)
396| fname = KWARG('fname', '2016-07-16_13-40-11.jpg', str, kwargs)
397| Log.info('title-image:: %s' % str(list(kwargs.keys())))
398| Log.debug('ipath="%s"' % ipath)
399| Log.debug('apath="%s"' % apath)
400| Log.debug('fname="%s"' % fname)
401| Log.info()
402| srcpath = path.join(ipath, 'size')
403| dstpath = path.join(apath, '_tmp')
404|
405| ttl1 = 'The Tree'
406| ttl2 = '2023 Jan-June'
407| cfg1 = 'f:arial,b/s:120/ha:0.5/va:0.3/fg:white/bg:95,128,171,191/pad:5,33,150,33/'
408| cfg2 = 'f:arial,i/s:80/ha:0.5/va:0.5/fg:white/bg:95,128,171,191/pad:0/'
409| props = {
410| 'spath': srcpath, 'sname':fname,
411| 'dpath': dstpath, 'dname':'img_%04d.png', 'start':'0',
412| 'count':'75',
413| 'codes':'/fi:10,5/fo:10,5//ti:10/to:40,10/',
414|
415| 'title':'%s/:/%s' % (ttl1, cfg1),
416| 'title2':'%s/:/%s' % (ttl2, cfg2),
417| }
418| rv = AniDispatch('ttls', **props)
419| Log.debug(rv)
420|
421| Log.info()
422| return 'Done'
423|
424|
425| def do_ani_sandbox (**kwargs):
426| """Do Sandbox for Animation calls."""
427| ipath = KWARG('ipath', ImgsPath, str, kwargs)
428| apath = KWARG('apath', AnimPath, str, kwargs)
429| fpath = KWARG('fpath', BasePath, str, kwargs)
430| xdim = KWARG('xdim', 1024, int, kwargs)
431| ydim = KWARG('ydim', 768, int, kwargs)
432| color = KWARG('c', 'black', str, kwargs)
433| srcname = KWARG('fi', 'image.png', str, kwargs)
434| dstname = KWARG('fo', 'sandbox.png', str, kwargs)
435| srcpath = path.join(ipath, 'orig')
436| dstpath = path.join(apath, '_tmp')
437| Log.info('ani-sandbox:: %s' % str(list(kwargs.keys())))
438| Log.debug('ipath="%s"' % ipath)
439| Log.debug('apath="%s"' % apath)
440| Log.debug('fpath="%s"' % fpath)
441| Log.debug('srcpath="%s"' % srcpath)
442| Log.debug('dstpath="%s"' % dstpath)
443| Log.debug('sname="%s"' % srcname)
444| Log.debug('dname="%s"' % dstname)
445| Log.debug('dims=(%d, %d)' % (xdim,ydim))
446| Log.info()
447|
448| props = {
449| 'spath':srcpath, 'sname':srcname,
450| 'dpath':dstpath, 'dname':dstname,
451| 'color':color, 'start':'0', 'count':'1',
452| 'xdim':xdim, 'ydim':ydim,
453| }
454| rv = AniDispatch('', **props)
455| Log.debug(rv)
456|
457| Log.info()
458| return 'Done'
459|
460| def do_test (**kwargs):
461| """Do a test."""
462| ipath = KWARG('ipath', ImgsPath, str, kwargs)
463| apath = KWARG('apath', AnimPath, str, kwargs)
464| fpath = KWARG('fpath', BasePath, str, kwargs)
465| fname = KWARG('fname', 'test.png', str, kwargs)
466| xdim = KWARG('xdim', 1024, int, kwargs)
467| ydim = KWARG('ydim', 768, int, kwargs)
468| Log.info('test:: %s' % str(list(kwargs.keys())))
469| Log.debug('ipath="%s"' % ipath)
470| Log.debug('apath="%s"' % apath)
471| Log.debug('fpath="%s"' % fpath)
472| Log.debug('fname="%s"' % fname)
473| Log.debug('dims=(%d, %d)' % (xdim,ydim))
474| Log.info()
475|
476| Log.info()
477| return 'Done'
478|
479| def dispatch (cmd, **kwargs):
480| """Dispatch to a module function based on CMD."""
481| Log.info('CMD: %s' % cmd)
482| for kw in kwargs:
483| Log.debug('argument[%s]: %s' % (kw,kwargs[kw]))
484| Log.info()
485| if cmd == 'ts': return do_frame_timestamps(**kwargs)
486| if cmd == 'xf': return do_frame_crossfades(**kwargs)
487| if cmd == 'fi': return do_movie_fade_in(**kwargs)
488| if cmd == 'fo': return do_movie_fade_out(**kwargs)
489| if cmd == 't0': return do_title_opening(**kwargs)
490| if cmd == 't1': return do_title_image(**kwargs)
491| if cmd == 't9': return do_title_closing(**kwargs)
492| if cmd == 'mi': return do_merge_images(**kwargs)
493| if cmd == 'mask': return do_image_mask(**kwargs)
494| if cmd == 'anim': return do_ani_sandbox(**kwargs)
495| if cmd == 'test': return do_test(**kwargs)
496| return 'Nothing to do!'
497|
498| if __name__ == '__main__':
499| print('autorun: %s' % argv[0])
500|
501| Log.start(path.join(BasePath,'thetree.log'))
502| Log.level(info())
503| Log.info(argv[0])
504| Log.info()
505| cmd = argv[1] if 1 < len(argv) else CMND
506| kwargs = ARGS
507| for a in argv[2:]:
508| nv = a.split('=', 2)
509| kwargs[nv[0]] = (nv[1] if 1<len(nv) else '')
510| Log.debug('%s: "%s"' % (nv[0], kwargs[nv[0]]))
511| try:
512| print('CMD: %s' % cmd)
513| print()
514| obj = dispatch(cmd, **kwargs)
515| print()
516| print(obj)
517| Log.info()
518| Log.info(obj)
519| except:
520| etype, evalue, tb = exc_info()
521| ts = extract_tb(tb)
522| Log.error()
523| Log.error('%s: %s' % (etype.__name__,str(evalue)))
524| Log.error()
525| for t in ts[-5:]:
526| Log.error('[%d] %s (%s)' % (t[1], t[2], t[0]))
527| Log.error(' %s' % t[3])
528| Log.error()
529| raise
530| finally:
531| Log.end()
532|
533| '''eof'''
534|