Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I load an image with its alpha channel with skimage?

I am using an Anaconda distribution of Python with stuff like numpy, scipy, and scikit-image.

When I call:

from skimage import io
img = io.imread('myimage.png')

It ignores my alpha channel and returns the image as an array with shape (W, H, 3) instead of (W, H, 4). How can I get the alpha channel of the image?

like image 991
Luis Masuelli Avatar asked Oct 30 '22 10:10

Luis Masuelli


1 Answers

Some PNG images include alpha channel from the go. So just simple

from skimage import io
img = io.imread('myimage.png')

returns (W, H, 4).

Some times, with PNG or JPG images you don't get aRGB, you can try

import cv2
img = cv2.imread('myimage.png')
img_a = cv2.cvtColor(img, cv2.COLOR_RGB2RGBA)
print(img_a.shape) #(W, H, 4)

As in both skimage.io and cv2.imread return a <class 'numpy.ndarray'> you can use both interchangeably for any operation.

like image 159
ASHu2 Avatar answered Nov 14 '22 01:11

ASHu2