How to use Pillow in a multi - process environment?

Dec 03, 2025

Leave a message

David Smith
David Smith
David is a senior mold designer at Suzhou Dongying Precision Mould Co., Ltd. With over 10 years of experience in precision mold production, he has played a key role in the company's independent mold development since 2009.

Hey there! I'm a supplier of Pillow, and today I'm gonna share some tips on how to use Pillow in a multi - process environment.

First off, let's understand why we might want to use Pillow in a multi - process setup. Pillow is a great Python library for image processing. But when you're dealing with a large number of images or complex image processing tasks, a single - process approach can be really slow. That's where multi - processing comes in. It allows you to split the work across multiple CPU cores, speeding up the overall processing time.

Prerequisites

Before we dive into the details, make sure you have Pillow installed. You can install it using pip:

pip install pillow

Also, you need to have a basic understanding of Python's multiprocessing module. This module provides a simple way to run multiple processes in Python.

Basic Setup

Let's start with a simple example. Suppose you have a bunch of images in a directory, and you want to resize them all. Here's how you can do it using multi - processing and Pillow.

import os
from PIL import Image
import multiprocessing


def resize_image(image_path):
    try:
        with Image.open(image_path) as img:
            new_size = (img.width // 2, img.height // 2)
            resized_img = img.resize(new_size)
            output_path = os.path.join('resized', os.path.basename(image_path))
            resized_img.save(output_path)
    except Exception as e:
        print(f"Error processing {image_path}: {e}")


if __name__ == '__main__':
    image_dir = 'images'
    image_files = [os.path.join(image_dir, f) for f in os.listdir(image_dir) if f.endswith(('.png', '.jpg', '.jpeg'))]
    if not os.path.exists('resized'):
        os.makedirs('resized')
    pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())
    pool.map(resize_image, image_files)
    pool.close()
    pool.join()
    print("All images resized successfully.")

In this code, we first define a function resize_image that takes an image path, opens the image using Pillow, resizes it, and saves the resized image in a new directory. Then, in the if __name__ == '__main__' block, we get a list of all the image files in the images directory. We create a new directory called resized to store the resized images.

We then create a multiprocessing.Pool object with the number of processes equal to the number of CPU cores available. The pool.map function applies the resize_image function to each image file in the list. Finally, we close the pool and wait for all the processes to finish using pool.close() and pool.join().

Sharing Resources

One of the challenges in a multi - process environment is sharing resources. For example, if you want to use a single Pillow Image object across multiple processes, you need to be careful.

Python's multiprocessing module has different ways to share data between processes. One common way is to use shared memory. However, when it comes to Pillow images, it's usually better to pass the image paths between processes and open the images separately in each process. This is because Pillow images are not easily serializable, which means they can't be easily shared between processes.

Error Handling

When working in a multi - process environment, error handling becomes even more important. If one process fails, it shouldn't bring down the whole operation. In the example above, we added a try - except block in the resize_image function to catch any exceptions that might occur during image processing.

17335628263059f54c4f920cf28f75e407089c30353d620250331085854

Performance Considerations

While multi - processing can significantly speed up image processing, it's not always the best solution. There's an overhead associated with creating and managing multiple processes. So, if you're dealing with a small number of images or simple tasks, a single - process approach might be faster.

Also, make sure you don't overload your system by creating too many processes. In the example above, we used multiprocessing.cpu_count() to determine the number of processes. This is a good starting point, but you might need to adjust it based on your specific system and task requirements.

Use Cases in Our Business

As a Pillow supplier, we use multi - process image processing in several ways. For example, when we receive a large order of Pillow Cover, we need to process product images for our website. We use multi - processing to resize, crop, and add watermarks to these images quickly.

Another use case is when we're working on new Pillow designs. We might need to generate multiple variations of the same design, and multi - processing helps us do this in a timely manner.

We also use multi - processing for quality control. We can process images of the finished products to check for any defects. For instance, we can use Pillow to detect if the stitching on a pillow cover is straight or if there are any stains on the pillow.

Conclusion

Using Pillow in a multi - process environment can be a powerful way to speed up image processing tasks. By splitting the work across multiple CPU cores, you can handle large volumes of images more efficiently. However, it's important to consider the overhead of multi - processing and handle errors properly.

If you're in the market for high - quality Pillow products or need help with image processing for your business, don't hesitate to reach out. We're here to help you with all your pillow - related needs and can provide you with the best solutions for your image processing requirements. Whether you're a small business or a large corporation, we have the expertise and resources to meet your needs.

References

  • Python multiprocessing documentation
  • Pillow official documentation

That's all for today's blog. I hope you found it useful. If you have any questions or comments, feel free to leave them below.

Send Inquiry