使用R并行批量调整图像大小 [英] Batch resize images in parallel using R
问题描述
我正在尝试使用R批量调整大小(即缩小文件大小)数千个图像。我已经设法使用下面的代码实现了这一点,但它需要很长时间(特别是在调整大小> 50,000时)图像。有没有办法在多个核心上运行此任务?我是并行计算的完整新手,因此非常感谢任何帮助。提前致谢!
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])))
}
推荐答案
秘密武器是 GNU Parallel 。使用自制进行安装:
The secret weapon is GNU Parallel. Install it with homebrew:
brew install parallel
现在,创建一个输出目录,这样你的输入图像就不会被破坏并运行一堆 mogrify
并行命令:
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
请备份直到你掌握这个!!! / strong>
Please make a backup till you get the hang of this !!!
基准
我制作了1000张400x400的JPEG随机图片噪音并按顺序转换为
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
然后并行:
time parallel -X mogrify -resize 39% -path results ::: *.jpg
real 0m3.351s
user 0m23.021s
sys 0m0.706s
这篇关于使用R并行批量调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!