Home Schedule

Lab 8: Color Effects (10 pts)

Setup

  1. Locate your "CS102" folder
  2. Download lab08.zip
  3. Extract the content of the zip file to lab08 in your CS102 folder
  4. Open "lab08.py" with Thonny
  5. READ THE COMMENTS
  6. Submit lab08.py to Moodle when you are finished

Now that we are writing functions, make sure your work today (and all future assignments) follows our class Python style guidelines and you include function comments. Be careful of your indentation!

Function 1

Write a function to edit the following image (this image is already contained in the zip file above as the file apples.jpg):

The function will be named rotate_color and will take a picture as a parameter. This function will create a copy of the original image and loop over each pixel in the copy. For each pixel in the copied image, you will will swap the values of each color from red to green, green to blue, and blue to red. Once all the pixels have been edited, the function should return the edited copy of the original image.

Example Color Rotation:

(12, 255, 90) => (90, 12, 255)

Example Function:

    
    def rotate_color(picture_param):
        # copy the picture
    
        # process the pixels in the copy
    
        # return the modified copy
    

Your new image MUST be written out with the filename apples_swap.jpg.

Example Test Code

   media.writePictureTo(rotate_color(picture), 'apples_swap.jpg')

DO NOT SUBMIT THE IMAGE FOR THE ASSIGNMENT ONLY THE CODE!

Function 2

Using the same image, you will make a second function called show_red_channel and this function will take a picture as a parameter. This function will create a copy of the original image and loop over each pixel in the copy. For each pixel in the copied image, you will get the red value of that pixel and set all the other color channels to that same value. Once all the pixels have been edited, the function should return the edited copy of the original image.

Example Pixel Manipulation:

(11, 40, 255) => (11, 11, 11)

Example Function:

    
    def show_red_channel(picture_param):
        # copy the picture
    
        # process the pixels in the copy
    
        # return the modified copy
    

Your new image MUST be written out with the filename red_channel.jpg.

Example Test Code

   media.writePictureTo(show_red_channel(picture), 'red_channel.jpg')

DO NOT SUBMIT THE IMAGE FOR THE ASSIGNMENT ONLY THE CODE!

Function 3

Copy the function below into your lab08.py file:

    
    ## EXPLAIN THIS
    def mystery_function(picture):
        new_pic = media.duplicatePicture(picture)
            
        for x in range(0, 100, 1):
            for y in range(0, 20, 1):
                pixel = media.getPixel(new_pic, x, y)
                media.setColor(pixel, media.blue)
            
        return new_pic
    

Run this function on your apple picture using the following code:

   media.writePictureTo(mystery_function(picture), 'mystery.jpg')

In the comment above the function (where it currently says EXPLAIN THIS), write an explanation AS A COMMENT describing what this function does.

DO NOT SUBMIT THE IMAGE FOR THE ASSIGNMENT ONLY THE CODE!

Useful Functions:

You may want to use media.show(picture) or media.explore(picture) to help, but these should NOT appear in the final code submitted.