Basics of Image Processing in Python

Tavish Srivastava 24 Jun, 2022
4 min read

Writing today’s article was a fascinating experience for me and would also be for the readers of this blog. What’s so different? Two things: firstly the article is about something I always wanted to do since I was a 5-year old; secondly this topic / tools / algorithm are new to me as well. I am no way a master of image processing, but the utility of this field simply blew my mind.

image processing in python

Imagine, if you can create an application of auto-tagging a photograph as that of Facebook, or create your own face recognition password to your laptop. In this article, I will pick up a very simple but interesting application of Image processing. We will use Python to do the image processing.  In next few articles, I will take over more complex examples of Image Processing. Here is the problem I will be working on in this article:

Problem Statement

Guess what is this image?


You are right, it’s a typical picture of an open sky. I took this picture last to last night in Bangalore from my terrace. At that point of time, I had no clue that this can be such an exciting and rewarding exercise. As a kid, I used to spend hours counting these stars but almost always failed to go beyond 50-60. I wanted to complete this exercise using some help from my machine. I had no clue that this was even possible till last Sunday, and today I have completed this long pending task using Python.

Let’s get started

Step 1: Import the required library

Skimage package enables us to do image processing using Python. The language is extremely simple to understand but does some of the most complicated tasks. Here are a few library you need to import to get started,



Step 2 : Import the image

Once we have all the libraries in place, we need to import our image file to python. Following is the code you can use to import the image file. Note that the image is imported in grey scale, which basically means that each pixel is a shade of grey. And each pixel essentially becomes one cell in a matrix. In this case the image is matrix of 480*581 cells (or image of 480*581 pixels).

Python Code:

Step 3 : Find the number of Stars

Now comes the critical part where our major labor is done by a few commands. These few command go out for searching continuous objects in the picture. Blobs_log gives three outputs for each object found. First two are the coordinates and the third one is the area of the object. The radius of each blob/object can be estimated using this column (area of the object).


As we can see that the algorithm has estimated 308 visible stars. Let’s now look at how accurate are these readings.


Step 4 : Validated whether we captured all the stars

The number 308 is still coming out of a black box. Let’s look at whether we have spotted all the stars correctly. For this I am circling each estimated star position. And the look at the image if we are missing any star.


Here is the complete code :

[stextbox id=”grey”]
from matplotlib import pyplot as plt
from skimage import data
from skimage.feature import blob_dog, blob_log, blob_doh
from math import sqrt
from skimage.color import rgb2gray
import glob
from import imread
example_file = glob.glob(r"C:UsersTavishDesktopwint_sky.gif")[0]
im = imread(example_file, as_grey=True)
plt.imshow(im, cmap=cm.gray)
blobs_log = blob_log(im, max_sigma=30, num_sigma=10, threshold=.1)
# Compute radii in the 3rd column.
blobs_log[:, 2] = blobs_log[:, 2] * sqrt(2)
numrows = len(blobs_log)
print("Number of stars counted : " ,numrows)
fig, ax = plt.subplots(1, 1)
plt.imshow(im, cmap=cm.gray)
for blob in blobs_log:
    y, x, r = blob
    c = plt.Circle((x, y), r+5, color='lime', linewidth=2, fill=False)


End Notes

Image Processing is fascinating! I started my journey with Python Image Processing not more than 5 days. For the benefit of the community, I will encourage any suggestions or best practices to be shared on this forum. This exercise might not have any practical application but similar analysis can be done for purity estimations. For instance, in glass industry we need the number of silica particles in the glass at a microscopic level. By capturing the frames in a video, you can use this simple code to do a lot of things. For example, estimation of traffic through CCTV footage. This code can easily be tailored to achieve the same.

Did you find the article useful? If you have done similar pieces of work on Python Image processing, please share them with us. Do let us know your thoughts about this article in the box below.

If you like what you just read & want to continue your analytics learning, subscribe to our emailsfollow us on twitter or like our facebook page.

Tavish Srivastava 24 Jun, 2022

Tavish Srivastava, co-founder and Chief Strategy Officer of Analytics Vidhya, is an IIT Madras graduate and a passionate data-science professional with 8+ years of diverse experience in markets including the US, India and Singapore, domains including Digital Acquisitions, Customer Servicing and Customer Management, and industry including Retail Banking, Credit Cards and Insurance. He is fascinated by the idea of artificial intelligence inspired by human intelligence and enjoys every discussion, theory or even movie related to this idea.

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers


Rajesh 30 Dec, 2014

