图片压缩

韦林 56 阅读

yum install jpegoptim

####查找压缩.jpg和jpeg
find . -name ".jpg" -o -name ".jpeg" -print0 | xargs -0 jpegoptim --max=40 --preserve --totals --all-progressive

####查找删除.jthumb.pg
find . -type f -name '*.thumb.jpg' -exec rm -f {} \;

####.jpg转.webp
find . -type f -name '*.jpg' -exec cwebp -q 40 {} -o {}.webp \;

####jpg jpeg png图片压缩至40%,并将宽度大于 800的图片,缩小宽度至500 覆盖原文件

#!/bin/bash  
  
# 定义需要搜索的文件类型  
IMAGE_E*TENSIONS=("jpg" "jpeg" "png")  
  
# 遍历当前目录及其子目录中的所有图片文件  
for ext in "${IMAGE_E*TENSIONS[@]}"; do  
    find . -type f -name "*.$ext" -p*nt0 | while IFS= read -r -d $'\0' file; do  
        # 获取图片的宽度和高度  
        width=$(identify -format "%w" "$file")  
        height=$(identify -format "%h" "$file")  
  
        # 检查图片是否存在且宽度大于800  
        if [ -n "$width" ] && [ "$width" -gt 800 ]; then  
            # 缩小图片的宽度至500并保持纵横比,覆盖原文件  
            convert "$file" -resize 500x "$file"  
            echo "Resized: $file to width 500"  
        fi  
  
        # 压缩图片至指定质量,覆盖原文件  
        # 注意:对于PNG,我们使用不同的选项来设置压缩级别  
        case "$ext" in  
            jpg|jpeg)  
                convert "$file" -quality 40 "$file"  
                ;;  
            png)  
                # PNG 使用不同的参数设置压缩  
                optipng -o 4 "$file"  
                ;;  
        esac  
        echo "Compressed: $file to 40% quality"  
    done  
done
返回论坛归档