Files
open-cv-experiment/test/2024,9,10/set_alpha.py
T
2024-09-10 14:44:04 +08:00

18 lines
493 B
Python

import cv2 as cv
# read image
img=cv.imread("opencv_logo.png",cv.IMREAD_UNCHANGED)
# concert to rgba
img_rgba=cv.cvtColor(img,cv.COLOR_BGR2BGRA)
# split alpha channel
blue,green,red,alpha = cv.split(img_rgba)
#set alpha channel
alpha[int(alpha.shape[0]/2):alpha.shape[0],int(alpha.shape[1]/2):alpha.shape[1]]=128
# remix alpha to img_rgba
img_rgba=cv.merge((blue,green,red,alpha))
# show
cv.imshow("changed",img_rgba)
cv.waitKey(0)
cv.destroyAllWindows()
# save
cv.imwrite("img.png",img_rgba)