Sub topic 5
Decoding at Reduced Size with JPEG Draft Mode
Assume you know basic Python, Pillow, and image tensors. You should also understand that an image file has a compressed representation, while a decoded image is an array of pixels.
JPEG decoding can consume more CPU time than the final resize. A camera image may contain millions of pixels even when the model needs only 224 × 224 pixels. Decoding every source pixel and then discarding most of them wastes work and memory.
JPEG supports reduced-size decoding. The decoder can produce an image at roughly one-half, one-quarter, or one-eighth of the original width and height. Pillow exposes this option through Image.draft.
How draft decoding works
Call draft after opening the JPEG but before converting or loading it. The requested size tells the decoder the approximate size you need. The decoder chooses a supported reduction level, so the result may not exactly match the requested dimensions. Resize the result afterward to obtain the model's exact input shape.
from pathlib import Path
from PIL import Image
def load_reference(path, size=(224, 224)):
with Image.open(path) as image:
image = image.convert("RGB")
return image.resize(size, Image.Resampling.BILINEAR)
def load_draft(path, size=(224, 224)):
with Image.open(path) as image:
# Request an intermediate size. JPEG chooses a supported scale.
image.draft("RGB", (size[0] * 2, size[1] * 2))
image = image.convert("RGB")
return image.resize(size, Image.Resampling.BILINEAR)
if __name__ == "__main__":
path = Path("photo.jpg")
result = load_draft(path)
print(result.mode, result.size)The final resize remains important. Draft decoding controls the decoder's work, but it does not guarantee the exact output dimensions.
What draft mode changes
| Input | `draft` effect | Required action |
|---|---|---|
| JPEG | May decode at a smaller supported scale | Resize to the exact target |
| PNG | Usually no decoder reduction | Use another optimisation |
| JPEG with an alpha requirement | JPEG has no alpha channel | Handle transparency separately |
Draft mode is most useful when the source image is much larger than the target. If the source is already close to the target size, the reduced decoder may save little time and can reduce detail before interpolation.
Accuracy and image orientation
Reduced decoding is an approximation. It can produce pixels that differ from the reference pipeline, especially around sharp edges and small objects. Whether that matters depends on the model and task. Classification may tolerate small differences, while OCR, keypoint detection, or medical imaging may not.
Apply the same colour conversion, orientation handling, crop policy, and interpolation method in both pipelines. For example, if the reference pipeline applies EXIF orientation metadata, the optimised pipeline must apply it too. Otherwise, a speed change can accidentally become a data change.
Benchmark and verify
Measure decode and resize stages separately on a representative sample. Compare average latency, not just one image. Then compare output tensors using a maximum absolute difference or a task-specific accuracy test.
A practical rollout is:
- Keep the full-resolution loader as a reference.
- Test draft decoding on JPEG images from every important source.
- Record throughput and memory use.
- Check predictions or labels on a validation subset.
- Enable draft mode only when the measured trade-off is acceptable.
Exercise
Why does draft decoding need a final resize even when the requested draft size is 224 × 224?
Show answer
JPEG supports only certain reduction ratios, so the decoder may return dimensions near the request rather than exactly 224 × 224. The final resize establishes the exact model input shape.