{
  "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",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "d32b3958-c88c-476e-f2c9-477c9449165b"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Mounted at /drive\n"
          ]
        }
      ],
      "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 more experience defining your own functions, using the <code>range</code> function and <code>for loops</code> in Python to crop and resize images.\n",
        "\n",
        "**Cropping**\n",
        "\n",
        "Following the <code>crop</code> algorithm from the reading [Cropping Pictures](http://www.cs.kzoo.edu/cs103/Readings/CroppingPictures.pdf), define a function that takes an image as a parameter, along with coordinates (upperLeftX, upperLeftY) and (bottomRightX, bottomRightY), and creates a new image which is a small section of the original."
      ],
      "metadata": {
        "id": "MJ8VoPpTy-mD"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Define the crop function here\n"
      ],
      "metadata": {
        "id": "8WMLzD5bAQxP"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Open an image and test the crop function here.\n",
        "\n"
      ],
      "metadata": {
        "id": "4cewcCckBk6N"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Resizing - Making smaller**\n",
        "\n",
        "The following function, <code>quarter</code>, from the reading [Resizing and Blurring](http://www.cs.kzoo.edu/cs103/Readings/ResizingAndBlurring.pdf), makes an image smaller by using every other pixel in every other row.  Load this function and test it below.\n"
      ],
      "metadata": {
        "id": "S6Mg9BgzAdC6"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Creates a new picture 1/4 the size of\n",
        "# the original\n",
        "def quarter(picture):\n",
        "  # create a new image\n",
        "  width = picture.width//2 + 1\n",
        "  height = picture.height//2 + 1\n",
        "  newPic = Image.new('RGB',(width, height))\n",
        "\n",
        "  # use every other row, every other column\n",
        "  for y in range(0, picture.height, 2):\n",
        "    for x in range(0, picture.width, 2):\n",
        "      rvalue, gvalue, bvalue = picture.getpixel((x,y))\n",
        "      newPic.putpixel((x//2, y//2),(rvalue, gvalue, bvalue))\n",
        "\n",
        "  # return the new picture\n",
        "  return newPic"
      ],
      "metadata": {
        "id": "CbazLNCZB7bn"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Test your quarter function here\n",
        "\n"
      ],
      "metadata": {
        "id": "meNr-pLpDp0a"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Resizing - Making larger**\n",
        "\n",
        "The following function, <code>quadruple</code>, from the reading [Resizing and Blurring](http://www.cs.kzoo.edu/cs103/Readings/ResizingAndBlurring.pdf), makes an image larger by using every pixel in every other row four times.  Load this function and test it below.\n"
      ],
      "metadata": {
        "id": "j_BFc8bYMfrO"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Creates a new image 4 times the size\n",
        "# of the original (2*width, 2*height)\n",
        "def quadruple(picture):\n",
        "  # create a new image\n",
        "  width = picture.width*2\n",
        "  height = picture.height*2\n",
        "  newPic = Image.new('RGB',(width, height))\n",
        "\n",
        "  for y in range(0, height):\n",
        "    for x in range(0, width):\n",
        "      # use each value of x and y twice\n",
        "      rvalue, gvalue, bvalue = picture.getpixel((x//2,y//2))\n",
        "      newPic.putpixel((x, y),(rvalue, gvalue, bvalue))\n",
        "\n",
        "  # return the new picture\n",
        "  return newPic"
      ],
      "metadata": {
        "id": "GjE8gsWZH9iZ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Test the quadruple function here\n"
      ],
      "metadata": {
        "id": "c-rCCybQIOlj"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Blurring**\n",
        "\n",
        "The following function, <code>blur</code>, from the reading [Resizing and Blurring](http://www.cs.kzoo.edu/cs103/Readings/ResizingAndBlurring.pdf), makes a new image the same size as the original, but blurs it by taking the average red, green, and blue values of each pixel along with the pixels above, below, to the left, and to the right and setting these average values as the color values of the corresponding pixel in the new image.  Load this function and test it below."
      ],
      "metadata": {
        "id": "ahvRleAABi5s"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Blur an image by averaging 5 pixels per pixel\n",
        "# A simple blur\n",
        "def blur(picture):\n",
        "  width = picture.width\n",
        "  height = picture.height\n",
        "  newPic = Image.new('RGB', (width, height))\n",
        "  for y in range(1, height-1):\n",
        "    for x in range(1, width-1):\n",
        "      rtop, gtop, btop = picture.getpixel((x, y-1))\n",
        "      rleft, gleft, bleft = picture.getpixel((x-1, y))\n",
        "      rright, gright, bright = picture.getpixel((x+1, y))\n",
        "      rbottom, gbottom, bbottom = picture. getpixel((x, y+1))\n",
        "      rvalue, gvalue, bvalue = picture.getpixel((x,y))\n",
        "      newR = (rtop + rleft + rright + rbottom + rvalue)//5\n",
        "      newG = (gtop + gleft + gright + gbottom + gvalue)//5\n",
        "      newB = (btop + bleft + bright + bbottom + bvalue)//5\n",
        "      newPic.putpixel((x,y),(newR, newG, newB))\n",
        "\n",
        "  return newPic\n"
      ],
      "metadata": {
        "id": "peXqXT_jCcX-"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Test the blur function here\n",
        "\n"
      ],
      "metadata": {
        "id": "EYN2IJBlFcU2"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Exploring**\n",
        "\n",
        "What do you think will happen if you make an image larger and then smaller (*i.e.*, you create a new image by calling the <code>quadruple</code> function on an original image and then call the <code>quarter</code> function on that result)?  Will you get a result that is identical to the original image?  Explain why or why not.\n",
        "\n",
        "What would happen if you make an image smaller and then larger?  Is the result exactly the same as the original? Explain why or why not.\n",
        "\n",
        "Test these scenarios below and then write your answer here.\n",
        "\n",
        "**Your answer here:**"
      ],
      "metadata": {
        "id": "HUjKXo8JzhiZ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Test sizing up, then down\n",
        "\n"
      ],
      "metadata": {
        "id": "9uFJUIEmHzEM"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Test sizing down, then up\n"
      ],
      "metadata": {
        "id": "HnNMw-xIH3kq"
      },
      "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.  Make sure that your Code cells have been run and that your results for the color change effects are showing.  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"
      }
    }
  ]
}