32-bit RGBA matrix with PIL image

Let's say I load an image as:

> image = Image.open('temp.png')

<PngImagePlugin.PngImageFile image mode=RGBA size=1200x600 at 0x112F0C488>

      

Please note that the dimensions of the image 1200x600

.

I would like to get this image as a 2D numpy array where each entry contains an integer 32 .

If I do this:

np.array(image).shape

      

I get:

(600,1200, 4)

      

How can I convert this to 32 bit RGBA 2D numpy array?

+3


source to share


1 answer


1. Indexing

You are misunderstanding how NumPy indexes images. NumPy prefers indexing strings by basic parameters (y, x, c) for images for the reasons described here :

The disadvantage of [indexing columns] is the potential performance measure. Its sharing data sequentially, either implicitly in array operations, or explicitly by looping through the image lines. When this is done, the data will be available in a suboptimal order. As the first index increases, what actually happens is that elements located far from each other in memory are accessed sequentially, usually at low memory access rates.

If you'd rather use indexing on the (x, y, c) column and don't mind the potential performance penalty, use numpy.transpose

to swap the indexes:

np.array(image).transpose((1, 0, 2))

      

But the NumPy documentation recommends you just get used to it:



We recommend that you simply learn to change the normal order of the indices when accessing array elements. It goes against the grain of course, but it is more in line with Python semantics and natural data ordering.

2. Color channels

The third axis of the array gives you 4 color channels per pixel, here the values ​​(red, green, blue, alpha). This is more useful for most applications than a single 32-bit number: for example, you can extract the alpha channel by writing image[...,3]

.

If you really want 32-bit color values, you can use the method ndarray.view

to get an idea of ​​the same data image with a different one dtype

, then use numpy.reshape

to remove the last axis (which is now redundant):

a = np.array(image)
a.view(dtype=np.uint32).reshape(a.shape[:-1])

      

+3


source







All Articles