简体   繁体   English

通过 Linux 命令将 Webp 图像转换为 PNG

[英]Convert Webp images to PNG by Linux command

I have many webp format images in a folder but with .jpg` extension likewebp format images in a folder but with有许多webp format images in a folder but with扩展名为 .jpg`

abc-test.jpg

It's a webp format image.这是一个webp格式的图像。 I want it to convert in .png format with same name for that I have used this command and it worked我希望它转换为具有相同名称的.png格式,因为我使用了这个命令并且它有效

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

It converted all webp images to .png but the issue is it's saving images like this:它将所有webp图像转换为.png但问题是它正在保存这样的图像:

abc-test.jpg.png

How can I save it without .jpg extension like如何在没有.jpg扩展名的情况下保存它

abc-test.png

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.如果您有许多要转换/重命名的文件,我建议您使用GNU Parallel ,不仅可以通过并行执行它们来更快地转换它们,还可以利用修改文件名的功能。

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.如果您想要进度表或“预计到达时间” ,您可以在parallel命令后添加--progress--eta

If you want to see what GNU Parallel would run, without actually running anything, add --dry-run .如果您想查看GNU Parallel将运行的内容,而无需实际运行任何内容,请添加--dry-run

I commend GNU Parallel to you in this age where CPUs are getting "fatter" (more cores) rather than faster.在这个 CPU 变得“更胖” (更多内核)而不是更快的时代,我向您推荐GNU Parallel

I did it with short oneliner that does not require parallel to be installed in the system我用不需要parallel安装在系统中的短oneliner做到了

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:我会尝试修改 @mark-setchell 递归解决方案,使其看起来像这样:

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 . ${x%.*}部分是这里需要解释的部分 - 它告诉 bash 将. and everything after the dot from the x variable.以及x变量中点之后的所有内容。 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.因为我没有检查这里的正则表达式是懒惰还是贪婪,所以对于带有更多点的名称很容易出现行为不端 - 因此可以进一步调整答案。

If the problem is with linux image viewers - thats the reason of convertion - then I found that: here如果问题出在 linux 图像查看器上——这就是转换的原因——那么我发现:这里

"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. 《在 Ubuntu 和其他 Linux 中为 GNOME 图像查看器添加 WebP 支持默认情况下,照片查看器不支持 WebP 图像文件。但是,您可以通过安装 webp-pixbuf-loader 库来添加 WebP 支持。它不仅允许您打开 WebP GNOME 图像查看器中的文件,它还在文件资源管理器中显示 WebP 文件的缩略图。

On Ubuntu-based Linux distributions, you can install this library using a PPA.在基于 Ubuntu 的 Linux 发行版上,您可以使用 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

How to convert .webp images to .png on Linux如何在 Linux 上将 .webp 图像转换为 .png

Tested on Linux Ubuntu 20.04在 Linux Ubuntu 20.04 上测试

This question is the top hit for the Google search of "linux convert .webp image to png" .这个问题是谷歌搜索“linux convert .webp image to png”的热门问题 Therefore, for anyone stumbling here and just wanting that simple answer, here it is:因此,对于任何在这里跌跌撞撞并只想得到这个简单答案的人来说,这里是:

# 1. Install it
sudo apt update
sudo apt install webp

# 2. Convert in.webp to out.png
dwebp in.webp -o out.png

Done!完毕! You now have out.png .你现在有了out.png

A good thing to do is to use sed along with mv.一件好事是将 sed 与 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如果你想保留旧文件而不是mv你可以使用cp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM