In [1]:
import numpy as np
from PIL import Image
In [2]:
src = np.array(Image.open('data/src/lena.jpg'))
mask = np.array(Image.open('data/src/horse_r.png').resize(src.shape[1::-1], Image.BILINEAR))
In [3]:
print(mask.dtype, mask.min(), mask.max())
In [4]:
mask = mask / 255
In [5]:
print(mask.dtype, mask.min(), mask.max())
In [6]:
dst = src * mask
In [7]:
Image.fromarray(dst.astype(np.uint8)).save('data/dst/numpy_image_mask.jpg')
In [8]:
mask = np.array(Image.open('data/src/horse_r.png').convert('L').resize(src.shape[1::-1], Image.BILINEAR))
In [9]:
print(mask.shape)
In [10]:
mask = mask / 255
In [11]:
# dst = src * mask
# ValueError: operands could not be broadcast together with shapes (225,400,3) (225,400)
In [12]:
# mask = mask[:, :, np.newaxis]
In [13]:
mask = mask.reshape(*mask.shape, 1)
In [14]:
print(mask.shape)
In [15]:
dst = src * mask
In [16]:
Image.fromarray(dst.astype(np.uint8)).save('data/dst/numpy_image_mask_l.jpg')