Batch resize images in parallel using R

2019-05-27 03:42发布

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])))

}

1条回答
forever°为你锁心
2楼-- · 2019-05-27 04:15

The secret weapon is GNU Parallel. Install it with homebrew:

brew install parallel

Now, make an output directory so your input images don't get clobbered and run a bunch of mogrify commands in parallel:

mkdir results
parallel -X mogrify -resize 39% -path results ::: *.jpg

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

time for i in *.jpg; do convert $i -resize 39% results/$i; done

real    0m19.086s
user    0m18.615s
sys     0m3.445s

And then in parallel:

time parallel -X mogrify -resize 39% -path results ::: *.jpg

real    0m3.351s
user    0m23.021s
sys     0m0.706s
查看更多
登录 后发表回答