不知道是搜索方法的问题还是自己不愿意看文档的问题,
总之本来想在Python的标准库里面找到一个可以判断是否为灰度图的标准库函数,
但是死活找不到,当新问题的难度比旧问题难时,果断回来解决旧问题,
那就是自己编写一个,效率问题嘛,还是忽略一下,不考虑了
总而言之,哪位大神看到了,有更好的方法求评论,求告知,跪求高效的算法。
参考怎么读取每个像素的RGB
这里判断是否为灰度图的标准是:每一个像素所对应的R、G、B的值是否相等。
def is_color_image(url):
im=Image.open(url)
pix=im.convert('RGB')
width=im.size[0]
height=im.size[1]
oimage_color_type="Grey Image"
is_color=[]
for x in range(width):
for y in range(height):
r,g,b=pix.getpixel((x,y))
r=int(r)
g=int(g)
b=int(b)
if (r==g) and (g==b):
pass
else:
oimage_color_type='Color Image'
return oimage_color_type