Runtime Warning overflow encountered in ubyte_scalars

from PIL import Image
import numpy as np 
import matplotlib.pyplot as plt

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('images/numbers/0.1.png')
iar = np.asarray(i)
3iar = threshold(iar)

i2 = Image.open('images/numbers/y0.4.png')
iar2 = np.asarray(i2)
#iar2 = threshold(iar2)

i3 = Image.open('images/numbers/y0.5.png')
iar3 = np.asarray(i3)
#iar3 = threshold(iar3)

i4 = Image.open('images/sentdex.png') 
iar4 = np.asarray(i4)
#iar4 = threshold(iar4)

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()

The error I'm getting:

Warning (from warnings module):
  File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\Lib\idlelib\MuditPracticals\Image_Recognition\imagerec.py", line 11
    avgNum = reduce(lambda x, y: x + y, eachPix[:3]) / len(eachPix[:3])
RuntimeWarning: overflow encountered in ubyte_scalars

Warning (from warnings module):
  File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\Lib\idlelib\MuditPracticals\Image_Recognition\imagerec.py", line 16
    if reduce(lambda x, y: x + y, eachPix[:3]) / len(eachPix[:3]) > balance:
RuntimeWarning: overflow encountered in ubyte_scalars

Traceback (most recent call last):
  File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\Lib\idlelib\MuditPracticals\Image_Recognition\imagerec.py", line 47, in <module>
    threshold(iar3)
  File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\Lib\idlelib\MuditPracticals\Image_Recognition\imagerec.py", line 17, in threshold
    eachPix[0] = 255
ValueError: assignment destination is read-only

Answers


Regarding the Overflow RuntimeWarnings:

You shouldn't worry about these, what they are essentially telling you is that the range for the uint_8 (unsigned integer) type defined by numpy and generally used for image files, has simply exceeded its acceptable range.

From the link supplied, uint_8 types have a range of:

Unsigned integer (0 to 255)

numpy just issues the warning to inform you of the overflow. Thankfully, it will automatically adjust the result into a value of acceptable range.

For example:

from PIL import Image
import numpy as np

img = Image.open("/path/to/image.png")
img_array = np.asarray(img) # array values are of type uint_8 (!)

print img_array[0][0] # prints [ 12,  21,  56, 255] 

uint8_1 = img_array[0][0][3] # = 255
uint8_2 = img_array[0][0][2] # = 56

uint8_3 = uint8_1 + uint8_2 

# When executed raises a RuntimeWarning of overflow ubyte_scalars
# But! The result 'rolls over' to the acceptable range. So,
print uint8_3  # prints 55

Regarding the actual Error:

Your error ValueError: assignment destination is read-only is actually raised when assigning values to your numpy array newAr. It's informative, what it tells you is that the array is read only; the contents are read only: you can access them but cannot modify them.

So an action like this:

# using img_array from previous snippet.
img_array[0][0][0] = 200 

Will raise a ValueError.

Thankfully, this is easily bypassed by setting a flag parameter for the array:

# using img_array from the previous snippet
# make it writeable 
img_array.setflags(write=True)

# Values of img_array[0][0] are, as before: [ 12,  21,  56, 255]
# changing the values for your array is possible now!
img_array[0][0][0] = 200

print img_array[0][0] # prints [ 200,  21,  56, 255]

Suppressing Overflow RuntimeWarnings:

Final note: You could always suppress/ignore these warnings, even though this is generally not the best idea. (A couple of warnings in the console are annoying but they give you a clearer view of things)

To do this, just add the following after importing numpy:

import numpy as np
np.seterr(over='ignore')

i1=Image.open('images/numbers/0.1.png') iar1=np.array(i1)

rather than asarray methode use array


Try replacing y:x+y with y:int(x)+int(y)


Need Your Help

Custom DropdownlistFor cannot be Validated with jQuery.Validate()

asp.net-mvc jquery-validate html-helper html-select html.dropdownlistfor

I created a custom helper method containing a DropdownlistFor method. When I select the -1. index, the other Dropdownlists can directly be validated. On the other hand, the custom Dropdownlist is not

Specify a parameter for using files inside a Jar

java properties jar embedded-resource

Say log.properties is in the test.jar. I would like to specify the file parameter using some file inside the jar, is this doable?