How to use Pillow to work with bitmap images?

Jan 12, 2026

Leave a message

How to Use Pillow to Work with Bitmap Images

Bitmap images are ubiquitous in our digital lives, whether for photography, graphic design, or web development. Pillow, a powerful Python Imaging Library (PIL), offers a wide range of capabilities for working with bitmap images. As a pillow supplier, we understand the importance of both physical pillows and the software library Pillow. In this blog post, we'll explore how you can use Pillow to manipulate bitmap images effectively.

Installation and Setup

Before you can start using Pillow, you need to install it. The simplest way to do this is using pip, the Python package installer. Open your command prompt or terminal and run the following command:

pip install pillow

Once installed, you can import Pillow in your Python script using the following import statement:

from PIL import Image

Opening and Displaying Bitmap Images

The first step in working with any bitmap image is to open it. Pillow makes this incredibly easy. You can use the Image.open() function to open an image file. Here's an example:

from PIL import Image

# Open an image file
image = Image.open('example.jpg')

# Display the image
image.show()

In this example, we open an image file named example.jpg and then display it using the show() method. The show() method opens the image in your default image viewer.

Getting Image Information

Once you have opened an image, you can retrieve various information about it, such as its size, format, and mode. Here's how you can do it:

from PIL import Image

image = Image.open('example.jpg')

# Get the size of the image
width, height = image.size

# Get the format of the image
image_format = image.format

# Get the mode of the image
image_mode = image.mode

print(f"Size: {width}x{height}")
print(f"Format: {image_format}")
print(f"Mode: {image_mode}")

The size attribute returns a tuple containing the width and height of the image. The format attribute returns the file format of the image, such as JPEG or PNG. The mode attribute returns the pixel format of the image, such as RGB or RGBA.

Resizing and Cropping Images

One of the most common tasks when working with bitmap images is resizing and cropping. Pillow provides easy-to-use methods for both purposes.

Standard Cotton Pillow Cases bestStandard Cotton Pillow Cases manufacturers

Resizing Images

To resize an image, you can use the resize() method. Here's an example:

from PIL import Image

image = Image.open('example.jpg')

# Resize the image to a specific width and height
new_width = 200
new_height = 300
resized_image = image.resize((new_width, new_height))

# Save the resized image
resized_image.save('resized_example.jpg')

In this example, we resize the image to a width of 200 pixels and a height of 300 pixels. We then save the resized image as resized_example.jpg.

Cropping Images

To crop an image, you can use the crop() method. The crop() method takes a tuple of four coordinates (left, upper, right, lower) that define the boundaries of the crop. Here's an example:

from PIL import Image

image = Image.open('example.jpg')

# Define the crop boundaries
left = 50
upper = 50
right = 200
lower = 200
cropped_image = image.crop((left, upper, right, lower))

# Save the cropped image
cropped_image.save('cropped_example.jpg')

In this example, we crop the image to a rectangle starting at (50, 50) and ending at (200, 200). We then save the cropped image as cropped_example.jpg.

Image Processing and Enhancement

Pillow also provides a variety of methods for image processing and enhancement, such as converting an image to grayscale, applying filters, and adjusting the brightness and contrast.

Converting an Image to Grayscale

To convert an image to grayscale, you can use the convert() method with the mode set to 'L' (for luminance). Here's an example:

from PIL import Image

image = Image.open('example.jpg')

# Convert the image to grayscale
grayscale_image = image.convert('L')

# Save the grayscale image
grayscale_image.save('grayscale_example.jpg')

In this example, we convert the image to grayscale and save it as grayscale_example.jpg.

Applying Filters

Pillow provides a number of filters that you can apply to an image. For example, you can apply a blur effect using the BLUR filter. Here's an example:

from PIL import Image, ImageFilter

image = Image.open('example.jpg')

# Apply a blur filter
blurred_image = image.filter(ImageFilter.BLUR)

# Save the blurred image
blurred_image.save('blurred_example.jpg')

In this example, we apply a blur filter to the image and save the result as blurred_example.jpg.

Adjusting Brightness and Contrast

You can adjust the brightness and contrast of an image using the ImageEnhance module. Here's an example of adjusting the brightness:

from PIL import Image, ImageEnhance

image = Image.open('example.jpg')

# Increase the brightness by a factor of 1.5
enhancer = ImageEnhance.Brightness(image)
brightened_image = enhancer.enhance(1.5)

# Save the brightened image
brightened_image.save('brightened_example.jpg')

In this example, we increase the brightness of the image by a factor of 1.5 and save the result as brightened_example.jpg.

Conclusion

As you can see, Pillow is a versatile and powerful library for working with bitmap images. Whether you need to open, display, resize, crop, process, or enhance images, Pillow provides all the necessary tools.

As a pillow supplier, we not only offer high - quality Standard Cotton Pillow Cases and White Duck Down Pillows, but we also understand the value of digital image processing in today's market. Our products are perfect for various needs, from home use to commercial settings. And if you are interested in decorative pillows, we also have Pillow Cases for Throw Pillows available.

If you are interested in purchasing our pillows or have any questions about our products, we encourage you to reach out to us for a procurement discussion. We are committed to providing the best products and services to meet your needs.

References

  • Pillow Documentation: Python Imaging Library Handbook
  • Online Python Coding Communities and Tutorials
  • Digital Image Processing Textbooks

Send Inquiry