尝试使用python2.7.8在x32上运行x64计算机的代码

2024-05-14 23:49:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在运行的代码是,但我想这是因为我在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]) 索引器错误:标量变量的索引无效。你知道吗


Tags: lambdainfromimageimportreduceforlen
1条回答
网友
1楼 · 发布于 2024-05-14 23:49:09

好吧,我不得不用opencv代替,它工作正常,但不是很准确

import cv2

img = cv2.imread('test.png')

grayscaled = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
retval, threshold = cv2.threshold(grayscaled, 45, 255, cv2.THRESH_BINARY)

cv2.imshow('threshold',threshold)
cv2.imwrite("11.png", threshold)
cv2.waitKey(0)
cv2.destroyAllWindows()

相关问题 更多 >

    热门问题