0
$\begingroup$

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

$\endgroup$
2

1 Answer 1

1
$\begingroup$

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.

$\endgroup$
3
  • 1
    $\begingroup$ This just gives me 1.0 every time. $\endgroup$
    – John Roper
    Nov 27, 2016 at 19:19
  • 2
    $\begingroup$ The 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. $\endgroup$
    – emu
    Nov 28, 2016 at 14:03
  • 1
    $\begingroup$ So how to I calculate image brightness from this? $\endgroup$
    – John Roper
    Nov 30, 2016 at 1:09

You must log in to answer this question.

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