OpenCV or PIL Image? Which one is better for PyTorch?

OpenCV loads images as BGR by default, which needs extra care when using pre-trained models in RGB.

OpenCV has a lot of image processing functionality, which may be useful in data augmentation.

Apart from this, is there a performance (speed) difference between OpenCV and PIL Image?

Which image handling library would you recommend for PyTorch?

There seem to be some benchmarks showing a better performance for OpenCV, while others show some use cases where PIL is the winner (or Pillow-SIMD).
Pillow-SIMD is a drop-in replacement for PIL using SIMD operations.

You can find some benchmarks here, but as always with benchmarks I would be skeptical.

Personally, I would recommend to use PIL, since it’s supported by torchvision which makes data augmentation and processing quite easy.

1 Like