游戏SDK

利用imagemagick来处理图片

2018-11-07  本文已影响121人  逆光少年

imagemagick:一个相当强大的图象处理库。

准备工作:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install imagemagick

图片处理

图片处理脚本

1.利用2x图片来生成1x图片的脚本

#! /bin/bash
# File name : convertImage.sh
# Author: Tang Qiao
# 
# print usage
usage() {
    cat << EOF
    Usage:
        convertImage.sh <src directory> <dest directory>
EOF
}
if [ $# -ne 2 ]; then
    usage
    exit 1
fi
SRC_DIR=$1
DEST_DIR=$2
# check src dir
if [ ! -d $SRC_DIR ]; then
    echo "src directory not exist: $SRC_DIR"
    exit 1
fi
# check dest dir
if [ ! -d $DEST_DIR ]; then
    mkdir -p $DEST_DIR
fi
for src_file in $SRC_DIR/*.* ; do
    echo "process file name: $src_file"
    # 获得去掉文件名的纯路径
    src_path=`dirname $src_file`
    # 获得去掉路径的纯文件名
    filename=`basename $src_file`
    # 获得文件名字 (不包括扩展名)
    name=`echo "$filename" | cut -d'.' -f1`
    # remove @2x in filename if there is
    name=`echo "$name" | cut -d"@" -f1`
    # 获得文件扩展名
    extension=`echo "$filename" | cut -d'.' -f2`
    dest_file="$DEST_DIR/${name}.${extension}"
    convert $src_file -resize 50% $dest_file
done

脚本使用方法:

脚本文件路径 源文件夹路径 目标文件夹路径
/Users/juyu/Desktop/convertImage.sh /Users/juyu/Desktop/sourceImage /Users/juyu/Desktop/destiImage

2.检查图片宽高是否是偶数的脚本

#! /bin/bash
# File name : checkImageSize.sh
# Author: Tang Qiao
# 
usage() {
    cat <<EOF
    Usage:
        checkImageSize.sh <directory>
EOF
}
if [ $# -ne 1 ]; then
    usage
    exit 1
fi
SRC_DIR=$1
# check src dir
if [ ! -d $SRC_DIR ]; then
    echo "src directory not exist: $SRC_DIR"
    exit 1
fi
for src_file in $SRC_DIR/*.png ; do
    echo "process file name: $src_file"
    width=`identify -format "%[fx:w]" $src_file`
    height=`identify -format "%[fx:h]" $src_file`
    # check width
    modValue=`awk -v a=$width 'BEGIN{printf "%d", a % 2}'`
    if [ "$modValue" == "1" ]; then
       echo "[Error], the file $src_file width is $width" 
    fi
    # check height
    modValue=`awk -v a=$height 'BEGIN{printf "%d", a % 2}'`
    if [ "$modValue" == "1" ]; then
       echo "[Error], the file $src_file height is $height" 
    fi
done

脚本使用方法:

脚本文件路径 文件夹路径
/Users/juyu/Desktop/convertImage.sh /Users/juyu/Desktop/sourceImage

修改图片文件 Hash 值

使用 ImageMagick 对 png 图片做轻量压缩,及不损失图片质量,又可改变图片文件 hash 值。方法:
1.切换至工程目录
2.执行

find . -iname "*.png" -exec echo {} \; -exec convert {} {} \;

参考文章:
http://blog.devtang.com/2012/08/26/use-script-to-power-up-ui-work/
https://github.com/oneyian/SpamCode

上一篇 下一篇

猜你喜欢

热点阅读