A Tip A Day – Python Tip #8: Why should we Normalize image pixel values or divide by 255?

Photo by Hari Nandakumar on Unsplash

In our previous post A Tip A Day – Python Tip #7: OpenCV – CV2: imread() and resize(), we have explored a simple image and its pixel values.
The pixel values can range from 0 to 256. Each number represents a color code.
When using the image as it is and passing through a Deep Neural Network, the computation of high numeric values may become more complex.
To reduce this we can normalize the values to range from 0 to 1.

In this way, the numbers will be small and the computation becomes easier and faster.
As the pixel values range from 0 to 256, apart from 0 the range is 255. So dividing all the values by 255 will convert it to range from 0 to 1.


lets take a look at one of the white pizel’s value.

I gave it a try to visualize the array values using seaborn heatmap and below is the result.


In the above output the values range from 0 to 256.



In this result, you can see that pixels that make the strokes in “4” image are 0.93 or 0.99 or near by values.

Hope you can understand why and how we do normalize simple images.

Lets explore more about images in upcoming posts.

More interesting Python tips:

A Tip A Day – Python Tip #6 – Pandas Merge

A Tip A Day – Python Tip #5 – Pandas Concat & Append

Best way to Impute NAN within Groups — Mean & Mode



Leave a Reply

Your email address will not be published. Required fields are marked *