简体   繁体   中英

Blending multiple images while retaining the original colors

What I want: Done using photoshop (multiply)

blend() with alpha=0.5, also blending with the white image on which I pasted it Any workaround for my problem? The original images can be anything, just gave a reference image

Image A with a transparent background (0,255,255,255)

Image B with a transparent bg (255,0,255,255)

There are other ways to do it, but this should be quite generic and adaptable for other situations:

#!/usr/bin/env python3

from PIL import Image
import numpy as np

# Load both images and make into Numpy arrays
a=np.array(Image.open('A.png').convert('RGBA'))
b=np.array(Image.open('B.png').convert('RGBA'))

# Make masks of all opaque pixels in each image, i.e. alpha>0
mA = a[...,3] > 0
mB = b[...,3] > 0

# Make empty result image
res = np.zeros_like(a)
res[mA] = np.uint8([255,0,255,255])     # make all pixels from A magenta
res[mB] = np.uint8([0,255,255,255])     # make all pixels from B cyan
res[mA & mB] = np.uint8([0,0,255,255])  # make all pixels common to A and B blue

# Save result
Image.fromarray(res).show()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM