There are custom libraries in python that you can use to remove the background in images.
An example is the rembg library used below:
from rembg import remove
from PIL import Image
input_path = 'input.png'
output_path = 'output.png'
input = Image.open(input_path)
output = remove(input)
output.save(output_path)
My project is to compare the background of the images.
Is it possible to save the removed background as png too?
The background is marked as zeros in the alpha (transparency channel) of
outputimage.We may extract the alpha channel, negate it
(255 - alpha), and scale the input by the negated alpha.We may have to divide by 255 for bringing alpha to range [0, 1] before scaling.
Code sample:
Input:

Output:

Negated alpha:

Note:
The above solution assumes that there may be some alpha values between 0 and 255 (some semi-transparent background pixels), and my be simplified if assumed there are only 0 and 255 values.