Download and unzip the provided code. Let’s write a competitor to Zoom. Our first client is the cardinal you will find in cardinal.jpg. Our cardinal is not totally happy with his image; he’d like to be facing right rather than left, and he’d like a virtual background to appear behind him (SnowyBaker.jpg).
I’ve gotten you started with some code in imagine.py. Go read and run that code now. I’ve written the part that loads the image from a file and displays it on the screen. I’ve also written a function set_pixel()
that sets the color of a pixel at location x, y in the image to the specified r, g, b values. get_pixel
will get the r, g, and b values at a location x, y, and return them as a list. Play with those functions a bit now by calling them on different pixels to see how they work.
Write the function swap_pixels(image, x1, y1, x2, y2)
that swaps the color values of the pixel at (x1, y1) with the color values of the pixel at (x2, y2). I recommend you test it on a few sample pixels before moving on.
Write flip_row(image, row_y, width)
. This should horizontally flip every pixel in a row of the image (specified by row_y), in such a way that the pixels appear in reverse order. width
specifies the width of the image; for this example, you’ll call flip_row with the width value 400. Note that flip_row is not very useful by itself – to reflect the image so the cardinal is facing to the right, you’ll need to flip all the rows. We’ll do that next.
Write flip_horizontal(image, width, height)
. This should flip the cardinal so he faces right. Test this code before moving on.
Write virtual_background(image, background, width, height)
. This function should replace all pixels that have a green value of >.5 in image
with a corresponding pixel at the same location in background
, placing the cardinal in front of Baker. It won’t work perfectly – we’d need a true green screen for that, rather than just replacing the green leaves – but it will be good enough.
NOTE. To write this function, you’ll need to work with each pixel at each x, y location. One way to do this is to loop over all y values to work through single column of the image, and use a loop to do that for all columns.