我正在运行的代码是,但我想这是因为我在x32位上运行它,你知道如何运行它吗?你知道吗
from PIL import Image
import numpy as np
from functools import reduce
import matplotlib.pyplot as plt
import time
from statistics import mean
def threshold(imgArray):
balanceAr = []
newArray = imgArray
for eachRow in imgArray:
for eachPix in eachRow:
avgNum = reduce(lambda x, y:x+y,eachPix[:3])/len(eachPix[:3])
balanceAr.append(avgNum)
balance = (lambda x, y:x+y,balanceAr[:3])/len(balanceAr[:3])
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('test.png')
iar = np.array(i)
threshold(iar)
fig = plt.figure()
ax1 = plt.subplot2grid((8,6),(0,0),rowspan=4,colspan=3)
ax1.imshow(iar)
plt.show()
我得到的错误是
第13行,入口 avgNum=reduce(λx,y:x+y,eachPix[:3])/len(eachPix[:3]) 索引器错误:标量变量的索引无效。你知道吗
好吧,我不得不用opencv代替,它工作正常,但不是很准确
相关问题 更多 >
编程相关推荐