I'm trying to batch resize (i.e., reduce the file size) of thousands of images using R. I've managed to achieve this using the code below, but it takes ages (especially when resizing >50,000) images. Is there any way that this task could be run on multiple cores? I'm a complete novice on parallel computing, so any assistance would be greatly appreciated. Thanks in advance!
library(imager)
pages <- list.files(path = '...insert directory path...',
full.names = TRUE)
for(x in 1:length(pages)) {
file <- load.image(pages[x])
resized <- imresize(file,
scale = 0.390625)
save.image(resized,
file = gsub("JPG", "jpg", paste(pages[x])))
}
The secret weapon is GNU Parallel. Install it with homebrew:
Now, make an output directory so your input images don't get clobbered and run a bunch of
mogrify
commands in parallel:Please make a backup till you get the hang of this !!!
Benchmark
I made 1,000 JPEGs of 400x400 full of random noise and converted them sequentially with
And then in parallel: