Skip to content

Instantly share code, notes, and snippets.

@floringogianu
Created May 1, 2017 20:58
Show Gist options
  • Save floringogianu/7fd11312ce6b15a975ec8f61649f3914 to your computer and use it in GitHub Desktop.
Save floringogianu/7fd11312ce6b15a975ec8f61649f3914 to your computer and use it in GitHub Desktop.
Simple benchmark of image resize methods.
import time
import numpy as np
from skimage.transform import resize
from PIL import Image
def pil_resize(x, target, method=Image.NEAREST):
img = Image.fromarray(x)
return np.array(img.resize(target, resample=method))
def ski_resize(x, target, method):
return np.uint8(resize(x / 255, target) * 255)
def benchmark_f(trials, f, target, method=None):
elapsed = 0
img_size = (160, 120) # Size of Atari frame
for i in range(trials):
x = np.random.randint(0, 255, img_size, dtype=np.uint8)
start = time.time()
_ = f(x, target, method) # noqa
elapsed += time.time() - start
print("%s-%s: %d trials | total=%.2fs | mean=%.2fs." %
(f.__name__, 'n' if method is None else method, trials, elapsed,
elapsed / trials))
if __name__ == "__main__":
trials = 1000
target = (84, 84)
benchmark_f(trials, pil_resize, target, Image.NEAREST)
benchmark_f(trials, pil_resize, target, Image.LANCZOS)
benchmark_f(trials, pil_resize, target, Image.BILINEAR)
benchmark_f(trials, pil_resize, target, Image.BICUBIC)
benchmark_f(trials, pil_resize, target, Image.BOX)
benchmark_f(trials, pil_resize, target, Image.HAMMING)
benchmark_f(trials, ski_resize, target)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment