Click here to Skip to main content
15,900,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

Python
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)
Posted
Updated 31-Oct-16 3:38am
v4
Comments
Suvendu Shekhar Giri 31-Oct-16 8:50am    
any luck with debugging?
Richard MacCutchan 31-Oct-16 9:03am    
You need to look at the values of all the variables in the expression that gives the error.

1 solution

These are not errors but warnings.

They tell you that the result of the operation x + y might overflow. Your parameters are of type uint8 (range 0 to 255). When adding two such values the result may be greater than 255 which can't be stored as uint8. 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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900