jpegtran optimize without changing filename

后端 未结 6 1984
囚心锁ツ
囚心锁ツ 2021-02-05 06:42

I need to optimize some images, but not change their name.

jpegtran -copy none -optimize image.jpg > image.jpg 

However, this seems to crea

6条回答
  •  既然无缘
    2021-02-05 07:40

    I use this script. Works flawlessly. I hope this helps.

    https://gist.github.com/iimos/7424025

    #! /bin/sh
    
    EXTENSIONS="jpe?g"
    
    if [ -z "$1" ]; then
        DIR="`pwd`"
    else
        DIR="$1"
    fi
    
    # Optimize JPEG images
    find "$DIR" -regextype posix-egrep -regex ".*\.($EXTENSIONS)\$" -type f | xargs -I{} jpegtran -optimize -progressive -outfile "{}.optimized" "{}"
    
    # Rename xxx.jpg.optimized -> xxx.jpg
    find "$DIR" -name '*.optimized' -print0 | while read -d $'\0' file; do 
        chown $(stat -c "%U:%G" "${file%.optimized}") "$file"
        chmod $(stat -c "%a" "${file%.optimized}") "$file"
        mv -f "$file" "${file%.optimized}"; 
    done
    

    Usage 1:

    optimize-images.sh /images/dir
    

    Usage 2:

    cd /images/dir
    optimize-images.sh 
    

提交回复
热议问题