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)
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.
ahum so answers work, i can type them... well
Well actually i'm not using asarray because i'm working in opposite direction (asarry creates triplets) i have data in the form of channels. so red green and blue; my data looks like this a = [ r,r,r,r,r,r,r,r,r ...] where r are all the redvalues in a one dimensional array, in fact this channel-image is red.
b = all bleu ,and c is all green. so the format i have is not in triplets. I wonder if there is a quick combine function to generate such triplets. in fact Pil uses arrays inside arrays, i was looking at pil but if pygame or something else can show me a pick from channels is ok PIL is not a requirement; but it seams all formats use triplets; while my data doesnt contain it. Since Numpy is the most important array library as far as i know, i wonder if it can do such array operations,
its sad i cannot draw here to explain what i want i have like a, b and c
a = [123123123132]
b = [989898389398]
c = [343434343434]
d[a,b,c] = a[] + b[] + c[] >>> this isnt python code this is my thinking code
so d becomes d[[a,b,c],[a,b,c],[a,b,c], ... ..]
and d[0] = [1,9,3]
精彩评论