errorType使用PIL python来计算灰度级

时间:2017-11-18 22:43:11

标签: python opencv python-imaging-library pillow

我正在尝试计算python上的图片灰度级 公式数学是:

:'<,'>s/case (\w*): return (\w*);/case \2: return \1;/

但是类型中的错误

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))

1 个答案:

答案 0 :(得分:1)

尝试将该行更改为

val = img.getpixel((x, y))