Fast Algorithm to Determine Average Brightness of an Image

Sannyasin Brahmanathaswami brahma at hindu.org
Tue Sep 20 18:06:13 EDT 2016


@ hh

That works!

fascinating. Though what is bright not bright, is, as you say, "not sharp" 

Images that return a value > 100 still seem bright enough to warrant darker type… 

and of course you may have e.g. very dark stone mountains (taking up 1/3 of the composition space at the bottom half of the image) with a very bright, blue clear sky… 

So "average brightness" has only so much utility in terms of choosing a color brightness for an overlay.

But, this is still very useful function.  Thank you!
 

On 9/20/16, 10:14 AM, "use-livecode on behalf of hh" <use-livecode-bounces at lists.runrev.com on behalf of hh at hyperhh.de> wrote:

    You could try the following function. It is "dirty" (the definition
    of 'brightness' is also unsharp!), but it is very fast.
    
    local ii="tmp-1.414214"
    
    -- uses the average color intensity of the pixel
    -- resulting from scaling down to a 1x1 image
    function avgBrightness theImage
      lock screen; lock messages
      if there is no img ii then create img ii
      set resizeQuality of img ii to "best"
      set width of img ii to the width of img 1
      set height of img ii to the height of img 1
      set imagedata of img ii to the imagedata of theImage
      set width of img ii to 1
      set height of img ii to 1
      put the imagedata of img ii into iData
      delete img ii
      return avg (byteToNum(byte 2 of iData), \
            byteToNum(byte 3 of iData), \
            byteToNum(byte 4 of iData))
      unlock screen; unlock messages
    end avgBrightnes



More information about the use-livecode mailing list