如何仅使用Python stdlib检查jpeg图像是彩色还是灰度 [英] How to check whether a jpeg image is color or gray scale using only Python stdlib

查看:164
本文介绍了如何仅使用Python stdlib检查jpeg图像是彩色还是灰度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在python中编写一个测试用例来检查jpg图像是彩色还是灰度。任何人都可以告诉我,如果有任何方法可以安装额外的库,如opencv?

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 extra libraries like opencv?

推荐答案

扩展@gat回答:

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

基本上检查每个像素以检查它是否为灰度(R == G == B)

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

这篇关于如何仅使用Python stdlib检查jpeg图像是彩色还是灰度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