Search code examples
pythonimage-processingopencv

Find If Image Is Bright Or Dark


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.


Solution

  • 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))