Если я не использую функцию '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