Thoughts on Programming

July 18, 2011

Flood Fill

Filed under: Python — shadiyya @ 11:10 am

Flood filling means that a group of connected pixels with close values is filled with, or is set to, a certain value. The flood filling process starts with a specified point (“seed”) and continues until it cannot find any new pixels to fill due to a large difference in pixel values. Here I am using pygame with a recursive method to fill the area. For every pixel filled, the functions analyze four neighbor pixels. So, this kind of connectivity is called 4-connectivity.

The pygame module should be imported first. The code is using large number of recursions. So we have to set the recursion limit to some large value. For that, we should import the sys module also.

We have to define a function to check whether the user pressed the quit button.  Then we have to take a point inside the polygon to specify the “seed” and fill it with the specified colour. The recursive function “floodfill” should call its four adjacent points and fill them until it reaches the boundary.

The function to implement this is:

The program should display the colour filling process until the user press the quit button. Here are some screen shots during flood filling process:


The whole program is available here:


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at

%d bloggers like this: