python opencv 为图片添加alpha通道并设置透明

python opencv 为图片添加alpha通道并设置透明opencv 读取图片后通道为BGR的格式,这里做个示范将图片的左半边设置为透明效果。

import cv2
import numpy as np

img = cv2.imread("/home/shuai/Desktop/lena.jpg")

b_channel, g_channel, r_channel = cv2.split(img)

alpha_channel = np.ones(b_channel.shape, dtype=b_channel.dtype) * 255
# 最小值为0
alpha_channel[:, :int(b_channel.shape[0] / 2)] = 100

img_BGRA = cv2.merge((b_channel, g_channel, r_channel, alpha_channel))

cv2.imwrite("lena.png", img_BGRA)


原图

透明图 (100)

透明图(0)

--------------------- 
版权声明:本文为CSDN博主「HelloWorld1108」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/HelloWorld1108/article/details/88563457

你可能感兴趣的:(#,数字图像处理)