Re: float rgba list -> Image
Christoph Gohlke <[email protected]> Wed, 31 Aug 2011 09:27:34 -0700
| Newsgroups | gmane.comp.python.image |
|---|---|
| Message-ID | <[email protected]> |
On 8/31/2011 7:21 AM, Ingo Randolf wrote: > Hi all. > > I have a list with float-values describing an image like this: > [R, G, B, A, R, G, B, A, R, G, B, A, R, G, B, A, R, G, B, A, ... etc.] > > the RGBA-values are from 0. - 1. > > is there a clever way to make a PIL-Image out of something like this? > > > i played around with putdata... and came along with this, very, very slow solution: > > px = list-of-rgba-values-as-floats > img = the-image > > #make int-tuples > newdata = [(int(px[(x*4)]*255), int(px[(x*4)+1]*255), int(px[(x*4)+2]*255), int(px[(x*4)+3]*255)) for x in range( img.size[0] * img.size[1] )] > Image.putdata(newdata) > > > well i never saw a result, because it took too long for my short patience... ;) > > > i also tried to set the values pixel-wise... this is also very, very slow... > > > how to do that in a more clever way? > > thanks for any hints > inx > > Consider using numpy <http://numpy.scipy.org/> and Image.fromarray. For example: import numpy from PIL import Image shape = 256, 256, 4 px = list(numpy.random.rand(shape[0]*shape[1]*shape[2])) newdata = numpy.array(px, dtype=numpy.float32) newdata *= 255.99998474 newdata = newdata.astype(numpy.uint8) newdata.shape = shape image = Image.fromarray(newdata) Christoph _______________________________________________ Image-SIG maillist - [email protected] http://mail.python.org/mailman/listinfo/image-sig