I'm trying to blend using linear lighting, in Python. However, I want to blend with the background image at 50% opacity, like how you change the opacity in Photoshop.
This is my code:
import cv2
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
# # Decrease opacity of background
img = Image.open('industrial_storage.png')
# img.putalpha(127) # Half alpha; alpha argument must be an int
# img.save('industrial_storage.png')
# read image and convert to float in range 0 to 1
detail = cv2.imread('Detail.png', cv2.IMREAD_UNCHANGED).astype("float32") / 255.0
detail = cv2.cvtColor(detail, cv2.COLOR_BGR2BGRA)
background = cv2.imread('industrial_storage.png', cv2.IMREAD_UNCHANGED).astype("float32") / 255.0
# apply linear light backgrounding
linear_light = (background > 0.5) * (detail + 2*(background-0.5)) + (background <= 0.5) * (detail + 2*background - 1)
result = (255 * linear_light).clip(0, 255).astype(np.uint8)
# save results
cv2.imwrite('result.png', result)
In this code, I have used the formula for linear light blending as given on https://www.deepskycolors.com/archivo/2010/04/21/formulas-for-Photoshop-blending-modes.html. I have also created alpha channels for both images. The background image has alpha channel of 127 for all pixels (half opacity). However, the alpha blending does not working. The code gives me the result:




cv2.addWeightedmethod is used for blending images. Since you want 50% opacity it can be accomplished by:The final image is: