Find If Image Is Bright Or Dark

Farhan R. picture Farhan R. · Sep 25, 2018 · Viewed 8.4k times · Source

I would like to know how to write a function in Python 3 using OpenCV which takes in an image and a threshold and returns either 'dark' or 'light' after heavily blurring it and reducing quality (faster the better). This might sound vague , but anything that just works will do.

Answer

SpghttCd picture SpghttCd · Sep 25, 2018

You could try this :

import imageio
import numpy as np

f = imageio.imread(filename, as_gray=True)

def img_estim(img, thrshld):
    is_light = np.mean(img) > thrshld
    return 'light' if is_light else 'dark'

print(img_estim(f, 127))