Clarified animation generation code
authorNeil Smith <neil.git@njae.me.uk>
Sat, 22 Jul 2017 18:03:36 +0000 (19:03 +0100)
committerNeil Smith <neil.git@njae.me.uk>
Sat, 22 Jul 2017 18:03:36 +0000 (19:03 +0100)
05-display-board/display-board-animation.ipynb

index b004b96a096e6dd9f9de815f1d3c00b38a2bda8f..38a8c523657a03a158956f629a82f1cafe7b9b64 100644 (file)
    },
    "outputs": [],
    "source": [
-    "def draw_frame(grid, command, frame_number):\n",
+    "def draw_frame(grid, command, frame_number, gif=True):\n",
     "    im = Image.new('RGBA', (WIDTH * 10, HEIGHT * 10 + 21))\n",
     "\n",
     "    # make a blank image for the text, initialized to transparent text color\n",
-    "    txt = Image.new('RGBA', im.size, (255,255,255,0))\n",
-    "#     txt = Image.new('RGBA', im.size, (0, 0, 0, 0)) # use this line rather than line above for animated png\n",
+    "    if gif:\n",
+    "        txt = Image.new('RGBA', im.size, (255,255,255,0))\n",
+    "    else:\n",
+    "        txt = Image.new('RGBA', im.size, (0, 0, 0, 0)) # use this line rather than line above for animated png\n",
     "\n",
     "    # get a font\n",
     "    fnt = ImageFont.truetype('Pillow/Tests/fonts/FreeMono.ttf', 18)\n",
     "    d = ImageDraw.Draw(txt)\n",
     "\n",
     "    # draw text, full opacity\n",
-    "    d.text((1,1), command, font=fnt, fill='white')\n",
-    "#     d.text((1,1), command, font=fnt, fill='black') # use this line rather than line above for animated png\n",
+    "    if gif:\n",
+    "        d.text((1,1), command, font=fnt, fill='white')\n",
+    "    else:\n",
+    "        d.text((1,1), command, font=fnt, fill='black') # use this line rather than line above for animated png\n",
     "\n",
     "    draw = ImageDraw.Draw(im)\n",
     "    for (r, row) in enumerate(grid):\n",
     "    # draw.line((0, im.size[1], im.size[0], 0), fill=128)\n",
     "\n",
     "    out = Image.alpha_composite(im, txt)\n",
-    "#     out.save('frame{:04}.png'.format(frame_number), 'PNG') # uncomment here to save animated png frames\n",
+    "    if not gif:\n",
+    "        out.save('frame{:04}.png'.format(frame_number), 'PNG') # uncomment here to save animated png frames\n",
     "\n",
     "    del draw\n",
     "    \n",