{
  "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": "4132fcd1-fac4-429c-c485-02a53f7e6240"
      },
      "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, ImageDraw, ImageFont\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 through changing the color of pixels in an image.\n",
        "\n",
        "**Coloring all pixels**\n",
        "\n",
        "The following function colors all of the pixels of an image black. Run this cell.  Then create a new Code cell, open one of your images, or create an empty (non-black) image and test that this function works as expected."
      ],
      "metadata": {
        "id": "MJ8VoPpTy-mD"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def colorAllBlack(picture):\n",
        "  # duplicate the original picture\n",
        "  newPic = picture.copy()\n",
        "\n",
        "  for y in range(newPic.height):\n",
        "    # change the pixels in row y\n",
        "    for x in range(newPic.width):\n",
        "      newPic.putpixel((x,y),(0, 0, 0))\n",
        "\n",
        "  # return the new picture\n",
        "  return newPic\n"
      ],
      "metadata": {
        "id": "8WMLzD5bAQxP"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Open an image and test the colorAllBlack function here.\n",
        "# Show the resulting image\n",
        "\n"
      ],
      "metadata": {
        "id": "4cewcCckBk6N"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Color some pixels**\n",
        "\n",
        "The function below is designed to color only a portion of an image.  Find an image you would like to use, or just use an empty (non-black) image. Determine which section you would like to color black.  You will need the coordinates of the upper left corner of the segment and the lower right coordinate of the segment.  You will use these numbers to in place of *startY*, *endY*, *startX*, and *endX* in the ranges being used in the loops of the function below.  Edit the function with your numbers.  The call the function with your image and show the result.\n"
      ],
      "metadata": {
        "id": "S6Mg9BgzAdC6"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def colorSomeBlack(picture):\n",
        "  # duplicate the original picture\n",
        "  newPic = picture.copy()\n",
        "\n",
        "  for y in range(100, 150):\n",
        "    # change the pixels in row y\n",
        "    for x in range(120, 200):\n",
        "      newPic.putpixel((x,y),(0, 0, 0))\n",
        "\n",
        "  # return the new picture\n",
        "  return newPic"
      ],
      "metadata": {
        "id": "CbazLNCZB7bn"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Test your colorSomeBlack function here\n",
        "\n"
      ],
      "metadata": {
        "id": "meNr-pLpDp0a"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**(Optional) Making the function more general**\n",
        "\n",
        "Modify the <code>colorSomeBlack</code> function so that it takes the upper left coordinate and the lower right coordinate of the section to be changed as parameters.  You should have *startX, startY, endX, endY* as parameters to the function, and then use specific values for these parameters when you call the function.  A function call might now look like <blockquote><code>newPic = colorSomeBlack(myImage, 350, 120, 475, 200)</code></blockquote>\n"
      ],
      "metadata": {
        "id": "j_BFc8bYMfrO"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Define your modified colorSomeBlack function here"
      ],
      "metadata": {
        "id": "GjE8gsWZH9iZ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Test your modified function here"
      ],
      "metadata": {
        "id": "c-rCCybQIOlj"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Changing Red**\n",
        "\n",
        "The following code (from the [Manipulating Colors reading](http://www.cs.kzoo.edu/cs103/Readings/ManipulatingColors.pdf)) defines a function that reduces the amount of red in an image by 25%.  It then defines a function that increases the amount of red in an image by 20%.  Run this Code cell.  In the next Code cell, select your image and test it with each of the functions.  Show the original image, the image with decreased red, and the image with increased red all in one figure.  (See Example 6 in the reading if you need a reminder how to do this.)"
      ],
      "metadata": {
        "id": "ahvRleAABi5s"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Reduce the amount of red by 25%\n",
        "def reduceRed(picture):\n",
        "  newPic = picture.copy()\n",
        "  for x in range(newPic.width):\n",
        "    for y in range(newPic.height):\n",
        "          rvalue, gvalue, bvalue = newPic.getpixel((x,y))\n",
        "          newR = int(rvalue * 0.75)\n",
        "          newPic.putpixel((x,y),(newR, gvalue, bvalue))\n",
        "\n",
        "  return newPic\n",
        "\n",
        "# Increase the amount of red by 20%\n",
        "def increaseRed(picture):\n",
        "  newPic = picture.copy()\n",
        "  for x in range(newPic.width):\n",
        "    for y in range(newPic.height):\n",
        "          rvalue, gvalue, bvalue = newPic.getpixel((x,y))\n",
        "          newR = int(rvalue * 1.20)\n",
        "          newPic.putpixel((x,y),(newR, gvalue, bvalue))\n",
        "\n",
        "  return newPic"
      ],
      "metadata": {
        "id": "peXqXT_jCcX-"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Test decrease and increase red functions here\n",
        "\n"
      ],
      "metadata": {
        "id": "EYN2IJBlFcU2"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "What's the difference between these two functions?  Look at the statements in the functions.  The only difference is that in decreasing red, the red values of the pixels get multiplied by 0.75, and in increasing red, they get multiplied by 1.20.  That's it.  Multiplying by a number smaller than 1 makes the value smaller; multiplying by a value greater than 1 makes it bigger.  Define a new function, <code>changeRed</code>, that takes an image and a multiplier as parameters, and changes the red of each pixel by that amount.  It then returns the new image.  When you call the function, you can use any value for the multiplier, remembering how to get more/less red in an image.\n"
      ],
      "metadata": {
        "id": "a92yyIooCzeW"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# changeRed function gets defined here\n",
        "\n"
      ],
      "metadata": {
        "id": "FOgwUiHNzInJ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Test your changeRed function here"
      ],
      "metadata": {
        "id": "hkJcD4R2GAWi"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Changing green and blue**\n",
        "\n",
        "Sometimes we would like to change only the green, or the blue, or maybe the red and the blue.  Define new functions, <code>changeGreen</code> and <code>changeBlue</code> that work similarly to <code>changeRed</code>, but for the green and blue color channels."
      ],
      "metadata": {
        "id": "sgDLeqxCDxDQ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# define changeGreen function here\n",
        "\n",
        "\n",
        "\n",
        "# define changeBlue function here\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "Uo4Tn4TGDyJX"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Testing the change functions\n",
        "\n"
      ],
      "metadata": {
        "id": "StY4oGfNXDat"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Sunset effect**\n",
        "\n",
        "Copy the <code>sunset</code> from the reading and test it on an image."
      ],
      "metadata": {
        "id": "HUjKXo8JzhiZ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# sunset effect function\n",
        "\n"
      ],
      "metadata": {
        "id": "9uFJUIEmHzEM"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# test sunset effect\n"
      ],
      "metadata": {
        "id": "HnNMw-xIH3kq"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Define a modified version of the sunset function, call it <code>modifiedSunset</code>, of your <code>sunset</code> function so that instead of looping through all the pixels and changing their green and blue values directly, it just calls your new, generalized <code>changeGreen</code> and <code>changeBlue</code> functions. These new functions will take care of the looping and color changing. Your code in this function will take an image as a parameter.  It will then send the image into the <code>changeGreen</code> function, saving the result in a new variable.  It then sends this new image into the <code>changeBlue</code> function. saving the result in another new variable.  This second new variable then gets returned.  You should not be copying and pasting anything from the <code>sunset</code> function here.  The point is to allow the <code>changeGreen</code> and <code>changeBlue</code> functions do all the work. Test your modified function on several differnt images."
      ],
      "metadata": {
        "id": "7lmV9_roILKz"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# define modified sunset here\n",
        "\n"
      ],
      "metadata": {
        "id": "OkK4q47zJygb"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# test modified sunset here\n"
      ],
      "metadata": {
        "id": "GKcz6NZoJ1VN"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Other effects**\n",
        "\n",
        "Copying the <code>negative</code>, <code>grayscale</code>, and <code>weightedgrayscale</code> functions from the reading and test them here.  Use several different images in your testing."
      ],
      "metadata": {
        "id": "6IkSuP8CJ6NT"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# create a negative effect function\n",
        "\n",
        "\n",
        "# make grayscale effect function\n",
        "\n",
        "\n",
        "# make weighted grayscale effect function\n"
      ],
      "metadata": {
        "id": "PVDU1AsvKXP3"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# test the functions here\n",
        "\n"
      ],
      "metadata": {
        "id": "k0AwbH23Ka23"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Be creative - Write your own function**  \n",
        "\n",
        "Now it's time to be creative! Experiment with other variations of increasing or decreasing color values, or setting them all to some value. For example, you might see what happens if you increase or decrease red, green, and blue by the same amount, or if you increase blue and decrease red and green, or if you switch the red, green, and blue values. Once you find some combination that you find is appealing, write a new function to create this effect. Add a comment before the function to describe the effect you have created."
      ],
      "metadata": {
        "id": "lGhTaVgZ0AHx"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# This following code defines a function that **** describe your effect here *****\n",
        "# Define your function here\n",
        "\n"
      ],
      "metadata": {
        "id": "INL6ahJQzfXH"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Test your function here.\n",
        "\n"
      ],
      "metadata": {
        "id": "4jVEynKV8vqK"
      },
      "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.  \n",
        "\n",
        "Submit your .ipynb file on Kit."
      ],
      "metadata": {
        "id": "m8jTI0_OBDRM"
      }
    }
  ]
}