{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "\n",
        "**Name:**\n",
        "\n",
        "**Date:**\n",
        "\n",
        "**Description of activity:**"
      ],
      "metadata": {
        "id": "C0on_Ytd1_VR"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "eSXGOcERq37r"
      },
      "outputs": [],
      "source": [
        "# Mount your Google drive\n",
        "from google.colab import drive\n",
        "drive.mount('/drive')\n"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Import necessary packages\n",
        "from PIL import Image, ImageDraw\n",
        "import math\n",
        "import cv2\n",
        "import glob"
      ],
      "metadata": {
        "id": "LV6Z8len2FW0"
      },
      "execution_count": 2,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "The following code cell contains functions that will be useful for displaying a list of images, for saving a list of images to a folder on your drive and for converting a folder of jpgs to an mp4 file."
      ],
      "metadata": {
        "id": "Eqx-N_Xi4fvQ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Utility functions:\n",
        "\n",
        "# The following function displays a list of images\n",
        "def displayImages(img_list):\n",
        "  for i in range(len(img_list)):\n",
        "    display(img_list[i])\n",
        "\n",
        "# This function saves a list of images to a folder on drive\n",
        "def savePicsToJPGS(img_List, path, picName):\n",
        "  for i in range(len(img_List)):\n",
        "    img_List[i].save(path+\"/\"+picName+str(i)+\".jpg\",\"JPEG\")\n",
        "\n",
        "# This function converts a folder of jpgs to an mp4 file\n",
        "def convertPicsToMP4(path, videoname):\n",
        "  img_list = []\n",
        "  for filename in glob.glob(path+'/*.jpg'):\n",
        "    img = cv2.imread(filename)\n",
        "    height, width, layers = img.shape\n",
        "    size = (width,height)\n",
        "    img_list.append(img)\n",
        "\n",
        "  out = cv2.VideoWriter(path+'/'+videoname,cv2.VideoWriter_fourcc(*'MP4V'), 15, size)\n",
        "\n",
        "  for i in range(len(img_list)):\n",
        "    out.write(img_list[i])\n",
        "  out.release()"
      ],
      "metadata": {
        "id": "IIlfW7zw3q3d"
      },
      "execution_count": 3,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "The following code cell contains functions for moving boxes on a plain background."
      ],
      "metadata": {
        "id": "8BHg1cGw4xaQ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# This function moves a box across the top of an image.\n",
        "def moveBox():\n",
        "    # Create an empty list to hold the images\n",
        "    img_list = []\n",
        "\n",
        "    # Create an image to be used as the background\n",
        "    bgImage = Image.new('RGB',(200,200),'yellow')\n",
        "\n",
        "    # Create a box that will be the moving object\n",
        "    box = Image.new('RGB',(10,10),'red')\n",
        "\n",
        "    # Set up the loop to go across the image, stepping by 10\n",
        "    for x in range(0,bgImage.width,10):\n",
        "        # refresh the background image\n",
        "        newImage = bgImage.copy()\n",
        "        # paste the box into the image\n",
        "        newImage.paste(box, (x,10))\n",
        "        # add the new image to the list\n",
        "        img_list.append(newImage)\n",
        "\n",
        "    #return the list of images\n",
        "    return img_list\n",
        "\n",
        "# This function moves two boxes simultaneously. One box\n",
        "# moves down the background in a circular motion, while the\n",
        "# other just moves down the diagonal of the background.\n",
        "def movingRectangles():\n",
        "  img_list = []\n",
        "  bgImage = Image.new('RGB',(200,200))\n",
        "  box = Image.new('RGB',(15,15), 'blue')\n",
        "  pinkBox = Image.new('RGB', (20,20),'pink')\n",
        "  for y in range(bgImage.height//4):\n",
        "    # create a new image\n",
        "    new_img = bgImage.copy()\n",
        "    new_img.paste(box, (y*5, y*5))\n",
        "    pinkx = 100 + int(10*math.sin(y))\n",
        "    pinky = 4*y + int(10*math.cos(y))\n",
        "    new_img.paste(pinkBox, (pinkx, pinky))\n",
        "    img_list.append(new_img)\n",
        "  return img_list\n",
        "\n",
        "# The following function moves text across the top of an image\n",
        "def tickerTape():\n",
        "    img_list = []\n",
        "    bgImage = Image.new('RGB',(200,200))\n",
        "    d =ImageDraw.Draw(bgImage)\n",
        "\n",
        "    for x in range(0,bgImage.width,10):\n",
        "      newImage = bgImage.copy()\n",
        "      d = ImageDraw.Draw(newImage)\n",
        "      d.text((x,10),\"Hello\", fill=(200,100,150))\n",
        "      img_list.append(newImage)\n",
        "    return img_list"
      ],
      "metadata": {
        "id": "x2YNyBpArNfO"
      },
      "execution_count": 4,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Testing the moveBox function\n",
        "boxes = moveBox()\n",
        "\n",
        "# displaying the images\n",
        "displayImages(boxes)\n",
        "\n",
        "# saving the images to a folder (replace this with the path to a folder in your drive)\n",
        "# uncomment the next two lines when you are ready to save your images\n",
        "#boxPath = '/drive/MyDrive/ColabNotebooks/Boxes'\n",
        "#savePicsToJPGS(boxes, boxPath, 'boxPic')\n",
        "\n",
        "# converting the images in the Boxes folder to an MP4\n",
        "# uncomment the next line when you are ready to create the animation file\n",
        "#convertPicsToMP4('/drive/MyDrive/ColabNotebooks/Boxes','boxMovie.mp4')"
      ],
      "metadata": {
        "id": "u6sbXuSrrR1p"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}