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["myimage"]
img.pixels[(y * img.width + x) * 4 + channel] = value

where channel is 0 for red, 1 for green, 2 for blue and 3 for transparency.

If you wanted to compute, for example, the maximum blue amount of all pixels, Python magic can help you make it in one line:

bluemax = max(img.pixels[:][2::4])

Note that blue amount is number 2 out of 4 colors in total, therefore 2::4. In order to overcome a Blender API limitation, there are extra brackets [:] that convert image data to a classical list.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.