bash 在行命令中调整图像列表的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/561895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Resize a list of images in line command
提问by Jér?me
I would like to resize a list of images, all in the directory. To achieve that, I use convertfrom imagemagick. I would like to resize
我想调整所有在目录中的图像列表的大小。为了实现这一点,我使用convert了 imagemagick。我想调整大小
image1.jpg
image2.jpg
...
into
进入
image1-resized.jpg
image2-resized.jpg
...
I was wondering if there is a method to achieve this in a single command line. An elegant solution could be often useful, not only in this case.
我想知道是否有一种方法可以在单个命令行中实现这一点。一个优雅的解决方案通常很有用,不仅在这种情况下。
EDIT:
编辑:
I would like a nonscript-likesolution, ie. without for loop.
我想要一个非脚本式的解决方案,即。没有 for 循环。
回答by Johannes Weiss
If you want to resize them to 800x600:
如果要将它们调整为 800x600:
for file in *.jpg; do convert -resize 800x600 -- "$file" "${file%%.jpg}-resized.jpg"; done
(works in bash)
(在 bash 中工作)
回答by chaos
ls *.jpg|sed -e 's/\..*//'|xargs -I X convert X.jpg whatever-options X-resized.jpg
You can eliminate the sed and be extension-generic if you're willing to accept a slightly different final filename, 'resized-image1.jpg' instead of 'image1-resized.jpg':
如果您愿意接受略有不同的最终文件名“resized-image1.jpg”而不是“image1-resized.jpg”,您可以消除 sed 并使用扩展名:
ls|xargs -I X convert X whatever-options resized-X
回答by DaveMan
GNU Parallelis even easier than for loops, and it's often faster:
GNU Parallel比 for 循环更容易,而且通常更快:
parallel convert -resize 800x600 -- "{}" "{.}-resized.jpg" ::: *.jpg
A few things going on here, from right to left:
这里发生了一些事情,从右到左:
::: *.jpgmeans run the command for every jpg file{.}means insert the current filename without the suffix (.jpg){}means insert the current filenameparallelmeans run the following command many times in parallel. It will choose the max to do in parallel to match the number of cores your computer has. As each one finishes it will launch the next one until all the jpg files are converted.
::: *.jpg意味着为每个 jpg 文件运行命令{.}表示插入不带后缀 (.jpg)的当前文件名{}表示插入当前文件名parallel意味着并行运行以下命令多次。它将选择并行执行的最大值以匹配您的计算机拥有的内核数。每个完成后,它将启动下一个,直到所有 jpg 文件都被转换。
This runs the command convert --resize 800x600 -- foo.jpg foo-resized.jpgfor each file. The --tells convert to stop processing flags, in case a file name happens to start with a -.
这会convert --resize 800x600 -- foo.jpg foo-resized.jpg为每个文件运行命令。该--告诉转换到停止处理标志,如果一个文件名恰好开始一个-。
P.S. On my mac I have Homebrewinstalled, so I was able to install parallel and convert with
PS 在我的 Mac 上,我安装了Homebrew,所以我能够并行安装并转换
brew install parallel
brew install imagemagick
回答by Nietzche-jou
If your image files have different extensions:
如果您的图像文件具有不同的扩展名:
for f in *; do convert -resize 800x600 -- "$f" "${f%.*}-resized.${f##*.}"; done

