github:https://github.com/Guicai-Li/auto-image-scale
如何使用?
brew link jpeg
brew install ImageMagick
主要通过ImageMagic的convert图片处理方式粗略解决的。
#!/bin/sh
rm -rf 1x
rm -rf 2x
rm -rf 3x
mkdir 1x
mkdir 2x
mkdir 3x
for img in `ls *@2x.png`
do
name1x=1x/${img%@*}.png
name2x=2x/${img%@*}@2x.png
name3x=3x/${img%@*}@3x.png
echo ${name1x}
convert -resize 50%x50% ${img} ${name1x}
echo ${name2x}
cp ${img} ${name2x}
echo ${name3x}
convert -resize 150%x150% ${img} ${name3x}
done
配置过程中易出现的问题:
[libpng]could not link symlink include/libpng16
第二版:
针对第一版,粗略的将2@图直接放大150%导致失真的情况。
通过identify获取图片的宽高,进行计算,从而直接从@6图直接缩小成@1 @2 @3倍图片。
#!/bin/sh
rm -rf 1x
rm -rf 2x
rm -rf 3x
mkdir 1x
mkdir 2x
mkdir 3x
for img in `ls *@2x.png`
do
name1x=1x/${img%@*}.png
name2x=2x/${img%@*}@2x.png
name3x=3x/${img%@*}@3x.png
WIDTH=`identify ${img} | cut -f 3 -d " " | sed s/x.*//` #width
HEIGHT=`identify ${img} | cut -f 3 -d " " | sed s/.*x//` #height
echo $WIDTH
echo $HEIGHT
#计算出1倍图的大小
echo ${name1x}
dw=`echo "${WIDTH}/6" |bc`
echo $dw
dh=`echo "${HEIGHT}/6" |bc`
echo $dh
convert -resize $dwx$dh ${img} ${name1x}
#计算出2倍图的大小
echo ${name2x}
dw=`echo "${WIDTH}/3" |bc`
echo $dw
dh=`echo "${HEIGHT}/3" |bc`
echo $dh
convert -resize $dwx$dh ${img} ${name2x}
#计算出1倍图的大小
echo ${name3x}
dw=`echo "${WIDTH}/2" |bc`
echo $dw
dh=`echo "${HEIGHT}/2" |bc`
echo $dh
convert -resize $dwx$dh ${img} ${name3x}
done
第三版:
#!/bin/sh
#将@6倍图自动缩放为@1 @2 @3倍图
path="ProcessedImages"
if [[ ! -x "$path" ]]; then
mkdir "$path"
fi
for img in `ls *.png`
do
name1x=$path/${img%@*}.png
name2x=$path/${img%@*}@2x.png
name3x=$path/${img%@*}@3x.png
WIDTH=`identify ${img} | cut -f 3 -d " " | sed s/x.*//` #width
HEIGHT=`identify ${img} | cut -f 3 -d " " | sed s/.*x//` #height
echo $WIDTH
echo $HEIGHT
#计算出1倍图的大小
echo ${name1x}
dw=`echo "${WIDTH}/6" |bc`
echo $dw
dh=`echo "${HEIGHT}/6" |bc`
echo $dh
convert -resize $dwx$dh ${img} ${name1x}
#计算出2倍图的大小
echo ${name2x}
dw=`echo "${WIDTH}/3" |bc`
echo $dw
dh=`echo "${HEIGHT}/3" |bc`
echo $dh
convert -resize $dwx$dh ${img} ${name2x}
#计算出3倍图的大小
echo ${name3x}
dw=`echo "${WIDTH}/2" |bc`
echo $dw
dh=`echo "${HEIGHT}/2" |bc`
echo $dh
convert -resize $dwx$dh ${img} ${name3x}
rm $img
done