errorType 使用 PIL python 计算灰度
errorType using PIL python to calcul gray level
我正在尝试计算 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
尝试将该行更改为
val = img.getpixel((x, y))
我正在尝试计算 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
尝试将该行更改为
val = img.getpixel((x, y))