Previous Thread  Next Thread

chat icon Access to Image.pixels very slow

jkoops

Posted: Fri Nov 02, 2012 4:24 pm
Joined: 02 Nov 2012
Posts: 3
Hi,

As part of a custom exporter, I'm trying to copy the pixels of a texture image to a file.

However, it seems that accessing the Image.pixel array is very slow. If I read every pixel of a 128x128 image and write it to a file, it takes around 10 seconds. For a 1024x1024 image, it seems to take forever (I didn't have the patience to wait and killed Blender).

What I'm doing is something like this (I'm new to Python):

Code:

f = open("/tmp/pixels", "wb")
img = bpy.images[0]
for i in range(0, img.size[0] * img.size[1] * 4):
    f.write(struct.pack("@f", img.pixels[i]))


This is on OS-X with Blender 2.64. Am I not supposed to use the Image.pixels array like this? I saw somewhere that it would be faster to convert it to a list first (with list(img.pixes)), but then the conversion seems to take equally long...
Reply with quote


CoDEmanX

Posted: Fri Nov 02, 2012 9:56 pm
Joined: 05 Apr 2009
Posts: 692
make a copy of the pixels, see my last post in this thread:

http://www.blender.org/forum/viewtopic.php?t=24348&highlight=tga

btw. you can also use blender functionality to save image to file.

See: http://blenderartists.org/forum/showthread.php?266356-Saving-a-newly-created-image&highlight=image.save_as

But you may also look into save_as function:
http://www.blender.org/documentation/blender_python_api_2_64_5/bpy.ops.image.html#bpy.ops.image.save_as
_________________
I'm sitting, waiting, wishing, building Blender in superstition...
Reply with quote


jkoops

Posted: Mon Nov 05, 2012 10:00 am
Joined: 02 Nov 2012
Posts: 3
Thanks, that helps!

For those reading this thread later, the solution is writing the pixels out like this:

Code:

f.write(bytes([int(pix*255) for pix in img.pixels]))


where f is your output-file.
Reply with quote


 
Jump to:  
Powered by phpBB © 2001, 2005 phpBB Group