Python 新->;索引器:索引3超出大小为3的轴0的界限

Python 新->;索引器:索引3超出大小为3的轴0的界限,python,image,python-2.7,numpy,matplotlib,Python,Image,Python 2.7,Numpy,Matplotlib,如果我没有使用函数“threshold(iar3)”,代码工作得很好,但是在我添加函数的那一刻,代码出现了一个错误,但是代码还行(我猜) 代码是: from PIL import Image from matplotlib import pyplot as plt import numpy as np import time np.seterr(over='ignore') def threshold(imageArray): balanceAr = [] newAr = imageArray

如果我没有使用函数“threshold(iar3)”,代码工作得很好,但是在我添加函数的那一刻,代码出现了一个错误,但是代码还行(我猜)

代码是:

from PIL import Image
from matplotlib import pyplot as plt
import numpy as np
import time
np.seterr(over='ignore')
def threshold(imageArray):
balanceAr = []
newAr = imageArray
for eachRow in imageArray:
    for eachPix in eachRow:
        avgNum = reduce(lambda x, y: x + y, eachPix[:3]) / len(eachPix[:3])
        balanceAr.append(avgNum)
balance = reduce(lambda x, y: x + y, balanceAr) / len(balanceAr)
for eachRow in newAr:
    for eachPix in eachRow:
        if reduce(lambda x, y: x + y, eachPix[:3]) / len(eachPix[:3]) > balance:
            eachPix[0] = 255
            eachPix[1] = 255
            eachPix[2] = 255
            eachPix[3] = 255

        else:
            eachPix[0] = 0
            eachPix[1] = 0
            eachPix[2] = 0
            eachPix[3] = 255
return newAr
i=Image.open(open('images/User.1.10.jpg','rb'))
iar=np.array(i)
i2=Image.open(open('images/User.1.9.jpg','rb'))
iar2=np.array(i2)
i3=Image.open(open('images/User.1.11.jpg','rb'))
iar3=np.array(i3)
i4=Image.open(open('images/User.1.12.jpg','rb'))
iar4=np.array(i4)
threshold(iar3)
fig=plt.figure()
ax1=plt.subplot2grid((8,6),(0,0),rowspan=4,colspan=3)
ax2=plt.subplot2grid((8,6),(4,0),rowspan=4,colspan=3)
ax3=plt.subplot2grid((8,6),(0,3),rowspan=4,colspan=3)
ax4=plt.subplot2grid((8,6),(4,3),rowspan=4,colspan=3)
ax1.imshow(iar)
ax2.imshow(iar2)
ax3.imshow(iar3)
ax4.imshow(iar4)
plt.show()
我有个问题:

Traceback (most recent call last):
File "C:\Users\Lucas Diehl\Desktop\matplotlibgg.py", line 44, in <module>
threshold(iar3)
File "C:\Users\Lucas Diehl\Desktop\matplotlibgg.py", line 29, in threshold
eachPix[3] = 255
IndexError: index 3 is out of bounds for axis 0 with size 3
Traceback (most recent call last):
File "C:\Users\Lucas Diehl\Desktop\matplotlibgg.py", line 44, in <module>
threshold(iar3)
File "C:\Users\Lucas Diehl\Desktop\matplotlibgg.py", line 29, in threshold
eachPix[3] = 255
IndexError: index 3 is out of bounds for axis 0 with size 3
回溯(最近一次呼叫最后一次):
文件“C:\Users\Lucas Diehl\Desktop\matplotlibgg.py”,第44行,在
阈值(iar3)
文件“C:\Users\Lucas Diehl\Desktop\matplotlibgg.py”,第29行,在阈值中
每个pIx[3]=255
索引器:索引3超出大小为3的轴0的界限
回溯(最近一次呼叫最后一次):
文件“C:\Users\Lucas Diehl\Desktop\matplotlibgg.py”,第44行,在
阈值(iar3)
文件“C:\Users\Lucas Diehl\Desktop\matplotlibgg.py”,第29行,在阈值中
每个pIx[3]=255
索引器:索引3超出大小为3的轴0的界限

标题中的“新->”是什么?您的代码假定为RGBA数据,但您的像素只是RGBTy人,已解析!商标资本化;拼写;语法;降噪;布局。