[Image-SIG] float rgba list -> Image
cgohlke at uci.edu
Wed Aug 31 18:27:34 CEST 2011
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 * img.size )]
> 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
Consider using numpy <http://numpy.scipy.org/> and Image.fromarray. For
from PIL import Image
shape = 256, 256, 4
px = list(numpy.random.rand(shape*shape*shape))
newdata = numpy.array(px, dtype=numpy.float32)
newdata *= 255.99998474
newdata = newdata.astype(numpy.uint8)
newdata.shape = shape
image = Image.fromarray(newdata)
More information about the Image-SIG