{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "**Name:**\n",
        "\n",
        "**Date:**\n",
        "\n",
        "**Description of activity:**\n"
      ],
      "metadata": {
        "id": "sczaGya92i48"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Mount Google Drive\n"
      ],
      "metadata": {
        "id": "mv3Oarmb2zRK"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "1i3bvglG2d8z"
      },
      "outputs": [],
      "source": [
        "from google.colab import drive\n",
        "drive.mount('/drive')"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Import Libraries"
      ],
      "metadata": {
        "id": "GXNipdi-3F1Z"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from PIL import Image\n",
        "from matplotlib import pyplot as plt"
      ],
      "metadata": {
        "id": "8Nmg8IhE3KfV"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Exercises\n",
        "\n",
        "In this activity you will gain practice defining your own functions and using <code>for loops</code> in Python to manipulate pixels to create reflected and rotated images.\n",
        "\n",
        "**Vertical Reflection**\n",
        "\n",
        "A vertical reflection of an image creates a result as if you held a mirror up to the image.  You can think of this as reflecting the pixels across the right edge of the image.  Some things to keep in mind when defining this function:\n",
        "\n",
        "\n",
        "*   The dimensions of the reflected image are the same as the original.\n",
        "*   Pixels along the left edge of the original image should appear along the right edge of the new image.\n",
        "\n",
        "Write in answers to the following questions:\n",
        "\n",
        "1.   Where does pixel (0,0) (top left corner) get copied to in the\n",
        "new image?\n",
        "2.   Where does pixel (0, height - 1) (bottom left corner) get copied to in the new image?\n",
        "3.  Where does pixel (width - 1, 0) (top right corner) get copied to in the new image?\n",
        "4. Where does pixel (width - 1, height - 1) (bottom right corner) get copied to in the new image?\n",
        "5. For any particular pixel (x, y) in the original image, what should the *y*-value be for the corresponding pixel in the new image?\n",
        "6. For any particular pixel (x, y) in the originale image, what should the *x*-value be for the corresponding pixel in the new image?\n",
        "\n",
        "Follow the Vertical Reflection Algorithm, from the [Reflections and Rotations reading](http://www.cs.kzoo.edu/cs103/Readings/ReflectionsAndRotations.pdf) to define a function that takes an image as input, creates its mirror image, and then returns the mirrored image.  The algorithm is written below, as pseudocode.  Each line should be replaced with an actual code statement."
      ],
      "metadata": {
        "id": "MJ8VoPpTy-mD"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# define a vertical reflection function that takes an image as a parameter\n",
        "  # create a new blank image the same size as the original\n",
        "  # for each row y in the original picture:\n",
        "    # create a variable, newX, to hold the x-value of a pixel in new image and assign it to be the x-value of the last pixel in a row\n",
        "    # for each column x in the original picture:\n",
        "      # get the color values of the pixel (x,y) of the original picture\n",
        "      # assign the color values of the pixel (newX, y) of the new picture to be those of the original picture\n",
        "      # decrease the value of newX by 1\n",
        "  # return the new picture"
      ],
      "metadata": {
        "id": "8WMLzD5bAQxP"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Test your Reflection function**\n",
        "\n",
        "\n",
        "*   Open an image from your Google drive\n",
        "*   Call your reflection function, passing this image as the parameter, saving the result as a new image.\n",
        "* Display the original image side-by-side with the new image. (Remember to do this you can use plt.subplots to set up a figure with two graphs (images) in it.)\n",
        "* If you do not get the results you expected, ask a TA or instructor for help.\n",
        "* Now, reflect your reflected image and show the result.  What should it look like?  (**Hint:** It should look like the original image.  Why?)\n"
      ],
      "metadata": {
        "id": "S6Mg9BgzAdC6"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Test the vertical reflection function here\n",
        "\n"
      ],
      "metadata": {
        "id": "w0XxuuTsCnEP"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Horizontal Reflection**\n",
        "\n",
        "Now think about how you might reflect an image as if you put the mirror at the bottom of the image.  The top of the image becomes the bottom; the image ends up upside-down.  Define a new function that reflects an image across the bottom.  Things to think about:\n",
        "\n",
        "\n",
        "*   What will the width and height of the new image be?\n",
        "*   What is the relationship between the x- and y- values of pixels in the original image to the x- and y- values of pixels in the reflected image?\n",
        "\n"
      ],
      "metadata": {
        "id": "ahvRleAABi5s"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Define the horizontal reflection function here\n",
        "\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "peXqXT_jCcX-"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Test that this function behaves like we think it should. Show the original image side-by-side with the reflected image."
      ],
      "metadata": {
        "id": "a92yyIooCzeW"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Test horizontal reflection here\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "FOgwUiHNzInJ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Combining reflections**\n",
        "\n",
        "Try reflecting an image vertically, followed by reflecting it horizontally.  Show the result."
      ],
      "metadata": {
        "id": "sgDLeqxCDxDQ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Vertical reflection followed by a horizontal reflection\n",
        "\n"
      ],
      "metadata": {
        "id": "Uo4Tn4TGDyJX"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "What sequence of reflections would you need to use to get the image back to its' original orientation?\n",
        "\n",
        "Put your answer here:\n",
        "\n"
      ],
      "metadata": {
        "id": "HUjKXo8JzhiZ"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Reflecting across middle of image**\n",
        "Sometimes it's fun to put the mirror in the middle of the image, so you reflect one half onto the other half.  The following function from the reading reflects the left half of the image onto the right half.  Run this function, then write some code to test the function."
      ],
      "metadata": {
        "id": "OkXnOygdJkMf"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def reflectLeftOntoRight(picture):\n",
        "  newPic = picture.copy()\n",
        "  for y in range(newPic.height):\n",
        "    newX = newPic.width -1\n",
        "    for x in range(newPic.width//2):\n",
        "          rvalue, gvalue, bvalue = newPic.getpixel((x,y))\n",
        "          newPic.putpixel((newX,y),(rvalue, gvalue, bvalue))\n",
        "          newX = newX - 1\n",
        "\n",
        "  return newPic"
      ],
      "metadata": {
        "id": "LGWFeWafKAQJ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Rotations**  \n",
        "\n",
        "We will now experiment with rotating an image by 90°.  \n",
        "The Code cell below contains a function, <code>rotateRight</code> that rotates an image 90°.  It takes an image as a parameter, creates a new image with width equal to height of the original image, and height equal to the width of the original image. It then copies each row of the original image to a column of the new image.\n",
        "Run this code and then write some statements to test that the function works correctly.\n"
      ],
      "metadata": {
        "id": "lGhTaVgZ0AHx"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Function to rotate image 90 degrees to the right\n",
        "def rotateRight(picture):\n",
        "  # Creates a new image for the result\n",
        "  # Width of new image is height of original\n",
        "  # Height of new image is width of original\n",
        "  newPic = Image.new('RGB', (picture.height, picture.width))\n",
        "  # Create variable to keep track of x-coord in new picture\n",
        "  newX = newPic.width - 1\n",
        "  # Loop through original picture\n",
        "  for y in range(picture.height):\n",
        "    # Set up variable to keep track of y-coord in new picture\n",
        "    newY = 0\n",
        "    for x in range(picture.width):\n",
        "      rvalue, gvalue, bvalue = picture.getpixel((x, y))\n",
        "      newPic.putpixel((newX, newY), (rvalue, gvalue, bvalue))\n",
        "      newY = newY + 1\n",
        "    newX = newX - 1\n",
        "\n",
        "  return newPic"
      ],
      "metadata": {
        "id": "INL6ahJQzfXH"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Here you should test your function. Replace the lines below with real code.\n",
        "# OPEN AN IMAGE FROM YOUR GOOGLE DRIVE\n",
        "# CALL YOUR NEW FUNCTION ON THIS IMAGE AND SAVE THE RESULT IN A VARIBLE\n",
        "# SHOW THE ORIGINAL IMAGE AND THE NEW IMAGE"
      ],
      "metadata": {
        "id": "4jVEynKV8vqK"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Combining functions**\n",
        "\n",
        "You should write some code to test your answers to these questions.\n",
        "\n",
        "\n",
        "1.   If you rotate an image twice, do you get the same result as if you had reflected the image horizontally?  \n",
        "2.   Do you get the same result if you reflect the image vertically and then rotate it 90° that you would if you rotate it 90° and then reflect vertically?\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "mJ27vuhd1aBO"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Write your test code here"
      ],
      "metadata": {
        "id": "qDuagMJSU8wE"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Saving and Submitting\n",
        "\n",
        "Once you have created results that you are pleased with, skim through your notebook and make sure everything looks the way you expect.  If it all looks good, save this notebook as a .ipynb file.  (Go to File -> Download -> Download .ipynb)\n",
        "\n",
        "Submit your .ipynb file on Kit."
      ],
      "metadata": {
        "id": "m8jTI0_OBDRM"
      }
    }
  ]
}