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

前端 未结 9 2141

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:18

    There is more pythonic way using numpy functionality and opencv:

    import cv2
    def isgray(imgpath):
        img = cv2.imread(imgpath)
        if len(img.shape) < 3: return True
        if img.shape[2]  == 1: return True
        b,g,r = img[:,:,0], img[:,:,1], img[:,:,2]
        if (b==g).all() and (b==r).all(): return True
        return False
    

提交回复
热议问题