Skip to content

Instantly share code, notes, and snippets.

@jcupitt
Created August 21, 2020 00:35
Show Gist options
  • Save jcupitt/e186a3facfdae4084a72966ce6a6be16 to your computer and use it in GitHub Desktop.
Save jcupitt/e186a3facfdae4084a72966ce6a6be16 to your computer and use it in GitHub Desktop.
benchmark pyvips vs cv2 for image read
#!/usr/bin/python3
# setup with:
#
# mkdir sample
# cd sample
# for i in {1..300}; do cp ~/pics/k2.png $i.png; done
#
# on this modest 2015 laptop running Ubuntu 20.04, I see:
#
# $ ../bench-read.py *.png
# cv2: read 300 files with BGR->RGB conversion ...
# took 39.46 sec
# pyvips: read 300 files ...
# took 37.10 sec
import sys
import pyvips
import cv2
import time
import numpy as np
files = sys.argv[1:]
start_time = time.time()
d = []
print(f'cv2: read {len(files)} files with BGR->RGB conversion ...')
for f in files:
b = cv2.imread(f)
b = cv2.cvtColor(b, cv2.COLOR_BGR2RGB)
d.append(b)
print(f' took {time.time() - start_time:.2f} sec')
start_time = time.time()
d = []
print(f'pyvips: read {len(files)} files ...')
for f in files:
c = pyvips.Image.new_from_file(f, access='sequential')
c = np.ndarray(buffer=c.write_to_memory(),
dtype=np.uint8, shape=[c.height, c.width, c.bands])
d.append(c)
print(f' took {time.time() - start_time:.2f} sec')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment