# Get a full numeric z-depth from a rendered image

I know this issue has been mentioned before but I looked at all similar questions and did not find my exact answer. So here is my problem:

I need to get a full depth map of my render. I mean the distance of all pixels from camera. Answers usually leads to the point that we get a grey level image which far pixels are black and close ones are white, which is not what I'm looking for. Because it does some normalizations and the RGB has some limitations which decrease the accuracy. What I need is to save all Z values directly to a .csv file (or .EXR but I don't know much about it and .csv works for me) that keeps the Z value of all pixels.

Is it simply possible? Or I should a script for it? If an script is needed what is the function to get the Z pass of x,y pixel?

Thanks

You can obtain depth values by plugin the depths calculated at render time to a "viewer node" in the compositor.

First make sure the rendering uses z (should be set by default):

Setup the viewer node in the compositor:

Render the image (F12).

Now you can have data from Python (principles only below):

img=bpy.data.images['Viewer Node'] # get the image corresponding to the viewer node

img.size # the image size width=img.size[0] height=img.size[1]

img.pixels # raw vector of pixels representing the depths. You'll have to reshape it to obtain a matrix, using numpy or by other means


What's remaining is to write it to a file (which is no more related to Blender).

Hope that helps.