11
$\begingroup$

This is a little odd but I need to save a screen-shot as an array (numpy array for eg) without writing to disk. The idea here is to create and then call the array once per frame in an external script so doing this...

bge.render.makeScreenshot("frame.png")

...and then converting the png takes too long to be feasible mostly due to saving the image (which is something I don't need anyway).

I'm trying to avoid digging into the rasterizer's source as it's completely foreign to me so I figured I'd ask here first. Any thoughts or suggestions would be greatly appreciated.

$\endgroup$
4
  • 3
    $\begingroup$ Look into: glReadPixels, combine with this PNG writing function, stackoverflow.com/a/19174800/432509 $\endgroup$
    – ideasman42
    Commented Nov 21, 2014 at 13:47
  • $\begingroup$ Thank you! A quick proof of concept worked perfectly. When I have a full script I'll post it as the answer in the off chance that someone else needs to do this. $\endgroup$
    – Snesticle
    Commented Nov 21, 2014 at 15:14
  • 2
    $\begingroup$ you could post the script you ended up with as an answer, I'm sure others would find interesting. $\endgroup$
    – ideasman42
    Commented Nov 21, 2014 at 15:18
  • $\begingroup$ depending on your os, you might have a memory-backed filesystem, for example the debian/ubuntu/linux hosts tend to have a '/dev/shm/' folder which can be used to write files that are backed by memory not hard drive. this is naturally going to couple your scripting to your OS, being able to get the data directly would be nicer. $\endgroup$ Commented Jan 13 at 8:23

1 Answer 1

0
$\begingroup$

That works for me:

import bpy
import numpy as np

# Take screenshot
bpy.ops.render.opengl()

# Get screenshot data
image = bpy.data.images['Viewer Node']
pixels = image.pixels

# Convert to numpy array
np_pixels = np.array(pixels[:])

# Reshape array to match image dimensions
np_pixels = np_pixels.reshape((image.size[1], image.size[0], image.channels))

# Show the numpy array of the screenshot
print(np_pixels)

It doesn't write it to disk.

First it renders it, then it takes the image from the viewer node and then converting and reshaping to a numpy array.

I hope that helps you and it is what you looking for

$\endgroup$

You must log in to answer this question.

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