Files
2024-09-14 21:55:46 +08:00

18 lines
537 B
Python

import cv2 as cv
# read image
img=cv.imread("opencv_logo.png",cv.IMREAD_UNCHANGED)
# convert 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()
# compress and save
cv.imwrite("img.png",img_rgba,[cv.IMWRITE_PNG_COMPRESSION,9])