29

I have many webp format images in a folder but with .jpg extension like

abc-test.jpg

It's a webp format image. I want it to convert in .png format with same name for that I have used this command and it worked

find . -name "*.jpg" -exec dwebp {} -o {}.png \;

It converted all webp images to .png but the issue is it's saving images like this:

abc-test.jpg.png

But my requirement is to save it without .jpg extension like

abc-test.png
OBAID
  • 1,299
  • 2
  • 21
  • 43

5 Answers5

36

How to convert .webp images to .png on Linux

Tested on Linux Ubuntu 20.04

This question is the top hit for the Google search of "linux convert .webp image to png". Therefore, for anyone stumbling here and just wanting that simple answer, here it is:

# 1. Install the `webp` tool
sudo apt update
sudo apt install webp

# 2. Use it: convert in.webp to out.png
dwebp in.webp -o out.png

Done! You now have out.png.

References

  1. I learned about dwebp from the question itself
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
33

If you have many to convert/rename, I would recommend you use GNU Parallel and not only get them converted faster by doing them I parallel, but also take advantage of the ability to modify filenames.

The command you want is:

parallel dwebp {} -o {.}.png ::: *.jpg

where the {.} means "the filename without the original extension".

If you want to recurse into subdirectories too, you can use:

find . -name "*.jpg" -print0 | parallel -0 dwebp {} -o {.}.png

If you want a progress meter, or an "estimated time of arrival", you can add --progress or --eta after the parallel command.

If you want to see what GNU Parallel would run, without actually running anything, add --dry-run.

I commend GNU Parallel to you in this age where CPUs are getting "fatter" (more cores) rather than faster.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • 9
    Very interesting, thanks. For anyone unfamiliar with webp (like I was), you might first need to install the following packages: `sudo aptitude install parallel webp`. And for webp files with the correct extension, simply do `parallel dwebp {} -o {.}.png ::: *.webp`. – Alex Apr 27 '21 at 15:53
  • More info: [GNU Parallel](https://www.gnu.org/software/parallel/) , - - Memo: [https://www.gnu.org/software/parallel/parallel_cheat.pdf](https://www.gnu.org/software/parallel/parallel_cheat.pdf) – kFly Jan 02 '22 at 21:22
5

I did it with short oneliner that does not require parallel to be installed in the system

for x in `ls -1 *.jpg`; do dwebp {} -o ${x%.*}.png ::: $x; done

And this works for current directory

I would try to amend the @mark-setchell recursive solution so it would look like this:

for x in `find . -name "*.jpg"`; do dwebp {} -o ${x%.*}.png ::: $x; done

The ${x%.*} part is the one requiring a word of explanation here - it tells bash to take . and everything after the dot from the x variable. It is prone to misbehave for names with more dots as I did not check if regex here is lazy or greedy - the answer can be tuned further therefore.

tymik
  • 606
  • 7
  • 17
  • 1
    It is far preferable to use `for x in *.jpg` rather than parsing the output of `ls` - see https://mywiki.wooledge.org/ParsingLs – Mark Setchell Nov 25 '20 at 16:09
  • Your commands both have superfluous `:::` in the middle of them - probably left-overs from **GNU Parallel** – Mark Setchell Nov 25 '20 at 16:11
  • @MarkSetchell thanks, TIL - will check that and fix the answer if all works well so this is as clean as possible :) – tymik Nov 26 '20 at 20:01
-1

If the problem is with linux image viewers - thats the reason of convertion - then I found that: here

"Add WebP support to GNOME Image Viewer in Ubuntu and Other Linux By default, the photo viewer does not support WebP images files. However, you can add WebP support by installing webp-pixbuf-loader library. Not only it allows you to open WebP files in GNOME Image Viewer, it also displays thumbnails for WebP files in the file explorer.

On Ubuntu-based Linux distributions, you can install this library using a PPA. Use the following commands one by one:"

sudo add-apt-repository ppa:krifa75/eog-ordissimo
sudo apt update
sudo apt install webp-pixbuf-loader
Estatistics
  • 874
  • 9
  • 24
-3

A good thing to do is to use sed along with mv. It matches the pattern and replaces with a newer one.

for file in *.jpg;
do
   mv "$file" "`echo $file | sed s/.jpg/.png/`"
done

if you want to retain the old files instead of mv you can use cp