KatInterview

Build a Batch Grayscale Thumbnailer with Worker Pools

Data Structure DesignModel answer
MediumModel answerData Structure Designpythonpillowmultiprocessing

Last updated

Design and code a Pillow-based batch tool that grayscales and shrinks images, then scale it to many large files with multiprocessing, robust error handling, bounded memory and benchmarks.

You are asked to write a batch image tool in Python using the Pillow library.

Input: a list of image file paths and an output directory.

For every image:

  1. Convert it to grayscale.
  2. Resize it to a target size, keeping the result correct (think about what "correct" means for aspect ratio, orientation and transparency).
  3. Write the result into the output directory.

What to deliver:

  • A first version that is simple and fine for a handful of small images.
  • A second version that handles a large number of large images efficiently.
  • A discussion, with code, of how you would use parallelism or concurrency (for example ProcessPoolExecutor). Explain whether it helps for this workload and why, or when it would not.
  • Your approach to error handling, memory usage and file I/O.
  • A plan for measuring performance and proving that the parallel version is actually faster.

State any assumptions you make, for example whether the target size is a bounding box or an exact output size.

Hints

Hint 1

Write a single worker function that takes a source path, a destination path and a size, never raises, and returns a small result object. The sequential and parallel versions then differ only in who calls it.

Hint 2

Think about what can make a grayscale and resize 'incorrect': EXIF rotation, images with transparency or palettes, upscaling small images, and aspect ratio distortion. Also consider how JPEG decoding can be made cheaper when you only need a small output.

Hint 3

Decide what crosses the process boundary (paths and small results, not pixels), what bounds memory (worker count, in-flight tasks, decoded image size), and what your benchmark compares (1 worker vs N workers, wall time and images per second, warm cache vs cold).