Reputation:
I would like to create a composition node for Blender, which receives e. g. the RenderLayers node's output and iterates over its pixels (if I'm correct, its output is a color value for every pixel) and counts their average color value. Is it possible? More specifically, is it possible for an output color value at a certain position to access color values of an input at any other position that its own?
Upvotes: 0
Views: 1384
Reputation: 7079
Pynodes are meant to allow developers to write an entire custom node tree, it isn't meant to allow adding one custom node into an existing material or compositing node tree. It is known to have issues with automatic updating, this may have changed as I haven't looked for a while but sverchok and animation nodes still take care of updates themselves.
In general nodes are designed to be small items working on one result for the current pixel being calculated. While some built-in nodes can look at the surrounding pixels I don't expect it to be possible from pynodes.
I would suggest a script that only accesses a complete image not a custom pynode. This could be incorporated into an operator that can be used on the current image in the UV/Image editor.
Blender's Image class contains a pixels property that provides access to the raw image data.
import bpy
src_image = bpy.data.images['src']
w, h = src_image.size[0], src_image.size[1]
sum_r = 0
sum_g = 0
sum_b = 0
sum_a = 0
for x in range(w):
for y in range(h):
idx = ((y * w) + x) * src_image.channels
sum_r += src_image.pixels[idx]
sum_g += src_image.pixels[idx+1]
sum_b += src_image.pixels[idx+2]
sum_a += src_image.pixels[idx+3]
pix_count = len(src_image.pixels) / src_image.channels
avg_r = sum_r / pix_count
avg_g = sum_g / pix_count
avg_b = sum_b / pix_count
avg_a = sum_a / pix_count
print('red:{} green:{} blue:{} alpha:{}'.format(avg_r,avg_g,avg_b,avg_a))
Upvotes: 0