批量处理自动生成 HD 文件。

借助 ImageMagick,可以实现自动批处理生成 -HD 图片。


*****************************************************

DOS下,分两个BAT文件。使用时,把这两个文件放在一个文件夹A中,然后,把要命名的png图片全部放在独立的一个文件夹B中,B文件夹也放在A文件夹下。然后使用 scale.bat B,即会生成B-HD文件夹,里面是处理后的图片。


====================

文件scale.bat

====================

if ""=="%1" goto MYEND


cd %1
set aaaa=%CD%
cd ..
del /Q "%aaaa%-hd"
rmdir "%aaaa%-hd"


xcopy /S /E /I "%aaaa%" "%aaaa%-hd"


cd %aaaa%-hd


for /r %%i in (*.png) do call ..\scale_step2.bat %%~ni 


cd ..


:MYEND


===============

文件scale_step2.bat

===============

rename %1.png %1-hd.png
convert -scale 50%% %1-hd.png %1.png
mogrify -strip %1-hd.png









*****************************************************

mac机下,把ImageMagick文件放到~/目录下。分两个.sh文件。使用方法同DOS下


=====================

scale.sh

=====================

if [ "" = "$1" ]; then
    echo 'usage: ./scale.sh dirname'
    exit 1
fi


if [-d $1]; then
el
    echo 'usage: ./scale.sh dirname'
    exit 1
fi


cd $1
aaaa=$(pwd)
echo $aaaa
cd ..
rm -r $aaaa-hd
cp -r $aaaa $aaaa-hd


cd $aaaa-hd


export MAGICK_HOME="$HOME/ImageMagick-6.7.5"
export PATH="$MAGICK_HOME/bin:$PATH"
export DYLD_LIBRARY_PATH="$MAGICK_HOME/lib/"


chmod 777 ../rename.sh


clear


echo cd $aaaa-hd
echo


../rename.sh %.png %-hd.png


for img in *-hd.png
do
convert -scale 50% $img $img.png
mogrify -strip $img
done


echo
../rename.sh %-hd.png.png %.png
echo
cd ..


echo cd $aaaa


echo ""
echo "=========================================="
echo ""
echo "done, see ${aaaa}-hd"
echo ""
echo "=========================================="
echo ""


=====================

rename.sh

=====================

#!/bin/bash


g_splash='\';
g_wildcard='%';
g_spwc="${g_splash}${g_wildcard}";


#把源文件名转换为正则表达式,供ren函数替换文件名用
function cvtSrcNameToPattern
{
    local vfile=$1
    local vsplash2d='\\.';
    local vsplash3LB='\\\(';
    local vsplash3RB='\\\)';
    local finalpattern="";
    finalpattern=`echo "${vfile}" | sed -ne "s/\./${vsplash2d}/g" -ne "s/${g_spwc}/${vsplash3LB}.*${vsplash3RB}/gp"`;
    echo ${finalpattern}
}
#把目标文件名转换为正则表达式,供ren函数替换文件名用
function cvtDestNameToPattern
{
    local vfile=$1;
    local vsplash='\';
    local vsplash2='\\';
    local vsplash2d='\\.';
    vfile=`echo "${vfile}" | sed -ne "s/\./${vsplash2d}/gp"`;
    local vtmp1=${vfile%%${g_spwc}*};
    local vtmp2=${vfile#*${g_spwc}};
    local vDestPattern="";
    declare -i tii;
    tii=1;
    if [ -z "${vtmp1}" ] ; then
vDestPattern+=`echo "${vsplash}${tii}"`;
        vtmp1=${vtmp2%%${g_spwc}*};
        vtmp2=${vtmp2#*${g_spwc}};
tii+=1;
    fi
    while [ \( ! -z "${vtmp1}" \) -a \( ! -z "${vtmp2}" \) -a \( "${vtmp1}" != "${vtmp2}" \) ] ;
    do
vDestPattern+="${vtmp1}";
vDestPattern+=`echo "${vsplash}${tii}"`
vtmp1=${vtmp2%%${g_spwc}*};
vtmp2=${vtmp2#*${g_spwc}};
tii+=1;
    done
    if [ ! -z "${vtmp1}" ] ; then
vDestPattern+="${vtmp1}";
if [ -z "${vtmp2}" ] ; then
     vDestPattern+="${vsplash}${tii}";
fi
    elif [ ! -z "${vtmp2}" ] ; then
        vDestPattern+="${vsplash}${tii}${vtmp2}";
    fi
    echo "${vDestPattern}"
}
#把通配符转换为'*'号,供ren函数查找文件用
function cvtWildCardToAsterisk
{
    echo "$1" | sed -ne "s/${g_wildcard}/\*/gp";
}
#需要注意的是,源文件名中的通配符数量要大于或者等于目标文件名通配符的数量
function ren
{
    local vsrcfile=`echo "$1"`;
    local vdestfile=`echo "$2"`;
    local vsrcPatternFile="";
    if [ $# -ne 2 ] ; then
        echo -e "invalid useage!"
        ren_usage;
    fi
    local tmpfile="";
    local finalDestfile="";
    local tmpSrcPattern="";
    local tmpDestPattern="";
    tmpSrcPattern=`cvtSrcNameToPattern "${vsrcfile}"`;
    tmpDestPattern=`cvtDestNameToPattern "${vdestfile}"`;
    vsrcPatternFile=`cvtWildCardToAsterisk "${vsrcfile}"`;
    for tmpfile in ${vsrcPatternFile}
    do
finalDestfile=`echo "${tmpfile}" | sed -ne "s/${tmpSrcPattern}/${tmpDestPattern}/p"`;
if [ -e "${tmpfile}" ] ;then
     echo "mv \"${tmpfile}\" \"${finalDestfile}\""
     mv "${tmpfile}" "${finalDestfile}"
else
     echo "source file ${vsrcfile} does not exist!!"
fi
    done
}
function ren_usage
{
    echo -e "Usage: ren srcname destname"
    echo -e "Example: \"ren a.c b.c\" or \"ren a.${g_wildcard} b.${g_wildcard}\" or \"ren ${g_wildcard}.c ${g_wildcard}.cpp\""
    echo -e "Note: The wildcard is \"${g_wildcard}\"!"
}
ren "$@"

你可能感兴趣的:(function,正则表达式,dos,Path,library,wildcard)