bash 批处理 .png 到 .webp

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26565191/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:38:54  来源:igfitidea点击:

Batch process .png to .webp

imagebashpngwebp

提问by user2111006

I have around 100 .png images, and all of them have to be converted to .webp (Google's image format). I am using Google's CLI tool. Any idea how to batch process them.

我有大约 100 张 .png 图像,所有这些图像都必须转换为 .webp(Google 的图像格式)。我正在使用 Google 的 CLI 工具。知道如何批量处理它们。

回答by InfinitePrime

You can do it with a help of a simple bashscript.

您可以借助一个简单的bash脚本来完成。

Navigate to the directory where your images reside and execute this:

导航到您的图像所在的目录并执行以下命令:

$ for file in *
> do
> cwebp -q 80 "$file" -o "$file.webp"
> done

You can change the output file name, as you want. But should end with a .webpextension.

您可以根据需要更改输出文件名。但应该以.webp扩展结束。

回答by Mark Setchell

You need to use GNU Parallel if you have that many, or you will be there all year!

如果你有那么多,你需要使用 GNU Parallel,否则你将全年都在那里!

Please copy a few files into a spare temporary directory first and try this there to be sure it does what you want before using it on 100,000 images:

请先将几个文件复制到一个备用的临时目录中,然后在那里尝试一下,以确保在 100,000 张图像上使用它之前它可以满足您的要求:

parallel -eta cwebp {} -o {.}.webp ::: *.png

That will start, and keep running, as many processes as you have CPU cores, each doing a cwebp. The files processed will be all the PNGfiles in the current directory.

这将启动并继续运行,与您拥有的 CPU 内核一样多的进程,每个进程都执行cwebp. 处理的文件将是PNG当前目录中的所有文件。

If the command line gets too long, you can pass the file list in using findlike this:

如果命令行变得太长,您可以find像这样传递文件列表:

find . -name "*.png" | parallel -eta cwebp {} -o {.}.webp