This is basically the same question that was posted here: How to merge a transparent png image with another image using PIL but using with scikit-image instead of PIL. I mean to paste the png keeping its transparency on top of a background image. Also, if there is actually a way of doing it, I would like to know which one is faster (PIL or scikit-image). Thanks.
Advertisement
Answer
Inspired by user8190410’s answer, I built my own function to do it:
JavaScript
x
26
26
1
from skimage import data
2
import numpy as np
3
4
x, y = 100, 100
5
background = data.imread('background.jpg') / 255.
6
image = data.imread('image.png') / 255.
7
8
background_height, background_width, background_depth = background.shape
9
image_height, image_width, image_depth = image.shape
10
11
template = np.zeros((background_height, background_width, image_depth))
12
template[y : y + image_height, x : x + image_width, :] = image
13
14
mask = np.stack([template[:,:,3] for _ in range(3)], axis = 2)
15
inv_mask = 1. - mask
16
result = background[:,:,:3] * inv_mask + template[:,:,:3] * mask
17
plt.figure(figsize = (15, 15))
18
plt.subplot(1, 3, 2)
19
plt.imshow(image)
20
plt.subplot(1, 3, 1)
21
plt.imshow(background)
22
plt.subplot(1, 3, 3)
23
plt.imshow(result)
24
plt.tight_layout()
25
plt.show()
26
Please let me know if I can do something to improve computation speed