我试图在python上计算图片的灰度
公式数学为:
ndg = red*0.299+green*0.587+blue*0.114
但是类型错误
TypeError: 'JpegImageFile' object is not subscriptable
代码开始于
from PIL import Image
img = Image.open("C://Users/shous/Desktop/houssem.jpg")
pix = img.load()
cols,rows = img.size
def ndg(img,rows,cols):
mat = [[0 for x in range(cols)] for y in range(rows)]
for x in range(cols):
for y in range(rows):
mat[x][y] = 0
for x in range(cols):
for y in range(rows):
val = img[x, y] # <<== error type
mat[x][y] = val[2]*0.299+val[1]*0.587+val[0]*0.114
print(mat[x][y])
return mat
print('mat ',ndg(img,rows,cols))
错误消息为:
File "C:/Users/shous/PycharmProjects/Compar/Compare.py", line 145, in <module>
val = img[x, y]
TypeError: 'JpegImageFile' object is not subscriptable