31

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
  • 11,885
  • 5
  • 66
  • 81
user2111006
  • 455
  • 1
  • 4
  • 6

2 Answers2

56

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,539
  • 14
  • 18
  • Where i can write this script? – Pratik Butani Dec 31 '14 at 10:33
  • 2
    @InfinitePrime, I just made a small edit to your answer. added quotes to the cwebp args (so your code doesn't break if there are spaces in the input file names) – Corey Goldberg Feb 22 '16 at 16:14
  • 6
    Unfortunately this results in files named like `file.png.webp`, at least in my usage on Mingw. – VivaLaPanda Oct 06 '17 at 22:31
  • 1
    on windows you can download cwebp binaries and use powershell to run it, see this answer https://stackoverflow.com/a/49602450/7448956 – pldg Apr 01 '18 at 20:55
  • @aaawww or you can install a linux subsystem – jirig Jun 04 '18 at 00:06
  • 18
    @AdrianSmith You can use `for file in *.png ; do cwebp -q 50 "$file" -o "${file%.png}.webp"; done` to drop png from the output file extension. – HaL Jul 18 '18 at 16:45
  • @HaL Thank you sir. Question: "What is the function of the %" ? – Timothy Oct 28 '20 at 09:19
  • Correct me if im wrong but this would result in a lossy compression, if you want to preserve the image in all its detail use ` -lossless -exact` flags instead of -q. – Peter Nov 06 '20 at 11:08
  • @HaL comment should be included in the answer. It's a really nice addition, dropping the original extension. – Gustavo Straube Jan 13 '21 at 13:41
33

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
  • 169,892
  • 24
  • 238
  • 370
  • 3
    Nice use of parallel. But you missed the dot! It should be {.}.webp – iNFINITEi Jul 13 '16 at 12:44
  • @iNFINITEi Well spotted - thank you. I believe I have corrected it now - but please say if you know different! – Mark Setchell Jul 13 '16 at 12:46
  • @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 – Hosam Elzagh 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
  • This will convert all png files, is there a way to only convert files that match the android api level, just like the Android Studio do, but in batch processing. – twlkyao Nov 07 '18 at 09:35
  • @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