How can I read pixel data from an image?

I would like to create a script, where I take an image. For example a 32 x 32 png and get the color data of each pixel, so that I can use it to build an object cube by cube. Very much like translating a pixel image into a 3d object, where each pixel is a cube. This does not seem overly complex, but I’m not sure how I would organize and script this. Could someone please point me in the right direction.


D = bpy.data


>>> type(D.textures[0].image.pixels)
<class 'bpy_prop_array'>


>>> len(D.textures[0].image.pixels)
4161600

If you plan to operate alot on the pixels of an image, make sure to use a copy:

pixels = Image.pixels[:]

otherwise access will be too slow!

every entry in the pixel array is a float value (usually ranging from 0.0 to 1.0), and it’s organized RGBA (1st pixel red, 2nd green, 3rd blue, 4th alpha, 5th red, 6th green…)

shoudl this be faster to work directly on image with PIL may be
and also have more functions available ?

thanks

Thank you, that helps. I was away for a little while so I could not respond earlier.