The goal of this lab assignment to allow you to practice processing and manipulating image files in Python by using the PIL library.
For this assignment, record your work in a Jupyter Notebook and upload it to the form below by 11:59 PM Friday, March 6.
To help you get started, we have provided you with a starter notebook, which you can download and then use to record your answers. To utilize the starter notebook, download it to wherever you are running Jupyter Notebook and then use the Jupyter Notebook interface to open the downloaded file.
The starter notebook is already formatted for all the various sections you need to fill out. Just look for the red instructions that indicates were you need to write text or the cyan comments that indicate where you need to write code.
For the first activity, you are to produce an image consisting of a gradient of two colors such as shown below:
To complete this activity, you will need to implement the
make_gradient_image
function described below:
def make_gradient_image(width, height, source, target):
''' Given the width and height of the target image, this function
produces a gradient image that goes from source color to target color.
'''
return None
In order to implement this function, you will need to do the following:
Create a new Image
with the specified width
and height
.
Iterate through all the pixels in the new Image
.
For each pixel, determine the pixel color value using the following formula:
weight = 1.0 - (x / width)
new_color = source*weight + target*(1 - weight)
What this does is compute a weight based on how far long we are in the
width of the image. Based on this weight, we will take a portion of
the pixel value from the source
image and take the remaining portion
from the target
image.
For instance, if we are 20%
of the way across the image, we want to
use 80%
of the source
color's value and 20%
of the target
color's value.
Store the computed color for each pixel.
Return the newly created Image
.
To test and demonstrate that your make_gradient_image
function is
working, create and display
two different images using two different
pairs of colors.
ND_BLUE = (12, 35, 64)
ND_GOLD = (211, 159, 16)
display(make_gradient_image(480, 320, ND_BLUE, ND_GOLD))
After you have implemented the make_gradient_image
function and
demonstrated it with two different images, answer the following questions:
Describe how you implemented the make_gradient_image
. In particular,
describe the overall control flow in the function and the computation you
performed to determine the color at each pixel.
Currently, the make_gradient_image
function produces a horizontal
gradient. How would you instead produce a vertical gradient?
For the second activity, you are to produce an image by blending two images
based on a provided weight
:
To complete this activity, you will need to implement the blend_images
function described below:
def blend_images(image1, image2, weight):
''' Blend the two given images using the specified weight:
new_pixel = pixel1*weight + pixel2*(1 - weight)
'''
return None
In order to implement this function, you will need to do the following:
Query the width
and height
of one of the images.
Create a new Image
with the queried width
and height
.
Iterate through all the pixels in the new Image
.
For each pixel, determine the pixel color value using the following formula:
new_pixel = pixel1*weight + pixel2*(1 - weight)
This computation should be similar to the one in make_gradient_image
.
Store the computed color for each pixel.
Return the newly created Image
.
To test and demonstrate that your blend_images
function is working,
create and display
two different blended images using two different pairs
of images from the Internet.
Remember that you can load an image from the Internet, by doing the following:
url1 = 'https://upload.wikimedia.org/wikipedia/en/e/e8/Taylor_Swift_-_Red.png'
url2 = 'https://upload.wikimedia.org/wikipedia/en/f/f6/Taylor_Swift_-_1989.png'
image1 = Image.open(BytesIO(requests.get(url1).content))
image2 = Image.open(BytesIO(requests.get(url2).content))
display(blend_images(image1, image2, 0.5))
After you have implemented the blend_images
function and demonstrated it
with two different pairs of images, answer the following questions:
Describe how you implemented the blend_images
. In particular,
describe the overall control flow in the function and the computation you
performed to determine the color at each pixel.
What happens if the two images don't have the same dimensions? What could you do to ensure that the blend operation always works even if the images are different sizes?
Once you have completed your lab, submit your Jupyter Notebook using the form: