This is a sample program that paints the transparent part of the image white. When I looked it up on the net, I found some ways to scan pixels with a for statement and to combine alpha channels after getting RGB. I was able to implement it in a few lines without doing such complicated processing, so I will leave it in the article as a memo for myself.
Paint the following transparent image in black (because you cannot see the change if it is white).
-*- coding:utf-8 -*-
import cv2
import numpy as np
# Read the input image (also read the alpha channel by specifying -1)
img = cv2.imread("kangaru.png ", -1)
# Get the index where the alpha channel is 0
ex) ([0, 1, 3, 3, ...],[2, 4, 55, 66, ...])
# It is a tuple (length 2) in which column and row are stored respectively.
index = np.where(img[:, :, 3] == 0)
# Paint white
img[index] = [255, 255, 255, 255]
# output
cv2.imwrite("output.png ", img)
You could easily acquire the alpha channel and paint the transparent part white.
Recommended Posts