Shell-Scripts/all-png-to-webp.sh

45 lines
1.3 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 检查是否安装了必要的工具
if ! command -v magick &> /dev/null; then
echo "错误: 未找到 ImageMagick请先安装 ImageMagick"
exit 1
fi
if ! command -v exiftool &> /dev/null; then
echo "错误: 未找到 ExifTool请先安装 ExifTool"
exit 1
fi
# 遍历当前目录下的所有 PNG 文件
for png_file in *.png; do
# 检查文件是否存在(防止没有 png 文件时的错误)
if [ ! -f "$png_file" ]; then
echo "当前目录下没有 PNG 文件"
exit 0
fi
# 获取文件名(不含扩展名)
filename="${png_file%.*}"
# 检查是否已经存在对应的 WebP 文件
if [ -f "${filename}.webp" ]; then
echo "跳过: ${filename}.webp 已存在"
continue
fi
# 转换为 WebP 格式
echo "正在转换: $png_file -> ${filename}.webp"
magick "$png_file" "${filename}.webp"
# 写入作者信息
echo "正在写入作者信息到: ${filename}.webp"
exiftool -overwrite_original -Author="奇趣保罗" "${filename}.webp"
# 修改 WebP 文件的创建日期为 PNG 文件的日期
echo "正在同步创建日期: ${filename}.webp <- $png_file"
touch -r "$png_file" "${filename}.webp"
done
echo "所有文件处理完成!"