Filling an image using Python-Pillow

Filling an Image with Python-Pillow

Seed Fill, also known as Flood Fill, is an algorithm for identifying connected paths within a well-defined, enclosed area. This algorithm has a variety of practical applications, such as:

  • Optimized Pathfinding
  • The Paint Bucket tool is a common tool found in several image processing packages that uses this algorithm internally.
  • The approach to solving a maze is to use functionals (paired with traversal algorithms such as breadth-first and depth-first, and pathfinding algorithms such as A-star and Dijkstra).
  • Use in Image Processing

There are several ways to implement this algorithm, such as –

  • Scanline Floodfill (row/column based floodfill)
  • Four/Eight Flood Control Zones
  • Threshold subtraction (only using pixels with the same value).

We will utilize the floodfill algorithm to perform image processing tasks. For this purpose, we will use the pillow library. To install the library, execute the following command in the command line:

pip install pillow

Note: Several Linux distributions often come pre-installed with Python and pillow Tutorial”>Pillow.

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

Parameters:
image – The open image object (obtained via Image.open, Image.fromarray, etc.).
seed_pos – The seed position (the coordinates of the pixel that will receive the seed value).
replace_val – The fill color (the color value to be used for replacement).
border – The optional border value (modifies the path selection based on the border color).
thresh – The optional threshold value (used to provide tolerance in the flooding, to include areas of pixels with similar values).

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

Example:

Images used:

Filling an image using Python-Pillow

# 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:

Fill the image using Python-Pillow

Explanation:

  • After importing the necessary modules for this task, we first create an image object (‘PIL.Image.Image’). This image object serves as a separate, core copy of the image file and can be used independently.
  • The seed variable is then assigned a coordinate value (the internal dimensions of the image). The coordinates are hand-picked, meaning the user should enter intentionally chosen coordinate values (the pixel coordinate values can be verified by using img.getpixel(coord)).
  • The pixel values obtained from these coordinates will be the pixels to be replaced in the image.
  • The rep_value variable is then assigned an RGB color value (yellow in this case). This value is assigned as an RGB tuple, which is specific to our particular case because our input image is in the RGB color space (img.mode == ‘RGB’).
    Note: The rep_value variable will contain a value based on the current image mode. That is, if img.mode == “L”, the rep value will not be a 3-component tuple, but an integer.
  • Then, we call the ImageDraw.floodfill() function, passing img, seed, rep_value, and thresh as parameters. Since the ImageDraw.floodfill() function modifies the passed image object in place, we don’t need to store the function’s return value (Nonetype).
  • Finally, we display the modified image using img.show() (Image.show()).

Leave a Reply

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