Last active
August 29, 2015 14:10
-
-
Save takehiko/eee0440fc3c662f9e396 to your computer and use it in GitHub Desktop.
Photo Minifier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/zsh | |
| # photominifier.sh | |
| # 撮影画像を縮小化する | |
| # by takehikom | |
| # 準備 | |
| # ・要 Zsh, ImageMagick (convert, idenfity) | |
| # ・DIR1 に,縮小化させたい画像ファイルのディレクトリを指定する | |
| # ・DIR2 に,画像ファイルのバックアップディレクトリを指定する | |
| # (DIR1 から DIR2 へのファイルコピーは行わない) | |
| # 動作 | |
| # ・DIR1 の各 JPG (ファイル名大小無視)ファイルについて, | |
| # 以下の条件をすべて満たすとき,縮小化する | |
| # * ファイル名が small_ で始まらない | |
| # * 撮影日が2か月以上過去(EXIF の DateTimeOriginal | |
| # タグをもとに判定; EXIFがないファイルは対象外) | |
| # * ディレクトリ DIR2/yyyy/yyyymmdd に,ファイルがあり, | |
| # そのサイズは縮小対象ファイルと同一 | |
| # ・拡大率25%,品質90%で,ファイル名の先頭に small_ をつける | |
| # ・縮小化の元ファイルを削除する | |
| DIR1="." | |
| DIR2="/" | |
| DATE_REF=$(date -d '-2 month' '+%Y%m%d') | |
| function photo_date () { | |
| identify -verbose "$1" | grep exif:DateTimeOriginal | sed -E 's/^.* ([0-9]{4}).([0-9]{2}).([0-9]{2}).*$/\1\2\3/' | |
| } | |
| function photo_year_date () { | |
| identify -verbose "$1" | grep exif:DateTimeOriginal | sed -E 's/^.* ([0-9]{4}).([0-9]{2}).([0-9]{2}).*$/\1\/\1\2\3/' | |
| } | |
| function photo_backupfile () { | |
| echo "${DIR2}/$(photo_year_date $1)/$(basename $1)" | |
| } | |
| for file1 in $(find $DIR1 -iname '*.jpg' '!' -name "small_*") | |
| do | |
| local file2=$(photo_backupfile $file1) | |
| local file_small=$(dirname $file1)/small_$(basename $file1) | |
| echo $file1 ':' $file2 ':' $file_small | |
| if [[ ! -e ${file2} ]] | |
| then | |
| echo " ${file2} not found." | |
| continue | |
| fi | |
| if [[ $DATE_REF -lt $(photo_date $file1) ]] | |
| then | |
| echo " ${file1} not old." | |
| continue | |
| fi | |
| if diff -q ${file1} ${file2} | |
| then | |
| else | |
| echo " two files are not same." | |
| continue | |
| fi | |
| echo convert $file1 -resize '25%' -quality 90 $file_small | |
| echo /bin/rm $file1 | |
| convert $file1 -resize '25%' -quality 90 $file_small && /bin/rm $file1 | |
| echo " minified" | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment