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

前端 未结 9 2190

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

    Expanding @gat answer:

    import Image
    
    def is_grey_scale(img_path):
        img = Image.open(img_path).convert('RGB')
        w,h = img.size
        for i in range(w):
            for j in range(h):
                r,g,b = img.getpixel((i,j))
                if r != g != b: return False
        return True
    

    Basically, check every pixel to see if it is grayscale (R == G == B)

提交回复
热议问题