Reputation: 51
At the moment my code takes an image in color and converts it to grayscale. The problem is that it makes the transparent pixels white or black.
This is what I have so far:
import cv2
img = cv2.imread("watch.png",cv2.IMREAD_GRAYSCALE)
cv2.imwrite("gray_watch.png",img)
Here is the pic for reference: Watch.png
Upvotes: 3
Views: 1300
Reputation: 51
I found that using only PIL can accomplish this:
from PIL import Image
image_file = Image.open("convert_image.png") # opens image
image_file = image_file.convert('LA') # converts to grayscale w/ alpha
image_file.save('output_image.png') # saves image result into new file
This preserves the transparency of the image as well. "LA" is a color mode. You can find other modes here: https://pillow.readthedocs.io/en/3.1.x/handbook/concepts.html#concept-modes
Upvotes: 2