在Python中,您可以使用OpenCV。如果您的系統中沒有它,請使用instructions to install OpenCV in Python。我認爲你可以對其他庫執行相同的操作,程序將是相同的,訣竅在於反轉蒙版並將其應用於某個背景,您將獲得蒙版圖像和蒙版背景,然後將兩者結合起來。
image1是用原始蒙版蒙版的圖像,image2是用倒轉蒙版蒙版的背景圖像,image3是合成圖像。 重要。 image1,image2和image3必須具有相同的大小和類型。蒙版必須是灰度。 
import cv2
import numpy as np
# opencv loads the image in BGR, convert it to RGB
img = cv2.cvtColor(cv2.imread('E:\\FOTOS\\opencv\\iT5q1.png'),
cv2.COLOR_BGR2RGB)
# load mask and make sure is black&white
_, mask = cv2.threshold(cv2.imread('E:\\FOTOS\\opencv\\SH9jL.png', 0),
0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# load background (could be an image too)
bk = np.full(img.shape, 255, dtype=np.uint8) # white bk, same size and type of image
bk = cv2.rectangle(bk, (0, 0), (int(img.shape[1]/2), int(img.shape[0]/2)), 0, -1) # rectangles
bk = cv2.rectangle(bk, (int(img.shape[1]/2), int(img.shape[0]/2)), (img.shape[1], img.shape[0]), 0, -1)
# get masked foreground
fg_masked = cv2.bitwise_and(img, img, mask=mask)
# get masked background, mask must be inverted
mask = cv2.bitwise_not(mask)
bk_masked = cv2.bitwise_and(bk, bk, mask=mask)
# combine masked foreground and masked background
final = cv2.bitwise_or(fg_masked, bk_masked)
mask = cv2.bitwise_not(mask) # revert mask to original
它可能是一個想法,顯示你的結果是什麼樣的。 –