我在我的代码中得到这个错误可以任何一个帮助 [英] I am getting this error in my code can any one help
本文介绍了我在我的代码中得到这个错误可以任何一个帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
警告(来自警告模块):
文件D:\Python \programs\IMG PRO\im3.py,第8行
avgNum = reduce(lambda x,y:x + y,eachPix [:3])/ 3
运行时警告:ubyte_scalars遇到溢出
警告(来自警告模块):
文件D:\Python \programs\IMG PRO\im3.py ,第16行
如果减少(lambda x,y:x + y,eachPix [:3])/ len(eachPix [:3])>余额:
运行时警告:ubyte_scalars遇到溢出
我尝试过:
来自 PIL import Image
import numpy as np
import matplotlib.pyplot as plt
def bal(imageArray) :
balanceAr = []
for everyRow in imageArray:
for eachPix in eachRow:
avgNum = reduce( lambda x,y:x + y,eachPix [: 3 ])/ 3
balanceAr.append(avgNum)
balance = sum(balanceAr [ 0 :len(余额Ar)])/ len(balanceAr)
返回余额
def 阈值( imageArray,balance):
newAr = imageArray
everyRow in newAr:
for eachPix in eachRow:
if (reduce( lambda x,y:x + y,eachPix [: 3 ] )/ 3 )>余额:
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 = 0
newa = list()
while i< 3:
i = i + 1
na = raw_input( 输入图像名称)
i = Image.open(na)
iar = np.array(i)
b = bal(iar)
iar = threshold(iar,b)
newa.append(iar)
解决方案
< blockquote>这些不是错误而是警告。
他们告诉你,操作 x + y
的结果可能会溢出。您的参数类型为 uint8
(范围0到255)。添加两个这样的值时,结果可能大于255,无法存储为 uint8
。然后结果被截断为8位。
如果可以接受或不接受。
谷歌搜索错误表明你不是这个代码部分的第一个:
python - ubyte_scalars中遇到的运行时警告溢出 - Stack Overflow [ ^ ]
Warning (from warnings module):
File "D:\Python\programs\IMG PRO\im3.py", line 8
avgNum = reduce(lambda x, y: x + y, eachPix[:3])/3
RuntimeWarning: overflow encountered in ubyte_scalars
Warning (from warnings module):
File "D:\Python\programs\IMG PRO\im3.py", line 16
if reduce(lambda x, y: x + y, eachPix[:3]) / len(eachPix[:3]) > balance:
RuntimeWarning: overflow encountered in ubyte_scalars
What I have tried:
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
def bal(imageArray):
balanceAr = []
for eachRow in imageArray:
for eachPix in eachRow:
avgNum = reduce(lambda x, y: x + y, eachPix[:3])/3
balanceAr.append(avgNum)
balance = sum(balanceAr[0:len(balanceAr)]) / len(balanceAr)
return balance
def threshold(imageArray,balance):
newAr = imageArray
for eachRow in newAr:
for eachPix in eachRow:
if (reduce(lambda x, y: x + y, eachPix[:3]) / 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=0
newa=list()
while i<3:
i=i+1
na=raw_input("enter image name")
i=Image.open(na)
iar = np.array(i)
b=bal(iar)
iar = threshold(iar,b)
newa.append(iar)
解决方案
These are not errors but warnings.
They tell you that the result of the operationx + y
might overflow. Your parameters are of typeuint8
(range 0 to 255). When adding two such values the result may be greater than 255 which can't be stored asuint8
. Then the result is truncated to 8 bits.
If this can be accepted or not depends.
Googling for the error shows that you are not the first one for this code portions:
python - Runtime Warning overflow encountered in ubyte_scalars - Stack Overflow[^]
这篇关于我在我的代码中得到这个错误可以任何一个帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文