如何在终端中批量/多处理裁剪图像?

乔可乐

我试图裁剪几张相同大小的图像,但是当我裁剪 5 张图像并且过程完成时,只裁剪了 4 张图像,没有裁剪 1 张图像,如果我裁剪 1 张图像,则会出现错误...

我正在使用命令

convert -crop 599x500+147+200 *.jpg

屏幕截图批量裁剪 5 张图片但仅处理 4 张图片

屏幕截图批量裁剪 1 张图片但出现错误

我应该如何调整我的命令?

赞娜

第一个问题似乎发生了,因为当convert接收到一个列表时,它将使用指定为输出文件名的最后一个文件。所有新文件都以最后一个文件命名,该文件本身保持不变(这不太可能是您想要或期望的,但这可能比您的文件被意外覆盖要好)。

When there is only one file in the directory (so your glob expands to the one file), convert complains about lack of defined images because it expects at least one input file and an output file name to be specified. The position you are using for your glob is the output filename position, so convert is complaining about the lack of an input file in your second image.

For more reliable results you should specify the input and output files:

 convert input-file.jpg -crop 599x500+147+200 new-file.jpg

If the name is long, you should be able to use tab completion (type the first few characters and then press tab to have the shell finish the name) for both input file and output file (that is, it's working fine for me).

对于批处理,您可以使用 shell 对每个文件运行一次命令,并使用一些字符串操作来构造新名称,因此新文件具有合理的名称,例如:

for f in *.jpg; do 
    echo convert "$f" -crop 599x500+147+200 "${f/.jpg/-cropped.jpg}"
done

echo第二行中,这显示了新名称的含义。如果新名称看起来正确,您可以删除echo并再次运行命令以实际裁剪图像。

将新文件放入新目录可能更容易,特别是如果您的文件名具有不同的扩展名...

mkdir cropped
for f in *.jpg *.png; do
    echo convert "$f" -crop 599x500+147+200 cropped/"$f" 
done

同样,您需要先删除,echo然后才能执行任何操作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章