Access to Image.pixels very slow

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

jkoops
Posts: 3
Joined: Fri Nov 02, 2012 4:10 pm
Location: Amsterdam

Access to Image.pixels very slow

Postby jkoops » Fri Nov 02, 2012 4:24 pm

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: Select all

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

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Fri Nov 02, 2012 9:56 pm

make a copy of the pixels, see my last post in this thread:

http://www.blender.org/forum/viewtopic. ... hlight=tga

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

See: http://blenderartists.org/forum/showthr ... ge.save_as

But you may also look into save_as function:
http://www.blender.org/documentation/bl ... ge.save_as
I'm sitting, waiting, wishing, building Blender in superstition...

jkoops
Posts: 3
Joined: Fri Nov 02, 2012 4:10 pm
Location: Amsterdam

Postby jkoops » Mon Nov 05, 2012 10:00 am

Thanks, that helps!

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

Code: Select all

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


where f is your output-file.


Return to “Python”

Who is online

Users browsing this forum: Yahoo [Bot] and 0 guests