How to use Pillow to perform image inpainting on an image?

Nov 10, 2025

Leave a message

Image inpainting is a powerful technique in the field of image processing, which aims to fill in missing or damaged parts of an image seamlessly. Pillow, a well - known Python library for image manipulation, can be effectively used to perform image inpainting. As a Pillow supplier, I am excited to share with you how to use Pillow for this purpose.

Understanding Image Inpainting

Before diving into the implementation, it's essential to understand what image inpainting is. Image inpainting is used in various scenarios, such as removing unwanted objects from an image, restoring old and damaged photos, or filling in areas obscured by text or other elements. The goal is to make the inpainted area blend in with the surrounding pixels so that the final image looks natural.

Prerequisites

To start using Pillow for image inpainting, you need to have Python installed on your system. You can then install Pillow using pip:

pip install pillow

Loading and Pre - processing the Image

The first step in any image processing task is to load the image. Pillow provides a simple way to do this. Here is an example of loading an image:

from PIL import Image

# Load the image
image_path = 'your_image.jpg'
image = Image.open(image_path)

# Convert the image to RGB mode if it's not already
if image.mode != 'RGB':
    image = image.convert('RGB')

In this code, we first import the Image module from Pillow. Then we open the image using the open method. It's a good practice to convert the image to RGB mode, as some operations may require it.

Defining the Inpainting Area

To perform inpainting, we need to define the area in the image that we want to fill. This can be done by creating a binary mask where the pixels in the area to be inpainted are set to 255 (white), and the rest are set to 0 (black).

import numpy as np

# Create a blank mask of the same size as the image
mask = Image.new('L', image.size, 0)

# Define the area to be inpainted (for example, a rectangle)
x1, y1, x2, y2 = 100, 100, 200, 200
draw = ImageDraw.Draw(mask)
draw.rectangle((x1, y1, x2, y2), fill = 255)

In this code, we first create a new grayscale image (mode='L') of the same size as the original image and fill it with 0. Then we use the ImageDraw module to draw a rectangle on the mask, which represents the area to be inpainted.

Simple Inpainting Technique: Average Neighboring Pixels

One of the simplest inpainting techniques is to fill the inpainted area with the average color of the neighboring pixels. Here is how you can implement it using Pillow:

from PIL import ImageDraw

# Convert the image and mask to numpy arrays
image_array = np.array(image)
mask_array = np.array(mask)

# Get the coordinates of the inpainted area
inpaint_coords = np.where(mask_array == 255)

# Iterate over the inpainted area
for x, y in zip(*inpaint_coords):
    # Get the neighboring pixels
    neighbors = []
    for dx in [-1, 0, 1]:
        for dy in [-1, 0, 1]:
            if dx == 0 and dy == 0:
                continue
            nx, ny = x + dx, y + dy
            if 0 <= nx < image_array.shape[0] and 0 <= ny < image_array.shape[1] and mask_array[nx, ny] == 0:
                neighbors.append(image_array[nx, ny])

    # Calculate the average color of the neighbors
    if neighbors:
        average_color = np.mean(neighbors, axis = 0).astype(int)
        image_array[x, y] = average_color

# Convert the numpy array back to an image
inpainted_image = Image.fromarray(image_array)

In this code, we first convert the image and mask to numpy arrays. Then we find the coordinates of the inpainted area using np.where. For each pixel in the inpainted area, we find its neighboring pixels that are not in the inpainted area. We calculate the average color of these neighbors and set the color of the inpainted pixel to this average color. Finally, we convert the numpy array back to an image.

Advanced Inpainting with Pillow and Other Libraries

While the simple inpainting technique can work for some cases, for more complex scenarios, you may want to use more advanced algorithms. One such algorithm is the fast marching method, which can be implemented using the scikit - image library in combination with Pillow.

Memory Foam PillowHousehold Pillow

from skimage.restoration import inpaint
import numpy as np

# Convert the image and mask to numpy arrays
image_array = np.array(image)
mask_array = np.array(mask)

# Perform inpainting using the fast marching method
inpainted_array = inpaint.inpaint_fmm(image_array, mask_array)

# Convert the numpy array back to an image
inpainted_image = Image.fromarray((inpainted_array * 255).astype(np.uint8))

In this code, we use the inpaint_fmm function from scikit - image to perform inpainting. This function uses the fast marching method to fill the inpainted area. After inpainting, we convert the numpy array back to an image.

Saving the Inpainted Image

Once you have the inpainted image, you can save it to a file.

# Save the inpainted image
output_path = 'inpainted_image.jpg'
inpainted_image.save(output_path)

This code saves the inpainted image to the specified file path.

Applications of Image Inpainting with Pillow

Image inpainting using Pillow has a wide range of applications. In the field of photography, it can be used to remove unwanted objects such as people, signs, or dust spots from an image. In the restoration of historical photos, it can help to repair damaged areas and bring back the original beauty of the photo. In the field of computer graphics, it can be used to create seamless textures or fill in missing parts of 3D models.

Our Pillow Products

As a Pillow supplier, we not only provide the knowledge of using Pillow for image inpainting but also offer a variety of high - quality Household Pillow and Memory Foam Pillow. Our household pillows are designed to provide maximum comfort during sleep, while our memory foam pillows conform to the shape of your head and neck, reducing pressure points and promoting better sleep.

Contact Us for Purchase and Collaboration

If you are interested in our Pillow products or have any questions about using Pillow for image inpainting, we welcome you to contact us for purchase and collaboration. We are committed to providing you with the best products and services.

References

  • Pillow official documentation
  • Scikit - image documentation
  • Image processing textbooks on image inpainting algorithms

Send Inquiry