# Python: Check image brightness

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]


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.

• This just gives me 1.0 every time. – John Roper Nov 27 '16 at 19:19
• 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. – emu Nov 28 '16 at 14:03
• So how to I calculate image brightness from this? – John Roper Nov 30 '16 at 1:09