使用PIL python计算灰度的errorType
我试图在python上计算图片的灰度 公式为:使用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):
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))