9
$\begingroup$

This script in the Python console:

>>> pixels = D.images[-1].pixels
>>> len(pixels)
8294400

>>> for i in range(64):
...     dummy = pixels[i]
...
>>>

takes over two seconds to run the for loop.

However, running pixels = [0 for i in range(len(pixels))] before the for loop causes the script to finish instantly.

What's going on with the Blender Python image pixel access, and how can I speed it up?

$\endgroup$
  • 1
    $\begingroup$ I just tested CoDEmanX's code in blender2.71 and this does not work. pixels=img.pixels[:] returns a tuple which is not editable. I had to use list(img.pixels) to do some pixel tweaking. $\endgroup$ – Greg K. Sep 12 '14 at 13:41
8
$\begingroup$

.pixels is a python object and pretty slow, especially if you change pixels. That is 'cause the entire buffer / python object is re-created for every single change (if you do it in a python loop). Not sure why it's that slow on read-only actions though...

You can speed it up tremendously if you cast it to a native python type (tuple) and use that copy instead:

img = bpy.data.images['Untitled']

pixels = img.pixels[:] # create a copy (tuple) for read-only access
# -- OR --
pixels = list(img.pixels) # create an editable copy (list)

# Use the tuple object, which is way faster than direct access to Image.pixels
for i in range(0, len(pixels), 4):
    pixels[i] = 1.0 - pixels[i] # invert red channel

# Write back to image.
# Slice notation here means to replace in-place, not sure if it's faster...
img.pixels[:] = pixels

# Should probably update image
img.update()
$\endgroup$
  • $\begingroup$ Thanks. I was casting with list(img.pixels) for temporary speedup, but this looks better. As a side note, would random access to this pixels = img.pixels[:] be decently fast? $\endgroup$ – wchargin Nov 3 '13 at 23:51
  • $\begingroup$ You can use a tuple copy if you need to read pixel values only, and cast to list if it needs to be editable. Both variants are very fast, and writing a modified copy back to Image.pixels is performing well too. Only direct access to .pixels is extremely slow. $\endgroup$ – CoDEmanX Jul 20 '15 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.