function round(x) return math.floor(x+0.5) end w, h = img.width, img.height local sum = 0 for y=0, h-1 do for x=0, w-1 do local i = img.getInt(x, y) local r = bit32.band(bit32.rshift(i, 16), 255) local g = bit32.band(bit32.rshift(i, 8), 255) local b = bit32.band(i, 255) sum = sum+r+g+b end end return "Average brightness: "..round(sum/(255*w*h*3)*100).."%"