float rgba list -> Image

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

float rgba list -> Image

Ingo Randolf
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



_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: float rgba list -> Image

Charles Cazabon-21
Ingo Randolf <[hidden email]> 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.]
>
> the RGBA-values are from 0. - 1.
>
> is there a clever way to make a PIL-Image out of something like this?

PIL.Image.fromstring() is probably your best bet.

Charles
--
------------------------------------------------------------------
Charles Cazabon                   <[hidden email]>
Software, consulting, and services available at http://pyropus.ca/
------------------------------------------------------------------
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: float rgba list -> Image

Charlie Clark-6
In reply to this post by Ingo Randolf
Am 31.08.2011, 16:21 Uhr, schrieb Ingo Randolf <[hidden email]>:

> 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...

You can turn that list into a list of tuples for each pixel like this

new_array = []
for idx in range(0, len(l), 4)
        new_array.append(tuple(l[idx:idx+4]))

You can probably wrap this in some functional programming so that you are  
not using memory for transitional data structures.

Charlie
--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Helmholtzstr. 20
Düsseldorf
D- 40215
Tel: +49-211-600-3657
Mobile: +49-178-782-6226
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: float rgba list -> Image

Christoph Gohlke
In reply to this post by Ingo Randolf


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  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: float rgba list -> Image

Chris Barker
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 - [hidden email]
> 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

[hidden email]
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: float rgba list -> Image

Ingo Randolf
Hello.

Thanks for all the answers...

i get the RGBA-list from within blender... loading an image or movie is stored in blender in this format and is accessible via python...
my idea (still) is to have a bridge from blender image-representation to PIL. this would enable to make image-transformations from within blender...

the problem i have right now, is that i want to align all the images in a movie in a taxture-atlas...  i think this could be done easily with PIL...

it seems reading pixel-values is very slow in blender though...

right now i have troubles getting numpy working in blenders bundled python (3.2)... so i could not try this proposal...

making tuples out of the list (either with or without value-scaling) is too slow...


ingo




Am 31.08.2011 um 20:51 schrieb Chris.Barker:

> 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

_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: float rgba list -> Image

Chris Barker
On 9/2/11 12:33 AM, Ingo Randolf wrote:
> i get the RGBA-list from within blender...

ouch! I'm still amazed when folks write wrappers that use lists of
numbers for large arrays of data -- it's simple NOT the way to go --
painfully slow and memory inefficient. This is what the PEP 3118 buffer
protocol is for -- I know it's new, but I sure wish folks would use it.

Does Blender not provide a way to get some sort of efficient image
object? in this case, you may even get better performance writing an
image to file, and reading it back in with PIL.

> right now i have troubles getting numpy working in blenders bundled python (3.2)... so i could not try this proposal...

what was the problem? a simple "setup.py install" should work fine.

> making tuples out of the list (either with or without value-scaling) is too slow...

did you try making an array.array object? That's in the stdlib at least,
though the scaling will be slower than numpy

-Chris


>
> ingo
>
>
>
>
> Am 31.08.2011 um 20:51 schrieb Chris.Barker:
>
>> 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
>
> _______________________________________________
> Image-SIG maillist  -  [hidden email]
> 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

[hidden email]
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig