如何仅使用 Python stdlib 检查 jpeg 图像是彩色还是灰度 [英] How to check whether a jpeg image is color or gray scale using only Python stdlib
本文介绍了如何仅使用 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?
推荐答案
可以检查每一个像素点是否为灰度(R == G == B)
You can check every pixel to see if it is grayscale (R == G == B)
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
这篇关于如何仅使用 Python stdlib 检查 jpeg 图像是彩色还是灰度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文