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.
- y < 110 and y > 10 and x > 70 and x < 200
- That will select pixels in the middle of the screen (y from 10-110 and x from 70-200). We want the opposite.
- (y > 110 or y < 10) and (x < 70 or x > 200)
- That requires both the x and y to be outside the center area. It will make a "cross" of color.
- y > 110 and y < 10 and x < 70 and x > 200
- How can the y be > 110 and also be < 10? There will be no pixels that meet the criteria to be changed.
- y > 110 or y < 10 or x < 70 or x > 200
- Correct
Which of these recipes will result in just the faces being in color and the outer parts of the image being in black and white?