Analytics Vidhya
Published in

Analytics Vidhya

A Tip A Day — Python Tip #8: Why should we Normalize image pixel values or divide by 255? | Dev Skrol

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.

import cv2 
import matplotlib.pyplot as plt
##Read image
plt.imshow(img, cmap="gray")


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

img[17,14] Output: 

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

import seaborn as sns plt.figure(figsize=(30,30)) 
sns.heatmap(img, annot= True, cmap="binary")


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


import seaborn as sns img = img/255 plt.figure(figsize=(30,30)) 
sns.heatmap(img, annot= True, cmap="binary")


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

Originally published at on February 19, 2021.

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem

Recommended from Medium

Difference between value type and a reference type in iOS swift?

BigQuery Cursor equivalent and Dynamic SQL.

What is the difference between Quality Control and Quality Assurance?

Introducing my little app to track any product’s price on Amazon.

Inner API Gateway for Cloud With Kong

How to download audio from tumbler?

My Preparation Process for the LS 129 Written Assessment

4 outlines of mountains representing lessons 1–4, a plateau at lesson 5, and real mountains representing the assessments.

How Garbage First Garbage Collector (G1GC) affected the performance of our back-end

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Asha Ponraj

Asha Ponraj

Data Science & Machine Learning Enthusiast | Software Developer | Blogger | |

More from Medium

BIRCH- An Efficient Hierarchical Algorithm for Clustering Very Large Databases

Your Guide to Adventuring Aruba

My Graduation Speech to Journalists

Why Event Cameras Are The Next Step in Computer Vision