{ "cells": [ { "cell_type": "markdown", "id": "277aa350-a03d-4f91-9f11-901850e1f23d", "metadata": {}, "source": [ "# Animations using series of images\n", "\n", "## Using image files\n", "\n", "Need to have the folder Cats in the directory containing the notebook." ] }, { "cell_type": "code", "execution_count": 15, "id": "51c88aa9-5f13-47d2-a18e-3ffe04832394", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Date and time 2023-03-22 10:21:05.338037\n", "Working on Cats\\cat-running-00.png\n", "Working on Cats\\cat-running-01.png\n", "Working on Cats\\cat-running-02.png\n", "Working on Cats\\cat-running-03.png\n", "Working on Cats\\cat-running-04.png\n", "Working on Cats\\cat-running-05.png\n", "Working on Cats\\cat-running-06.png\n", "Working on Cats\\cat-running-07.png\n", "Date and time 2023-03-22 10:21:06.149871\n", "Time since last check is 0:00:00.811834\n" ] } ], "source": [ "import datetime\n", "now = datetime.datetime.now()\n", "print(\"Date and time\",str(now))\n", "#\n", "import matplotlib.pyplot as plt\n", "import matplotlib.animation as anim\n", "from PIL import Image\n", "from glob import glob\n", "#\n", "# Can't use inline matplotlib backend for animations, so try TkAgg\n", "import matplotlib\n", "matplotlib.use('TkAgg')\n", "#\n", "fig, ax = plt.subplots(figsize = (5.12, 2.56))\n", "fig.subplots_adjust(bottom = 0, top = 1, left = 0, right = 1)\n", "ax.axis('off')\n", "#\n", "ims = []\n", "for fname in sorted(glob('Cats/cat-running-*.png')):\n", " print(\"Working on\",fname)\n", " im = ax.imshow(Image.open(fname), animated = True)\n", " ims.append([im])\n", "#\n", "ani = anim.ArtistAnimation(fig, artists = ims, interval = 33, repeat = True)\n", "ani.save('running-cat.gif', writer = 'imagemagick')\n", "#\n", "fig.show()\n", "#\n", "then = now\n", "now = datetime.datetime.now()\n", "print(\"Date and time\",str(now))\n", "print(\"Time since last check is\",str(now - then))" ] }, { "cell_type": "markdown", "id": "8a7c947e-8907-42a7-b311-6cfb8b6b977e", "metadata": {}, "source": [ "## Creating images\n", "\n", "Based on example in matplotlib documentation!" ] }, { "cell_type": "code", "execution_count": 1, "id": "73fbf063-bddb-43b2-93c5-cc5f37b64b6e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Date and time 2023-03-24 08:47:18.083984\n", "Date and time 2023-03-24 08:47:30.506297\n", "Time since last check is 0:00:12.422313\n" ] } ], "source": [ "import datetime\n", "now = datetime.datetime.now()\n", "print(\"Date and time\",str(now))\n", "#\n", "import matplotlib.pyplot as plt\n", "import matplotlib.animation as anim\n", "import numpy as np\n", "#\n", "# Can't use inline matplotlib backend for animations, so try TkAgg\n", "import matplotlib\n", "matplotlib.use('TkAgg')\n", "#\n", "def f(x, y):\n", " f = np.sin(x) + np.cos(y)\n", " return f\n", "#\n", "n_points = 128\n", "#\n", "# Using row (x) and column (y) vectors ensures output of function is n_points times n_points matrix \n", "# that can be shown as image \n", "x = np.linspace(0, 2*np.pi, n_points)\n", "y = np.linspace(0, 2*np.pi, n_points).reshape(n_points, 1)\n", "#\n", "fig, ax = plt.subplots(figsize = (4, 4))\n", "fig.subplots_adjust(bottom = 0, top = 1, left = 0, right = 1)\n", "ax.axis('off')\n", "x_step = 1/16\n", "y_step = 1/32\n", "ims = []\n", "#\n", "# Using n_frames (multiple of) n_points ensures smooth repeat of animation\n", "n_frames = n_points\n", "#\n", "for n in range(0, n_frames):\n", " im = ax.imshow(f(x, y), cmap = plt.get_cmap('plasma'), animated = True)\n", " ims.append([im])\n", " x += np.pi*x_step\n", " y += np.pi*y_step\n", "#\n", "ani = anim.ArtistAnimation(fig, artists = ims, interval = 30, repeat_delay = 0)\n", "ani.save('waves.gif', writer = 'imagemagick')\n", "#\n", "fig.show()\n", "#\n", "then = now\n", "now = datetime.datetime.now()\n", "print(\"Date and time\",str(now))\n", "print(\"Time since last check is\",str(now - then))" ] }, { "cell_type": "code", "execution_count": null, "id": "18808aba-8dcd-4034-a4b2-7ddd7ad84136", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.16" } }, "nbformat": 4, "nbformat_minor": 5 }