img = cv2.imread("heads.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
The image broke after togray
img = cv2.imread("heads.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
The image broke after togray
do you realize that your file has an alpha channel?
please consult docs for imread
.
you need to ask for IMREAD_UNCHANGED.
further, cvtColor canât convert to âgrayscale with alphaâ. that format isnât defined.
well, the âdesignerâ at the desk next to you might be an idiot, but you can still ârepairâ it:
im = cv2.imread("img.png",-1) # load WITH alpha
alpha = im[:,:,3] # extract A
im[alpha==0] = 255 # reset to 'opaque'
i2 = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) # there we go !
the file isnât broken. what you see (backgrounds) is normal behavior for alpha-aware programs.
cvtColor simply doesnât know how to handle alpha channels.
there is no âgrayscale with alphaâ format.
if you want to lose all color, you can cvtColor to gray, and then copy that info into the R,G,B channels, keeping the alpha channel. that will look gray, but maintain alpha.
those pixels where alpha == 0 donât matter at all. those pixels are allowed to have arbitrary color info. it is never actually visible.
the image has 4420000 pixels. 4851 pixels are not totally transparent or totally opaque. those you need to be careful with.