Вот пример наложения одного изображения на другое с использованием маски с Python / OpenCV.
Изображение футболки:
изображение рисунка:
изображение маски футболки:
- Read the 3 images and get shapes
- Convert the mask to gray and binarize
- Resize the pattern so that its smallest dimension is the size of the largest dimension of the tshirt image
- Crop it to exactly the same size as the tshirt image
- Apply the mask to the pattern image
- Apply the inverse mask to the tshirt image
- Add the two together
- Save the results
import cv2
import numpy as np
# read shirt image and get its max dimension
img = cv2.imread('tshirt.jpg')
hh, ww = img.shape[:2]
maxwh = max(hh, ww)
# read pattern image and get its size and minimum dimension
pattern = cv2.imread('tshirt_pattern.jpg')
ht, wd = pattern.shape[:2]
minwh = min(ht,wd)
# read shirt mask image
maskimg = cv2.imread('tshirt_mask.png')
# convert mask to gray and binarize
maskimg = cv2.cvtColor(maskimg, cv2.COLOR_BGR2GRAY)
maskimg = cv2.threshold(maskimg, 128, 255, cv2.THRESH_BINARY)[1]
# resize pattern so minimum dimension is size of largest dimension of tshirt image
scale = maxwh/minwh
pattern_enlarge = cv2.resize(pattern, dsize=(0,0), fx=scale, fy=scale)
# limit resized pattern to size of tshirt
pattern_enlarge = pattern_enlarge[0:hh, 0:ww]
# do masked overlay
pattern_masked = cv2.bitwise_and(pattern_enlarge, pattern_enlarge, mask=maskimg)
img_masked = cv2.bitwise_and(img, img, mask=(255-maskimg))
result = cv2.add(img_masked, pattern_masked)
cv2.imshow('image', img)
cv2.imshow('pattern', pattern)
cv2.imshow('mask', maskimg)
cv2.imshow('masked pattern', pattern_masked)
cv2.imshow('masked image', img_masked)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
# save results
cv2.imwrite('tshirt_masked.jpg', img_masked)
cv2.imwrite('pattern_masked.jpg', pattern_masked)
cv2.imwrite('tshirt_pattern_overlay.jpg', result)
изображение маскированного рисунка:
изображение маскированной футболки:
изображение результата: