JJobsMoi
Anthropic

Batch Image Processor

Coding

Problem

Overview

In this coding exercise, you will build a batch image processing system. You are given a set of images and transformation specifications in JSON format. Your task is to apply these transformations to each image and save the results to an output directory.

This is a practical coding challenge that tests your ability to:

  • Quickly research and use unfamiliar libraries - You are expected to search documentation during the interview
  • Parse and apply configuration from JSON files
  • Handle file I/O operations efficiently
  • Optimize for performance with parallel processing

Interview Format Notes

  • Documentation search is allowed and expected - The interviewer wants to see how you research and learn new APIs quickly
  • You may use any resources except AI-generated answers
  • Common library choices: Pillow (PIL) or scikit-image - consider familiarizing yourself with one beforehand
  • The interview involves testing on small images first, then optimizing for large images within a time target

Problem Setup

You are provided with four directories:

project/
├── small_images/      # Small test images for development
│   ├── image1.png
│   ├── image2.jpg
│   └── ...
├── large_images/      # Large images for performance testing
│   ├── photo1.png
│   ├── photo2.jpg
│   └── ...
├── transformations/   # JSON files defining transformations
│   ├── transform1.json
│   ├── transform2.json
│   └── ...
└── output/            # Directory to save processed images

Helper utilities are provided to:

  • List all files in each directory
  • Generate output file paths based on input image and transformation file

Transformation Specifications

Each JSON file in the transformations/ directory contains a list of transformations to apply sequentially. There are six types of transformations:

Transformations Without Parameters

TypeDescription
grayscaleConvert image to grayscale
flip_horizontalFlip image horizontally (mirror)
flip_verticalFlip image vertically

Transformations With Parameters

TypeParameterDescription
scalefactor (float)Scale image by the given factor (e.g., 0.5 = half size, 2.0 = double size)
blurradius (int)Apply Gaussian blur with the specified radius
rotateangle (float)Rotate image by the specified angle in degrees

Example Transformation JSON

{
  "transformations": [
    { "type": "grayscale" },
    { "type": "scale", "factor": 0.5 },
    { "type": "rotate", "angle": 90 }
  ]
}

This configuration would:

  1. Convert the image to grayscale
  2. Scale it to 50% of its original size
  3. Rotate it 90 degrees counter-clockwise

Requirements

Part 1: Basic Implementation

  1. Choose an image processing library - Research and select a Python library capable of performing all six transformation types. Common choices include:

    • Pillow (PIL)
    • scikit-image
    • OpenCV
  2. Implement transformation functions - Create functions for each of the six transformation types

  3. Process images with transformations:

    • For each transformation JSON file
    • For each image in the source directory
    • Apply all transformations in the JSON file sequentially to the image
    • Save the result to the output directory using the provided path utility
  4. Test with small images - Verify correctness using the small_images/ directory before moving to large images

Part 2: Performance Optimization

After verifying correctness with small images, process the large_images/ directory. You must complete processing within a target time limit (provided during the interview).

Key considerations:

  • Image processing is CPU-intensive
  • Each image can be processed independently
  • Consider parallelization strategies

Interface

def process_images(
    image_dir: str,
    transformation_dir: str,
    output_dir: str,
    get_output_path: Callable[[str, str], str]
) -> None:
    """
    Process all images with all transformation configurations.

    Args:
        image_dir: Path to directory containing source images
        transformation_dir: Path to directory containing transformation JSON files
        output_dir: Path to directory for saving processed images
        get_output_path: Utility function that generates output path
                        given (image_path, transform_json_path)
    """
    pass

Solution

Loading editor…

Sign in to get AI feedback on your answer. Your work is saved while you do.

Sign in to evaluate