Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy and pil reshape arrays

pil to numpy conversion leads to arrays like:

a = array([ [[r,g,b],[r,g,b].....[r,g,b],[r,g,b]]  ,  [[r,g,b],[r,g,b.....]] , int8)

triplets of rgb values; within rows so :

a[0] = [[r,g,b],[r,g,b].....[r,g,b],[r,g,b]] = first row

is there a quick way to convert such triplets numpy arrays back and ford to (well especialy back..)

a = [[rrrr],[rrrrr],[rrrrr],.... [bbbbb],[bbbbbb],[bbbbbb]...,[ggggg],[ggg],[ggg]]

or

like

a=[[rrr],[rrrrr],[....    ...]] **or** aa = [rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr..]

b=[[bbb],[bbbbb],[....    ...]]**or** bb = [bbbbbbbbbbbbbbbbbbbbbbbbbb..]

c=[[ggg],[ggggg],[....      .]] **or** cc = [ggggggggggggggggggggggggg..]

My problem I have a format like aa bb cc and know the image size 640x480 how to get it fast into pill format as below

a = array([ [[r,g,b],[r,g,b].....[r,g,b],[r,g,b]]  ,  [[r,g,b],[r,g,b.....]] , int8)
like image 883
user613326 Avatar asked Sep 11 '25 00:09

user613326


1 Answers

Does a.T give you what you want?

I'm assuming you've created your array using numpy's asarray function.

import Image, numpy
im = Image.open('test.png')
a = numpy.asarray(im)
R,G,B,A = a.T

The above will give you 4 separate 2D arrays, one for each band.

like image 91
Paul Avatar answered Sep 12 '25 12:09

Paul