This a simple implementation of the green screening process for images implemented in Python and MATLAB. Green Screening / the Green Screen algorithm is often used to change the background behind an object with a background of our choice. This process is widely used in applications where the background needs to be changed.
The process requires two images as it's input - foreground image and background image. The foreground image must have an object placed over a green background. The background image can be any image of our choice. These images should have the same dimensions. The output we get will have the same dimensions as well.
During the process, a new image (say output) is created. The pixels of the output image are taken depending on the following process: the algorithm checks every pixel of the foreground image for the intensity of the green color. If the intensity of the green color is beyond a certain threshold (say 240), then the output image will have the corresponding pixel taken from the background image, else the corresponding pixel is taken from the foreground image. This way the pixels are populated in the output image. This is the working of the green screen algorithm.
-
green.m: Run the process in MATLAB.
-
green.py: Run the process in Python. Requirements:
- PIL (pillow) imaging library
- Numpy
-
An image with an object over a green background as shown below: Foreground (images/fg.png):
-
Make sure both images are of the same dimensions.
Here is the output achieved with a threshold of 235:
Click here to check out my other projects

