37

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.

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
user2111006
  • 515
  • 1
  • 4
  • 6

2 Answers2

69

You can do it with a help of a simple bash script.

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

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

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

InfinitePrime
  • 1,718
  • 15
  • 18
39

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

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:

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 PNG files in the current directory.

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

find . -name "*.png" | parallel -eta cwebp {} -o {.}.webp
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • @MarkSetchell Can you explain what the dot does? My command `find JPEG/ -iname "*.jpeg" -print -exec cwebp -jpeg_like -af {} -o Webp/{}.webp \;` works without the dot(Ubuntu). Thank you in advance. Googling didn't help. P.S. the dot refers to the first dot in `{.}.webp` – saurabheights Aug 02 '16 at 17:11
  • how to execution this command and output .webp in another folder – xpredo Jul 11 '18 at 05:41
  • @Hosamalzagh You can just use `mkdir other/folder` then `parallel -eta cwebp {} -o other/folder/{} ::: *.png` – Mark Setchell Jul 11 '18 at 06:51
  • 1
    @saurabheights the dot in `{.}` removes the extension of the original match, outputting to `asdf.webp` instead of `asdf.jpeg.webp` – spro Jan 09 '21 at 02:18
  • Online tools such as https://towebp.io can do the job instantly. – Wessam El Mahdy Aug 26 '22 at 11:39