14.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 them 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.

Write code to return True if the green value is 20 or more greater than both the red and blue values. Otherwise, return False

Once you get that working, you can bopy 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 the goes through each pixel in img1 and asks isGreen about it. If so, that pixel gets replaced with the pixel from img2 that is at the same x, y.

You have attempted of activities on this page