I want to do a test for image brightness in the rendered image (
bpy.data.images["Render Result"]). I would like to be able to see if the image is dark or light. An example output of the code could be
The rendered image is: light
You can access the pixel data directly through the
Image.pixels property. It is a list of repeated RGBA values. To access a single pixel, use this:
img = bpy.data.images["Render Result"] value = img.pixels[(y * img.width + x) * 4 + channel]
0 for red,
1 for green,
2 for blue and
3 for transparency.
Now, image brightness is not a clearly defined thing. You can, for example, calculate the average pixel value over the whole image:
r, g, b = [sum(img.pixels[:][i::4]) / len(img.pixels) for i in range(3)]
That was "average" as in "arithmetic mean". Another possible interpretation of image brightness is a quantile, say, the 95th:
import numpy as np r, g, b = [np.quantile(img.pixels[:][i::4], 0.95) for i in range(3)]
The specific choice probably depends on the kind of images you are processing.