opencv remove png background

Solutions on MaxInterview for opencv remove png background by the best coders in the world

showing results for - "opencv remove png background"
Cian
14 Feb 2016
1#To set transparent background to white (or any other colour):
2
3import cv2
4#load image with alpha channel.  use IMREAD_UNCHANGED to ensure loading of alpha channel
5image = cv2.imread('your image', cv2.IMREAD_UNCHANGED)    
6
7#make mask of where the transparent bits are
8trans_mask = image[:,:,3] == 0
9
10#replace areas of transparency with white and not transparent
11image[trans_mask] = [255, 255, 255, 255]
12
13#new image without alpha channel...
14new_img = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR)
15
16#courtesy of @user1269942 https://stackoverflow.com/questions/53732747/set-white-background-for-a-png-instead-of-transparency-with-opencv
17