Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiply : The function however expects a 4-layer #26

Open
B14ckP4nd4 opened this issue Mar 25, 2023 · 1 comment
Open

multiply : The function however expects a 4-layer #26

B14ckP4nd4 opened this issue Mar 25, 2023 · 1 comment

Comments

@B14ckP4nd4
Copy link

I tried to use multiply effect and I got this error

TypeError: The blend_modes function "multiply" received a numpy array with 3 layers for its argument "img_in". The function however expects a 4-layer array representing red, green, blue, and alpha channel for this argument. Please supply a numpy array that includes all 4 layers to the "img_in" argument.

here is my code :

# Import background image
background_img_float = cv2.imread('bg.jpg', -1).astype(float)

# Import foreground image
foreground_img_float = cv2.imread('personal.jpg', -1).astype(float)


# Blend images
opacity = 0.7  # The opacity of the foreground that is blended onto the background is 70 %.
blended_img_float = multiply(background_img_float, foreground_img_float, opacity)

# Display blended image
blended_img_uint8 = blended_img_float.astype(np.uint8)  # Convert image to OpenCV native display format
cv2.imshow('window', blended_img_uint8)
cv2.waitKey()  # Press a key to close window with the image.
@Eunoia
Copy link

Eunoia commented Feb 23, 2024

This was my solution:

def add_alpha_channel(image):
    height, width = image.shape[:2]
    # Create an alpha channel with full opacity for every pixel
    alpha_channel = np.ones((height, width, 1), dtype=image.dtype) * 255
    return np.concatenate((image, alpha_channel), axis=-1)
    
background_img_float = add_alpha_channel(cv2.imread('bg.jpg', -1).astype(float))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants