Python 索引器?它是什么以及如何修复它

Python 索引器?它是什么以及如何修复它,python,database,image,Python,Database,Image,我是python新手,我将要展示的代码来自, 我得到这个错误: Warning (from warnings module): File "C:/Users/User/Desktop/tolong aku/thresholding logic.py", line 15 avgNum = reduce(lambda x, y: x + y, eachPix[:3])/len(eachPix[:3]) RuntimeWarning: overflow encountered in

我是python新手,我将要展示的代码来自, 我得到这个错误:

    Warning (from warnings module):
  File "C:/Users/User/Desktop/tolong aku/thresholding logic.py", line 15
    avgNum = reduce(lambda x, y: x + y, eachPix[:3])/len(eachPix[:3])
RuntimeWarning: overflow encountered in ubyte_scalars

Warning (from warnings module):
  File "C:/Users/User/Desktop/tolong aku/thresholding logic.py", line 21
    if reduce(lambda x, y: x + y, eachPix[:3])/len(eachPix[:3]) > balance:
RuntimeWarning: overflow encountered in ubyte_scalars

Traceback (most recent call last):
  File "C:/Users/User/Desktop/tolong aku/thresholding logic.py", line 41, in <module>
    iar = threshold(iar)
  File "C:/Users/User/Desktop/tolong aku/thresholding logic.py", line 25, in threshold
    eachPix[3] = 0
IndexError: index 3 is out of bounds for axis 0 with size 3

我想使图像变成灰色像素,然后收集数组结果以生成数据集。..tq。

当您尝试获取序列的元素(例如列表或元组)并使用现有元素范围之外的索引时,会出现索引错误

例如

如果尝试访问元素3或4,则会出现索引错误。只有元素t[0]、t[1]和t[2]存在

[78 84 84][172 181 181][188 196 198]…,[111 116 113][62 65 62][64 66 63]]那么,我只能使用多少个像素

意味着内部索引可以计数为2:元素[0]、[1]和[2]。
下一个索引可以从0变为(三元组的数量-1)

那么,这是否意味着数组的数据如果包含这样的元素,[[78 84][172 181 181 181][188 196 198]…,[111 116 113][62 65 62][64 66 63]],那么,我只能使用多少个像素?好的,所以我将[3]改为[2]那么,为什么输出图像只显示蓝色和白色?我可以将其更改为灰色像素吗?或者我可以保留蓝色图像以用于整个数据吗?我不知道您的颜色是如何组织的。如果[172 181 181]表示[red=172 green=181 blue=181],那么您可能应该删除最后一个赋值(
eachPix[3]=xxx
)不只是更改索引-您正在覆盖蓝色组件(或任何最后一种颜色)。只需将整个变量设置为[0],然后将另一个设置为[255],eachPix[0]=255 eachPix[1]=255 eachPix[2]=255其他:eachPix[0]=0 eachPix[1]=0 eachPix[2]=0我实际上只是python的初学者。我已经阅读了一些python basic,例如,,,非常抱歉提出这样的典型问题。非常感谢所有帮助。
# if you are on 32 bit OS:
#import Image

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import time

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] = 0

            else:
                eachPix[0] = 0
                eachPix[1] = 0
                eachPix[2] = 0
                eachPix[3] = 255

    return newAr

i = Image.open ('C:/Users/User/Desktop/tolong aku/50.0.png')
iar = np.array(i)

i2 = Image.open ('C:/Users/User/Desktop/tolong aku/50.1.png')
iar2 = np.array(i2)

iar = threshold(iar)
iar2 = threshold(iar2)

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)

ax1.imshow(iar)
ax2.imshow(iar2)

plt.show()
t = (1, 2, 3)     # has elements at indices 0, 1, and 2