Convert WEBP images to PNG by Linux command [closed]
Asked Answered
S

5

41

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
Sperm answered 14/3, 2019 at 11:26 Comment(0)
D
41

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.

Davita answered 20/3, 2019 at 10:1 Comment(2)
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.Vespine
More info: GNU Parallel , - - Memo: https://www.gnu.org/software/parallel/parallel_cheat.pdfAndraandrade
J
64

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.

Related

  1. My answer: Super User: How can I view .HEIC [and .webp] photos on Linux?
Jochbed answered 9/1, 2022 at 12:39 Comment(3)
I would suggest using ImageMagick convert instead, as it converts from a variety of formats to a variety of formats: convert xyz.webp xyz.png.Aneroid
@YakovK, for many years (7 years maybe) convert literally never, not once, worked for me, when trying to use it on Ubuntu 14.04, 16.04, 18.04, 20.04, and 22.04.Jochbed
Finally I found the fix, which I mention here, but by then had come to hate and resent that convert never works.Jochbed
D
41

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.

Davita answered 20/3, 2019 at 10:1 Comment(2)
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.Vespine
More info: GNU Parallel , - - Memo: https://www.gnu.org/software/parallel/parallel_cheat.pdfAndraandrade
J
8

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.

Jocularity answered 24/11, 2020 at 23:1 Comment(3)
It is far preferable to use for x in *.jpg rather than parsing the output of ls - see mywiki.wooledge.org/ParsingLsDavita
Your commands both have superfluous ::: in the middle of them - probably left-overs from GNU ParallelDavita
@MarkSetchell thanks, TIL - will check that and fix the answer if all works well so this is as clean as possible :)Jocularity
T
-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
Temporary answered 3/1, 2022 at 8:32 Comment(0)
V
-4

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

Volplane answered 14/3, 2019 at 14:37 Comment(1)
completely no use. these are different formats, changing extension is insufficient.Jocularity

© 2022 - 2024 — McMap. All rights reserved.