Skip to main content

Section 26.3 Combining Pictures

A common use of color replacement technology is “green scren” background replacement. An actor performs in front of a green wall or a Zoom meeting participant sits in front of a green sheet. A computer algorithm then looks for green pixels and replaces them with pixels from some other image. The result is that the person looks like they are in some other location. (Unless of course, they have a green shirt on, in which case it will look like their disembodied head is floating in the background.)
We are going to put the woman shown below into the beach scene:
First up, we need to have an isGreen function to identify which pixels are part of the green background. It will be very similar to the isRed from the last page, only we want to verify that the green is greater than the red or blue values.

Checkpoint 26.3.1.

Write code to return True if the green value is at least 20 greater than both the red and blue values. Otherwise, return False
Once you get that working, you can copy and paste it into the program below. It will load the two pictures, with the woman stored as img1 and the beach as img2. It then goes through each pixel in img1 and asks isGreen about it. If the answer is true, that pixel gets replaced with the pixel from img2 that is at the same x, y.

Checkpoint 26.3.2.

Add your isGreen function to this program.
You have attempted of activities on this page.