linux - 批量将图像文件 jpg、png 转换为 webp 也在子文件夹中

标签 linux bash image webp

我找到了一个脚本来转换目录中的文件 但我需要这个与子导演

你能帮帮我吗?

#!/bin/bash

PATH=/usr/local/bin:/usr/bin:/bin

# cd to the directory of the image so we can work with just filenames
dir="$(dirname "$1")"
cd "$dir" || exit 1
base="$(basename "$1" .png)"

# create a WebP version of the PNG
cwebp -q 80 "$base".png -o "$base".webp

# delete the WebP file if it is equal size or larger than the original PNG
if [[ `stat -c '%s' "$base".webp` -ge `stat -c '%s' "$base".png` ]]; then
    echo "Deleting WebP file that is no smaller than PNG"
    rm -f "$base".webp
fi

# delete the WebP file if it is size 0
if [[ -f "$base".webp && ! -s "$base".webp ]]; then
    echo "Deleting empty WebP file"
    rm -f "$base".webp
fi

最佳答案

好消息:您不必更改脚本!

你可以通过命令找到根目录下的所有目录:

find /path/to/root/dir -type d 

你可以为每个找到的目录添加一些命令的执行:

假设您的脚本名称是 script.sh 并且它位于您的主目录中,并且您想在当前目录下的所有子目录(包括当前目录)上运行它:

find . -type d -exec ~/script.sh "{}" \;

关于linux - 批量将图像文件 jpg、png 转换为 webp 也在子文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51384497/

相关文章:

linux - 如何将路径名中的 ".."转换为 bash 脚本中的绝对名称?

java - 一个简短的脚本,用于处理充满文件的目录,一个接一个,维护名称

php - 有没有办法在 php 中使用 $result->fetch_row 显示数据库中的图像?

java - 在scrollPane中显示多个图像会导致组件根据图像大小丢失

c++ - 使用 Raspberry Pi 和 Qt 的 Poco C++

linux - Ubuntu-如何在运行级别 5 中启动服务

linux - 我可以在创建 rpm 时拥有多个内核版本的通用规范文件吗

linux - noarch rpm 和 rpm 之间的区别

linux - 我的 shell 脚本使用的语法是否正确?

python - 使用带有 Python 的 GDAL 裁剪光栅文件