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:
from skimage import data import numpy as np x, y = 100, 100 background = data.imread('background.jpg') / 255. image = data.imread('image.png') / 255. background_height, background_width, background_depth = background.shape image_height, image_width, image_depth = image.shape template = np.zeros((background_height, background_width, image_depth)) template[y : y + image_height, x : x + image_width, :] = image mask = np.stack([template[:,:,3] for _ in range(3)], axis = 2) inv_mask = 1. - mask result = background[:,:,:3] * inv_mask + template[:,:,:3] * mask plt.figure(figsize = (15, 15)) plt.subplot(1, 3, 2) plt.imshow(image) plt.subplot(1, 3, 1) plt.imshow(background) plt.subplot(1, 3, 3) plt.imshow(result) plt.tight_layout() plt.show()
Please let me know if I can do something to improve computation speed