How to check whether a jpeg image is color or gray scale using only Python stdlib

前端 未结 9 2200

I have to write a test case in python to check whether a jpg image is in color or grayscale. Can anyone please let me know if there is any way to do it with out installing e

9条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-24 13:12

    As you are probably correct, OpenCV may be an overkill for this task but it should be okay to use Python Image Library (PIL) for this. The following should work for you:

    import Image
    im = Image.open("lena.jpg")
    

    EDIT As pointed out by Mark and JRicardo000, you may iterate over each pixel. You could also make use of the im.split() function here.

提交回复
热议问题