Solved – runtimeWarning: overflow encountered in ubyte_scalars warning
Posted on: March 07, 2021 by Deven
In this article, you will learn how to solve runtimeWarning: overflow encountered in ubyte_scalars warning.
Consider the code example below which throws the same warning:
from PIL import Image
import numpy as np
image1 = np.array(Image.open( "hello1.jpg" ))
image2 = np.array(Image.open( "hello2.jpg" ))
temp = image1[ 1 , 1 ]-image2[ 1 , 1 ]
the correct way of writing the above code is below:
from PIL import Image
import numpy as np
image1 = np.array(Image.open( "hello1.jpg" ))
image2 = np.array(Image.open( "hello2.jpg" ))
temp = int (image1[ 1 , 1 ])- int (image2[ 1 , 1 ])
Note: When processing an image with python, it can involve addition and subtraction between the pixel values of two images. It’s also worth noting that the pixel value of the image is of the ubyte type, and the data range of the ubyte type is 0~255
You can also completely remove this warning, which isn’t the best idea to do.
to completely remove the runtimeWarning you can just add the following after importing numpy:
import numpy as np
np.seterr(over='ignore')
Share on social media
//