How to create a mask for an image using Pillow?

Aug 11, 2026

Leave a message

Noah Davis
Noah Davis
Noah is a product designer at Yangzhou Xinkai Textile Hotel Supplies Factory. He combines fashion and practicality, creating unique hotel supplies that are popular among many hotels and resorts.

Hey there! As a pillow supplier, I've been in the business for quite a while, and I know a thing or two about all sorts of pillow - related stuff. But today, I'm gonna switch gears a bit and talk about something different: how to create a mask for an image using Pillow.

Pillow is a really cool Python library that's super handy for image processing. Whether you're a graphic designer, a hobbyist, or just someone who wants to play around with images, Pillow can be your best friend.

What is an Image Mask?

Before we dive into the how - to, let's quickly understand what an image mask is. An image mask is like a stencil. It's a grayscale image where each pixel's intensity determines how much of the corresponding pixel in the original image will be visible. A white pixel in the mask means the corresponding pixel in the original image will be fully visible, a black pixel means it'll be completely hidden, and gray pixels will result in partial visibility.

Installing Pillow

First things first, you need to have Pillow installed. If you haven't already, you can use pip to install it. Just open up your command prompt or terminal and type:

pip install pillow

Once it's installed, you're ready to start creating masks.

Creating a Simple Mask

Let's start with a basic example. Suppose you have an image and you want to create a circular mask for it. Here's how you can do it in Python using Pillow:

from PIL import Image, ImageDraw

# Open the original image
original_image = Image.open('your_image.jpg')

# Create a new blank image for the mask
mask = Image.new('L', original_image.size, 0)

# Create a drawing context
draw = ImageDraw.Draw(mask)

# Draw a circle on the mask
center_x = original_image.width // 2
center_y = original_image.height // 2
radius = min(original_image.width, original_image.height) // 2
draw.ellipse((center_x - radius, center_y - radius, center_x + radius, center_y + radius), fill=255)

# Apply the mask to the original image
result = Image.composite(original_image, Image.new(original_image.mode, original_image.size, 0), mask)

# Save the result
result.save('masked_image.jpg')

In this code, we first open the original image. Then we create a new blank grayscale image ('L' mode) with the same size as the original image. We use ImageDraw to draw a circle on the mask. The fill=255 parameter makes the circle white, which means the corresponding pixels in the original image will be visible. Finally, we use Image.composite to apply the mask to the original image and save the result.

Using a Custom Mask

Sometimes, you might want to use a custom - shaped mask. Maybe you have a logo or a specific pattern that you want to use as a mask. Here's how you can do it:

from PIL import Image

# Open the original image
original_image = Image.open('your_image.jpg')

# Open the mask image
mask_image = Image.open('your_mask_image.png').convert('L')

# Resize the mask to match the original image size
mask_image = mask_image.resize(original_image.size)

# Apply the mask to the original image
result = Image.composite(original_image, Image.new(original_image.mode, original_image.size, 0), mask_image)

# Save the result
result.save('custom_masked_image.jpg')

In this example, we open the original image and a custom mask image. We convert the mask image to grayscale ('L' mode) and resize it to match the size of the original image. Then we apply the mask using Image.composite and save the result.

High-star Hotel Pillows factoryNatural Latex Sleeping Pillow high quality

Practical Applications

Image masks have a ton of practical applications. For example, in graphic design, you can use masks to create cool effects like fading an image into a background. In photo editing, you can use masks to selectively apply filters or adjustments to certain parts of an image.

Our Pillow Offerings

As a pillow supplier, we have a wide range of high - quality pillows to offer. Check out our Hilton Microfiber Down Pillow, which provides excellent support and comfort. If you're looking for something more natural, our Natural Latex Sleeping Pillow is a great choice. And for those who want the luxury of high - star hotel pillows, we have High - star Hotel Pillows.

Contact for Purchase

If you're interested in our pillow products, don't hesitate to reach out for a purchase negotiation. We're always ready to discuss your needs and offer the best solutions.

References

  • Pillow official documentation
  • Python programming resources for image processing
Send Inquiry
Contact usif have any question
You can either contact us via phone, email or online form below. Our specialist will contact you back shortly.
contact us