J.Treutlein
J.Treutlein

Reputation: 983

Python: Apply sRGB colour profile and alpha channel to image

I have been having trouble trying to save a png image with sRGB and an alpha channel. I first crop an image and then save it like this:

from PIL import Image
import cv2

inputPath = 'picture.png'
img = cv2.imread(inputPath)

crop_img = img[bounds[3]:bounds[2], bounds[1]:bounds[0]]
pth = name + ".png"
crop_img.save(pth)

This however creates a file like this:

I want the file to be like this though:

How can I get this result in python?

P.S. The original image does have an alpha channel and sRGB colour profile.

Any help is greatly appreciated!

Upvotes: 1

Views: 1718

Answers (1)

Ishara Dayarathna
Ishara Dayarathna

Reputation: 3601

You can read your image as follows. It will load your image as such including the alpha channel.

img = cv2.imread(inputPath,-1)

UPDATE

Following code is equivalent to the above given answer since cv2.IMREAD_UNCHANGED=-1 in documentation. Though above snippet solve the issue, it is not a good programming practice to use it since it doesn't give the idea about what is really -1 does. But following code snippet gives an explicit idea about the behavior of the code.

img = cv2.imread(input,cv2.IMREAD_UNCHANGED)

Upvotes: 3

Related Questions