Ask Your Question
-1

how can I convert an image to grayscale without losing transparency?

asked 2018-03-18 17:35:31 -0600

I'm trying to convert an image to grayscale without losing any of it's transparency.

This is my code:

img = cv2.imread(some_path, 0)

Original image:

image description

After running the code:

image description

edit retag flag offensive close merge delete

Comments

img = cv2.imread(some_path, cv2.IMREAD_GRAYSCALE)

supra56 gravatar imagesupra56 ( 2018-03-18 21:30:14 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-03-19 03:28:30 -0600

berak gravatar image

you can't (at least not so easily.)

when working with computer-vision, you should avoid images with alpha in it, those only cause trouble.

what you could try is:

 # read it in "as is":
im = cv2.imread("iconx.png", -1)

# extract the alpha channel:
a:= im[:,:,3]

#use that as a mask for bitwise compositing:
im2 = cv2.bitwise_and(im,im,mask=a)

#convert *that* to grayscale
im2 = cv2.cvtColor(im2,cv2.COLOR_BGRA2GRAY)

image description

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-03-18 17:35:31 -0600

Seen: 3,514 times

Last updated: Mar 19 '18