Browse Source

Add an option to save animation images

Yann Weber 6 years ago
parent
commit
f9172c0265
1 changed files with 35 additions and 4 deletions
  1. 35
    4
      gte/__main__.py

+ 35
- 4
gte/__main__.py View File

@@ -7,7 +7,7 @@ import time
7 7
 import datetime
8 8
 import numpy as np
9 9
 from random import randint
10
-from multiprocessing import Pool
10
+from multiprocessing import Pool, Process, Queue
11 11
 
12 12
 logging.basicConfig(level=logging.INFO,
13 13
                     format="%(created)d %(asctime)s:%(module)s(%(lineno)d):%(levelname)s:%(message)s")
@@ -103,7 +103,8 @@ parser_gen.add_argument('--gray', '-G', action='store_const',
103 103
                         default=False, const=True)
104 104
 parser_gen.add_argument('--no-diagonals', '-d', action='store_const',
105 105
                         default=False, const=True)
106
-
106
+parser_gen.add_argument('--threads', '-t', type=int, metavar='N',
107
+                        default=2, help="Animation writting threads")
107 108
 
108 109
 args = parser.parse_args()
109 110
 
@@ -221,6 +222,24 @@ if 'pool_size' in args:
221 222
     exit(0)
222 223
 else:
223 224
     # Generate
225
+    if args.output_dir is not None:
226
+        #from PIL import Image
227
+        import scipy
228
+        def save_anim(data_q):
229
+            while True:
230
+                wval, fname = data_q.get()
231
+                if wval is None:
232
+                    return
233
+                #im = Image.fromarray(wval, 'RGB')
234
+                #im.save(fname)
235
+                scipy.misc.imsave(fname, wval)
236
+                logger.info('Written %s / %d' % (fname, args.steps//args.anim_div))
237
+
238
+        data_q = Queue()
239
+        wps = [Process(target=save_anim, args=(data_q,))
240
+               for _ in range(args.threads)]
241
+        for wp in wps:
242
+            wp.start()
224 243
     w = World(args.world_height, args.world_width, gray=args.gray)
225 244
     prog = rpnlib.RpnExpr.from_string(args.prog)
226 245
     turmits = [LivingTurmit(world=w, prog=prog, diag=not args.no_diagonals)
@@ -238,8 +257,20 @@ else:
238 257
         for turmit in turmits:
239 258
             turmit()
240 259
         if args.output_dir is not None and step % args.anim_div == 0:
241
-            #w.save('/tmp/anim/GTE_ANIM_%d.png' % (step/10))
242
-            w.save('/tmp/anim/GTE_ANIM_%d.png' % (step/args.anim_div))
260
+            fname = 'GTE_ANIM_%d.png' % (step/args.anim_div)
261
+            fname = os.path.join(args.output_dir, fname)
262
+            data_q.put((w._val, fname))
263
+        #if args.output_dir is not None and step % args.anim_div == 0:
264
+        #    #w.save('/tmp/anim/GTE_ANIM_%d.png' % (step/10))
265
+        #    fname = 'GTE_ANIM_%d?png' % (step/args.anim_div)
266
+        #    w.save(os.path.join(args.output_dir, fname))
267
+    
268
+    if args.output_dir is not None:
269
+        for _ in wps:
270
+            data_q.put((None, None))
271
+        for wp in wps:
272
+            wp.join()
273
+        data_q.close()
243 274
     stop = time.time()
244 275
 
245 276
     score_dir = sum([t.dirvar() for t in turmits]) / len(turmits)

Loading…
Cancel
Save