Search code examples
pythonmatplotlibimage-processingscikit-imagebrightness

Why does multiplying an image, loaded using scikit-image, by 0.5 show it brighter in matplotlib?


I simply want to darken an image that I've loaded using scikit-image in python.

If I do this:

test_image = io.imread("TestProvs2.bmp")

test_image = test_image * 0.5

show_map(test_image)

The array of RGB values does indeed get scaled down, so that each RGB value is 127.5.

Yet the resulting image has maximum values (left = original, right = new): enter image description here


Solution

  • When you read an image, the data type of your image is uint8. When you multiply it by 0.5, Python casts it to float64 but an image must be uint8, so it gives this error

    Lossy conversion from float64 to uint8. Range [0.0, 127.5]. Convert image to uint8 prior to saving to suppress this warning.
    

    What you have to do is to cast it to uint8 manually.

    test_image = (test_image * 0.5).astype(np.uint8)
    

    Don't forget to import numpy

    import numpy as np
    

    Generally, it's better to use OpenCV for image processing.