2
$\begingroup$

I want to know the easiest way to export rendered images from blender to be exported as numpy array. PS: I don't want to save the images first in *.jpg or *.png and then access them. I want to directly export the rendered images as numpy array.

$\endgroup$
3
  • 1
    $\begingroup$ ...a numpy array made of what values? $\endgroup$
    – m.ardito
    Mar 23, 2017 at 11:40
  • $\begingroup$ Each element of the array would correspond to a pixel value of rendered image. $\endgroup$
    – Vaibhav
    Mar 31, 2017 at 7:59
  • $\begingroup$ is is possible to render to any kind of array? $\endgroup$ Mar 24, 2020 at 23:29

1 Answer 1

-2
$\begingroup$

Numpy array isn't a native output option.

Notice up at the top of the screen, when you click Render,
it specifies Render Image
Render
If you want it to directly output to a non-standard container,
you'll have to modify the source code to accomplish that.
http://download.blender.org/source/
...
You can convert standard arrays into a numpy array like this:
https://stackoverflow.com/a/4562455/3342050
So if you don't mind running render, then using a script to export:

# path2numpy = '/home/me/.local/lib/python3/dist-packages/numpy/'   ##  linux
# path2numpy = 'C:\\Users\\me\\AppData\\Local\\Python\\Lib\\site-packages\\numpy\\'   ##  win

import sys
sys.path.append(path2numpy)

import numpy as np
array = []

# render = bpy.ops.render.render(layer="RenderLayer", scene="Scene")
# render = bpy.data.node_groups['Compositing Nodetree'].nodes['Render Layers'].layer

array.append(render)

np_array = np.array(array)

The render output data is most likely going to be found in one of those commented render lines.
You'll have to decide which one suits your needs.

You may have to iterate through the data to get it into the necessary format.
Something along the lines of:

for h in range(height):
  for w in range(width):
    R = render[h][w * bpp]
    G = render[h][w * bpp + 1]
    B = render[h][w * bpp + 2]
    data.append((R,G,B))
  ''.join(data)
  array.append(data)

Then convert regular array to numpy array, as above.
That's all dependent upon how you want that array set up though...

$\endgroup$
3
  • 3
    $\begingroup$ You wouldn't have to alter the source code, a blender add-on could do this. $\endgroup$ Mar 23, 2017 at 12:14
  • $\begingroup$ Add-ons don't bypass image generation. $\endgroup$ Mar 23, 2017 at 13:17
  • $\begingroup$ Why would they want to? 'Image generation' would be part of the process of generating the values that the add-on could then export, otherwise there's nothing to export. $\endgroup$ Mar 23, 2017 at 14:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .