KatInterview

Parallel Image Pipeline Without External Libraries

Arrays & HashingModel answer
MediumModel answerArrays & Hashingimage-processingparallel-processingarray-manipulation

Last updated

Build a CPU-based image transformation engine that chains grayscale, brightness, and resize operations across multiple images using pure Python.

Problem

You will implement an in-memory image transformation system using only Python standard library features—no OpenCV, PIL, or NumPy. Images are represented as three-dimensional nested lists with shape (height, width, 3), where each pixel holds [R, G, B] integer values in the range [0, 255].

Your BatchImageProcessor class must support three core transformations and a batch execution method:

1. to_grayscale(image)

Convert an RGB image to grayscale while preserving the three-channel structure. Output a pixel with three identical channels [Y, Y, Y], where luminance Y is computed as:

Y = int(0.299 × R + 0.587 × G + 0.114 × B)

2. adjust_brightness(image, factor)

Scale every RGB channel by the given floating-point factor, then clamp results to [0, 255]. Convert scaled values to integers by truncation.

3. resize(image, new_h, new_w)

Resize an image using nearest-neighbor interpolation. Map each output coordinate (r, c) to the source coordinate:

source_row = int(r × original_height / new_h)
source_col = int(c × original_width / new_w)

4. process_batch(images, pipeline, max_workers)

Apply a sequence of transformations to a list of images. The pipeline parameter is a list of tuples describing operations:

  • ("grayscale",) — convert to grayscale
  • ("brightness", factor) — adjust brightness by factor
  • ("resize", new_h, new_w) — resize to new dimensions

When max_workers > 1, parallelize processing across images.

Interface

class BatchImageProcessor:
    @staticmethod
    def to_grayscale(image: list[list[list[int]]]) -> list[list[list[int]]]:
        pass

    @staticmethod
    def adjust_brightness(image: list[list[list[int]]], factor: float) -> list[list[list[int]]]:
        pass

    @staticmethod
    def resize(image: list[list[list[int]]], new_h: int, new_w: int) -> list[list[list[int]]]:
        pass

    def process_batch(
        self,
        images: list[list[list[list[int]]]],
        pipeline: list[tuple],
        max_workers: int = 1
    ) -> list[list[list[list[int]]]]:
        pass

Example Usage

processor = BatchImageProcessor()

# 2×2 RGB image
img = [
    [[100, 150, 200], [50, 50, 50]],
    [[0, 255, 0], [255, 255, 255]]
]

# Scale brightness by 1.5
bright = processor.adjust_brightness(img, 1.5)
# Top-left: [150, 225, 255] (200×1.5=300 clamped to 255)

# Shrink to 1×1
tiny = processor.resize(img, 1, 1)
# Result: [[[100, 150, 200]]]

# Chain operations
batch = [img]
pipeline = [("brightness", 0.8), ("grayscale",), ("resize", 4, 4)]
result = processor.process_batch(batch, pipeline, max_workers=2)

Constraints

  • 1 ≤ height, width ≤ 2000
  • 1 ≤ batch size ≤ 100
  • factor ≥ 0.0
  • new_h, new_w ≥ 1
  • All input images have valid RGB pixel data

Hints

Hint 1

Each transformation is independent—treat them as pure functions that build new images without mutating inputs

Hint 2

Images in a batch are completely independent, making this an embarrassingly parallel problem suitable for multiprocessing

Hint 3

Use integer division for resize mapping to avoid floating-point precision issues that could cause out-of-bounds access