Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert RGB PIL image to numpy array with 3 channels?

Tags:

I am loading image with the following code

image = PIL.Image.open(file_path) image = np.array(image) 

It works, but the size of array appears to be (X, X, 4), i.e. it has 4 layers. I would like normal RGB layers. Is it possible?

UPDATE

I found that just removing 4th channel is unsufficcient. The following code was required:

image = PIL.Image.open(file_path) image.thumbnail(resample_size) image = image.convert("RGB") image = np.asarray(image, dtype=np.float32) / 255 image = image[:, :, :3] 

Why?

like image 423
Dims Avatar asked Jul 06 '17 17:07

Dims


People also ask

Can an image could be converted into a NumPy array?

Using OpenCV Library imread() function is used to load the image and It also reads the given image (PIL image) in the NumPy array format. Then we need to convert the image color from BGR to RGB. imwrite() is used to save the image in the file.

Does PIL use RGB or BGR?

But for PIL, the input is RGB, while it's BGR for cv2.


1 Answers

The fourth layer is the transparency value for image formats that support transparency, like PNG. If you remove the 4th value it'll be a correct RGB image without transparency.

EDIT:

Example:

>>> import PIL.Image >>> image = PIL.Image.open('../test.png') >>> import numpy as np >>> image = np.array(image) >>> image.shape (381, 538, 4) >>> image[...,:3].shape (381, 538, 3) 
like image 137
keredson Avatar answered Oct 02 '22 18:10

keredson