[Image-SIG] float rgba list -> Image

Chris.Barker Chris.Barker at noaa.gov
Wed Aug 31 20:51:24 CEST 2011


On 8/31/11 9:27 AM, Christoph Gohlke wrote:
> On 8/31/2011 7:21 AM, Ingo Randolf wrote:
>> 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.]

I can't help but wonder why it's in that data structure to begin with -- 
as you've seen, it's a pretty painfully inefficient way to do it.

>> i played around with putdata...

you need it in a binary format that is compatible:

> Consider using numpy <http://numpy.scipy.org/> and Image.fromarray.

this is probably the best way, but you can also use an array.array 
object -- create an array.array of type byte from your list, then pass 
that in to putdata. (details left as an exercise for the reader)

I think working with numbers in Python without numpy would be like 
working with text using lists of characters, rather than the string object.

-Chris




  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 - Image-SIG at python.org
> http://mail.python.org/mailman/listinfo/image-sig


-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov


More information about the Image-SIG mailing list