ImageMagick

1). 安装与更新

macos安装ImageMagick:brew installimagemagick 更新: brew upgrade imagemagick

2). 基本用法

格式转换 convert 2.png 22.jpg

转换50x50 convert -resize 22x22 img1.png resize.png

旋转: convert -rotate 90 img1.png rotated.jpg

获取尺寸 identify -format "%[fx:w]x%[fx:h]" rotate.jpg #得到 334x368%

编写生成2x图片的脚本(尺寸缩放到2/3)

实现效果: 先再脚本所在目录A下创建output文件夹,然后将所有图片复制到output文件夹中并添加@3x后缀,最后生成@2x的图片。

#!/bin/bash

#cd /Users/tyrad/Desktop/mm/test

cd  `dirname $0`

mkdir output

for i in `ls *.png` ; do cp $i output/`echo $i | sed 's/\.png//'`@3x.png ; done

for i in `ls *.png`
do

w=$(identify -format "%w" ${i})
h=$(identify -format "%h" ${i})

#shell计算中使用除法,基本默认上都是整除
#使用bc工具,sclae控制小数点后保留几位
w2=`echo "scale=2; $w/3*2" | bc`
h2=`echo "scale=2; $h/3*2" | bc`

#to 2x
convert -resize ${w2}x${h2}  ${i}  output/`echo $i | sed 's/\.png//'`@2x.png

done

优化:可以直接使用百分比来缩放

#!/bin/bash

cd  `dirname $0`

mkdir output 

for i in `ls *.png` ; do cp $i output/`echo $i | sed 's/\.png//'`@3x.png ; done

for i in `ls *.png` ; do convert -resize '66.6667%!'  ${i}  output/`echo $i | sed 's/\.png//'`@2x.png ; done 

icon调整

#!/bin/bash

cd  `dirname $0`

mkdir output 


for i in `ls *.png` 

do 
	convert -resize 20x20  ${i}  output/`echo $i | sed 's/\.png//'`_x20.png   
	convert -resize 40x40  ${i}  output/`echo $i | sed 's/\.png//'`_x40.png   
	convert -resize 87x87  ${i}  output/`echo $i | sed 's/\.png//'`_x87.png    
    convert -resize 58x58  ${i}  output/`echo $i | sed 's/\.png//'`_x58.png   
    convert -resize 80x80  ${i}  output/`echo $i | sed 's/\.png//'`_x80.png  
    convert -resize 120x120  ${i}  output/`echo $i | sed 's/\.png//'`_x120.png   
    convert -resize 180x180  ${i}  output/`echo $i | sed 's/\.png//'`_x180.png   
    convert -resize 76x76  ${i}  output/`echo $i | sed 's/\.png//'`_x76.png 
    convert -resize 152x152  ${i}  output/`echo $i | sed 's/\.png//'`_x152.png  
    convert -resize 167x167  ${i}  output/`echo $i | sed 's/\.png//'`_x167.png 
done 

参考:

http://www.netingcn.com/imagemagick-resize.html

https://www.cnblogs.com/ITtangtang/p/3951240.html

https://www.zhihu.com/question/20816420/answer/22984930