Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Open CV
  • scikit-image
  • pycairo
  • Pyglet
  • Python
  • Numpy
  • Pandas
  • Python Database
  • Data Science With Python
  • Machine Learning with Python
  • Django
  • Flask
  • R
Open In App
Next Article:
Python Pillow - Working with Images
Next article icon

Floodfill Image using Python-Pillow

Last Updated : 25 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Seed Fill also known as flood fill, is an algorithm used to identify connected paths in a definite enclosed region. The algorithm has an array of practical applications, such as –

  • Optimized pathfinding
  • Paint Bucket Tool a generic tool found in several image processing packages, uses the algorithm internally
  • Mazesolving uses floodfill (paired with traversing algorithms like breadth-first, depth-first and pathfinding algorithms such as A Star, Dijkstra)
  • Used in Image Processing

There are various ways in which the algorithm could be implemented such as –

  • Scanline Floodfill (row/column based floodfill)
  • Four/Eight Way Floodfill
  • Threshold less Floodfill (using only identical pixel values)

We will be utilizing floodfill algorithm in order to do image processing tasks. For this purpose, we will be using pillow library. To install the library, execute the following command in the command-line:-

pip install pillow

Note: Several Linux distributions tend to have Python and Pillow preinstalled into them

Syntax: ImageDraw.floodfill(image, seed_pos, replace_val, border-None, thresh=0) 

Parameters: 
image – Open Image Object (obtained via Image.open, Image.fromarray etc). 
seed_pos – Seed position (coordinates of the pixel from where the seed value would be obtained). 
replace_val – Fill color (the color value which would be used for replacement). 
border – Optional border value (modifies path selection according to border color) 
thresh – Optional Threshold Value (used to provide tolerance in floodfill, to incorporate similar valued pixel regions) 

Return: NoneType (modifies the image in place, rather than returning then modified image)

Example: 

Image Used:

 Sample Image 

Python3

# Importing the pillow library's
# desired modules
from PIL import Image, ImageDraw
  
# Opening the image (R prefixed to
# string in order to deal with '\'
# in paths)
img = Image.open(R"sample.png")
 
# Converting the image to RGB mode
img1 = img.convert("RGB")
 
# Coordinates of the pixel whose value
# would be used as seed
seed = (263, 70)
  
# Pixel Value which would be used for
# replacement
rep_value = (255, 255, 0)
  
# Calling the floodfill() function and
# passing it image, seed, value and
# thresh as arguments
ImageDraw.floodfill(img, seed, rep_value, thresh=50)
  
# Displaying the image
img.show()
                      
                       

Output:

 output 

Explanation:

  • After importing the necessary modules required for the task, we firstly create an image object (‘PIL.Image.Image’). This image objects acts as an separate in-core copy of the Image file, which could be used separately.
  • Then assign a coordinate value (inside dimensions of the image) for the seed variable. The Coordinates are picked manually, i.e. the user should put in the value of coordinate which is picked intentionally (the value of the pixel coordinate could be verified by using img.getpixel(coord)).
  • The pixel value obtained from these coordinates would be the one which is to be replaced inside the image.
  • Then assign rep_value variable with a RGB color value (yellow in this case). The value is being assigned as a RGB Tuple, which is specific for our particular case as our input image is of RGB color space (img.mode == ‘RGB’). 
    Note: The rep_value variable will contain value according to the Image mode of the current image, i.e. if img.mode == “L” then rep value will not be of tuple with 3 components, but rather would be of integer.
  • Then call the ImageDraw.floodfill() function by passing img, seed, rep_value and thresh as arguments. Since the ImageDraw.floodfill() function modifies the passed image object at place, we don’t need to store the return value (Nonetype) of the function.
  • In the end we display the modified image, using img.show() (Image.show()).


Next Article
Python Pillow - Working with Images

V

vasudev4
Improve
Article Tags :
  • Python
  • Python-pil
Practice Tags :
  • python

Similar Reads

  • Python | Crop image using pillow
    In this article, we will learn to crop an image using pillow library. Cropping an image means to select a rectangular region inside an image and removing everything outside the rectangle. To crop an image we make use of crop() method on image objects. Syntax : IMG.crop(box_tuple) Parameters : Image_
    1 min read
  • Python Pillow - Using Image Module
    In this article, we will see how to work with Image Module of PIL in Python. First, let's see how to install the PIL.  Installation: Linux: On the Linux terminal type the following: pip install Pillow Installing pip via terminal: sudo apt-get update sudo apt-get install python-pipWorking with Image
    5 min read
  • Python Image Editor Using Python
    You can create a simple image editor using Python by using libraries like Pillow(PIL) which will provide a wide range of image-processing capabilities. In this article, we will learn How to create a simple image editor that can perform various operations like opening an image, resizing it, blurring
    13 min read
  • Python Pillow - Image Sequences
    The ImageSequence module in the pillow contains a wrapper class that helps users to iterate over the frames of an image sequence. It can iterate over animations, gifs, etc. Iterator class This class accepts an image object as a parameter. It implements an iterator object that can be used by a user t
    3 min read
  • Python Pillow - Working with Images
    In this article, we will see how to work with images using Pillow in Python. We will discuss basic operations like creating, saving, rotating images. So let's get started discussing in detail but first, let's see how to install pillow. Installation To install this package type the below command in t
    4 min read
  • How to Concatenate image using Pillow in Python ?
    Prerequisites: Python Pillow Concatenate image means joining of two images. We can merge any image whether it has different pixels, different image formats namely, 'jpeg', 'png', 'gif', 'tiff', etc. In python, we can join two images using the Python image library also known as the pillow library. In
    3 min read
  • Python Pillow - Blur an Image
    Blurring an image is a process of reducing the level of noise in the image, and it is one of the important aspects of image processing. In this article, we will learn to blur an image using a pillow library. To blur an image we make use of some methods of ImageFilter class of this library on image o
    2 min read
  • Python Pillow - Flip and Rotate Images
    Prerequisites: Pillow Python Pillow or PIL is the Python library that provides image editing and manipulating features. The Image Module in it provides a number of functions to flip and rotate images. image.transpose() is the function used to rotate and flip images with necessary keywords as paramet
    2 min read
  • Find most used colors in image using Python
    Prerequisite: PIL PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. It was developed by Fredrik Lundh and several other contributors. Pillow is the friendly PIL fork and an easy-to-use library developed by Alex Clark and other contributors. We’l
    2 min read
  • Python - Channel Drop using Pillow
    A channel drop is a method of removing one of the channels of a multichannel image. By removing means turning the color value of a particular channel to 0 (all pixels), i.e. that particular channel doesn't have any effect on the final image (assuming colors are blended `normally`). Color theory (Col
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences