Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PIL python计算灰度的errorType_Python_Opencv_Python Imaging Library_Pillow - Fatal编程技术网

使用PIL python计算灰度的errorType

使用PIL python计算灰度的errorType,python,opencv,python-imaging-library,pillow,Python,Opencv,Python Imaging Library,Pillow,我试图在python上计算图片的灰度 公式为: ndg = red*0.299+green*0.587+blue*0.114 但是类型错误 TypeError:'JpegImageFile'对象不可订阅 代码开始于 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):

我试图在python上计算图片的灰度 公式为:

ndg = red*0.299+green*0.587+blue*0.114
但是类型错误

TypeError:'JpegImageFile'对象不可订阅

代码开始于

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))
从PIL导入图像
img=Image.open(“C://Users/shous/Desktop/houssem.jpg”)
pix=img.load()
列,行=img.size
def ndg(img、行、列):
mat=[[0表示范围内的x(cols)]表示范围内的y(行)]
对于范围内的x(cols):
对于范围内的y(行):
mat[x][y]=0
对于范围内的x(cols):
对于范围内的y(行):

val=img[x,y]#尝试将该行更改为

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