Hi Tavish, Very nice and creative article. THanks Rajesh

Anuj 31 Dec, 2014

Thanks a lot for sharing. I am also a beginner in the image processing world, good article to start the journey !!

Anon 31 Dec, 2014

Nice article, Tavish. When you say that "this exercise might not have any practical application", it would depend upon your definition of practical. ;-) Even while reading the article, I thought up of a possible use: the estimation of night sky darkness from a specific place. With artificial lights encroaching into a lot of places, I strongly feel that it is important that we make sure that the treasures of the night sky are 'directly' available to us. While this method may not be perfect owing to the differences in the mechanics of human and camera vision, I think it at least could be a good complement to standard methods. And in more general terms, these days machine learning and large-scale computational statistical analyses are widely prevalent in astronomy (a good example of "Big Science"). For more information see, e.g. here and here.

Richard 31 Dec, 2014

I am getting 'cm' is not defined at 'cmap=cm.gray'. Using Anaconda Python 2.7 Nice simple tutorial into CV! Thank you... Richard

Bryan 01 Jan, 2015

Great tutorial! I really like the straightforward example and clear code. The Python community could use a lot more blogs like this.

Phani 02 Jan, 2015

Nice article and simple to understand...You made it very easy:)..

kyle 03 Jan, 2015

Thanks for the info and tutorial! Really nice way to count stars! I noticed that cm.gray didn't work for me either (using py2.7, though not anaconda, on a mac). So I did the following: cm_gray = plt.get_cmap('gray') and then used that instead of cm.gray Minor thing - to see the green-circled blobs in the final image, need to add after the "for blob..." loop is done.

Anupriya 03 Jan, 2015

Wonderful article! Thanks for sharing the step by step guide! Python Imaging Library (PIL) is one of the popular libraries used for image processing. PIL can be used to display image, create thumbnails, resize, rotation, convert between file formats, contrast enhancement, filter and apply other digital image processing techniques etc. PIL supports image formats like PNG, JPEG, GIF, TIFF, BMP etc. It also possesses powerful image processing and graphics capabilities. To start with image processing first we need to download PIL and install in PC. PIL supports python version 2.1 to 2.7.

Parmod Kumar
Parmod Kumar 11 Aug, 2015

Very nice! Thanks for sharing this post!

suresh 09 Oct, 2015

can you please let me know where can I find "skimage: pkg to install ....

Abhinav 05 Nov, 2015

Hi Tavish, It is a great article, I had one question, if we were to do something like a video analysis for a retail store to see customer response to a particular add/display, would it be possible to do the same in Python? The last time I had worked on Image processing was to detect patterns in a video stream using Matlab, Also would it be possible to use the same concepts for finding patterns by invoking Python from SAS? (Am too stubborn of a SAS user actually) Regards Abhinav Mathur

Dr.G.MADHU 12 Aug, 2017

Dear Tavish, This is very good article to learners.Recently, my students also using it is very good for their basics. I had one query. I am applying this code on malaria microscopic images but i got very different results which are not validated correctly. So I need your suggestions Regards G.MADHU

Namrata 29 Oct, 2017

Hi Tavish, I am just beginning with image processing starting from your article. Great one! One thing I couldn't understand is the formula we used for calculating the radius.

Suraj 12 Nov, 2017

Is it possible in a different case to identify the dimensions of a particle? Say, a bubble, can we measure its diameter using python image processing? Please share any information if this is possible

venkatesh 27 Nov, 2017

hi i am trying to run your source code python idel 3.6.3 ,but it is showing list index out of range, can any one help for the above error.. Regards Venkatesh

Siddhant Mittal
Siddhant Mittal 11 Feb, 2018

hello, i recently started learning image processing but i am not able to understand things that clearly if any one can help in telling from where to study or from where i can arrange study material then please mail it to me my mailing id is [email protected]

Bhamidipati Srikanth
Bhamidipati Srikanth 21 Feb, 2018

Hi Tavish, Nice work. I have regarding my major project in X ray processing to predict pneumonia. If an X ray image is given as input, how can features be extracted from that? Is there any specialized thing to do that?

Malkhan Singh
Malkhan Singh 28 Feb, 2018

Thank you Sir for such interesting post, But I used openCV findContours() and drawContours(), so I got different answers for different minimum threshold values like min_thresh=10 or 9 output was 304, similarly for 8 count= 306,,7->>314,,6->>357,,5->>453,,4->>705,,3->>1160.

Natasha Sen
Natasha Sen 21 Dec, 2021

Great tutorial. Thank you for sharing it.