使用sips命令自动缩减图片尺寸
使用sips命令自动缩减图片尺寸记得之前有一本书上说,有一个工程师写脚本很厉害,小到发邮件,大到煮咖啡,全部都是脚本完成。作为ITer,我们得习惯将自己的日常需求自动化,智能化。脚本,只是一个开始……
背景
长期使用印象笔记来记录日志,之前没有搞清楚印象笔记的缩略图是怎么选的,前不久刚搞清楚。
此外,还在维护两个公众号,里面多多少少也有图片需要处理。
不论是写日志还是维护公众号,每次图片一多,图片的处理就非常耗时。
需求
既然是耗时,就需要节省时间啦,需求其实很明确。
通常:
- 桌面只有一张图片的时候,默认是题图或是印象笔记的缩略图。
- 如果有很多图片的话,那么一般来说就是文章的配图了。
后者一定要比前者小,在分辨率上。前者一般是照片,所以分辨率通常比较大,后者一般都是网络图片或者截图,尺寸会比较小。
分析
尺寸确定
找了一些照片什么的,感觉1920
这个宽度不错,如果是4:3
,那么就是1440
,16:9
对应的就是1080。
而配图要小一号,自然使用1600:1200
比较合适了。
工具确定
接下来就需要找OSX下处理图片的命令了。话说Python
可以做,但是好久没摸Python
了,但是shell
一直在用,而且在带别人学习。所以使用shell
自然最容易。
还好,OSX下有一个sips
的命令,对图片处理比较方便,相关信息都可以获取到。这个命令自从10.4
版本就有了。
命令的参数及使用可以参见这里:https://ss64.com/osx/sips.html
解决方案
因为课程里都要支持参数,所以默认支持了以下三个:
- -d:调试模式
- -v:复杂信息模式;
- -h:帮助;
具体脚本如下,在公司反而访问不了我的代理了,无语。回家再传到Github
上。
#!/bin/bash
# -------------------- Copyright --------------------
# FileName: asimg.sh
# Description: Automatic scaling images
# Version: 1.0
# Date: 2018/08/14
# Author: Rex Kang
# Email: rex.kang.qq.com
# -------------------- History --------------------
# 2018/08/14: First version
# -------------------- End --------------------
help() {
echo "usage:"
echo -e "\tbash $0 [-cdvh] imgs"
echo "parameters:"
echo -e "\t-d:\tEnabled debug mode."
echo -e "\t-v:\tEnabled verbose mode."
echo -e "\t-h:\tShow help."
}
process() {
OLD_IFS="$IFS"
path='~/Desktop'
cd $path
# get all the images that need process.
imgs=$(ls -A1 $path | tail -n +2 | grep -E "${ext}")
imgs_num=`echo "$imgs" | wc -l | grep -Eo '[0-9]+'`
if [ $imgs_num -eq 0 ]; then
echo 'No images need to be processed.'
exit 1
elif [ $imgs_num -eq 1 ]; then
default=("${cover[@]}")
$debug && echo -e "debug:\tcover mode."
elif [ $imgs_num -gt 1 ]; then
default=("${general[@]}")
$debug && echo -e "debug:\tgeneral mode."
fi
echo "${default[@]}"
( $verbose || $debug ) && echo -e "info:\timages: ${imgs_num}"
IFS=$'\n'
echo "$imgs" | while read line
do
# calculating pixels and scaling
img_name="$line"
echo -e "info:\t${img_name}"
output=$(sips -g pixelHeight -g pixelWidth "$img_name")
$debug && echo -e "output:\t"$output
width=`echo "$output" | grep pixelWidth | grep -Eo '[0-9]+'`
height=`echo "$output" | grep pixelHeight | grep -Eo '[0-9]+'`
max=$width; [ $height -gt $width ] && max=$height
$debug && echo -e "debug:\twidth: $width, height: $height, max: $max"
resolution=$(( height * width ))
$debug && echo -e "debug:\tresolution: $resolution, default: ${default[2]}"
if [ $resolution -gt ${default[2]} ]; then
$debug && echo -e "debug:\timage need to reduce."
max_after=`echo "scale=4;$max/sqrt($resolution/${default[2]})" | bc`
$debug && echo -e "debug:\timage max after resize is ${max_after}."
sips -Z $max_after "$img_name" > /dev/null 2>&1
[ $? -eq 0 ] && echo -e "info:\tresize successfully." || echo -e "info:\tresize failed."
else
echo -e "info:\tno need to resize."
fi
done
IFS="$OLD_IFS"
}
main() {
debug=false
verbose=false
default=(1600 1200 1920000)
general=(1600 1200 1920000)
cover=(1920 1440 2764800)
ext="\.jpeg|\.jpg|\.png|\.gif|\.bmp"
while getopts "hvd" OPT; do
case $OPT in
v)
verbose=true
;;
d)
debug=true
;;
h)
help
exit 0
;;
?)
help
exit 1
;;
esac
done
if $debug; then
echo "----- variables -----"
echo -e "debug:\t$debug"
echo -e "verbose:$verbose"
fi
process
}
main $@
其他
恩,初步完成,但是不同公众号的图片尺寸问题等还需要再斟酌一下,后续再考虑。
网友评论