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
Asked
Active
Viewed 3,000 times
0
John Roper
- 695
- 11
- 22
-
maybe related: http://blender.stackexchange.com/questions/66503/how-to-measure-the-dynamic-range-of-an-hdri – Nov 27 '16 at 17:54
-
1In relation to what? – p2or Nov 27 '16 at 20:18
1 Answers
1
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]
where channel is 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.
emu
- 197
- 6
-
1
-
2The maximum value is often 1, and it means that at least one pixel of the whole is bright. If you want a more specific measure of brightness, be more specific in your question. – emu Nov 28 '16 at 14:03
-
1