ImageMagick常用指令詳解


Imagemagick常用指令

(ImageMagick--藍天白雲)

(ImageMagick官網)

(其他比較有價值的IM參考)

(圖片自動旋轉的前端實現方案)

convert 轉換圖像格式和大小,模糊,裁剪,驅除污點,抖動,臨近,圖片上畫圖片,加入新圖片,生成縮略圖等。

identify 描述一個或較多圖像文件的格式和特性。

composite 根據一個圖片或多個圖片組合生成圖片

 

舉個例子

convert +profile '*' [src]{file}.{ext} -quality 80 -resize '280x140^>' -gravity Center -crop 280x140+0+0 +repage [out]{file}_280x140.{ext}

把一張圖片按80的質量去壓縮(jpg的壓縮參數),同時按圖片比例非強制縮放成不超過280x140的圖片.居中裁剪280x140,去掉圖片裁減后的空白和圖片exif信息,通常這種指令是為了保證圖片大小正好為280x140

 

下面對各個指令的含義簡要說明

 

-quality   圖片質量,jpg默認99,png默認75

 

-resize

100x100      高度和寬度比例保留最高值,高比不變

100x100^     高度和寬度比例保留最低值,寬高比不變

100x100!      寬度和高度強制轉換,忽視寬高比

100x100>     更改長寬,當圖片長或寬超過規定的尺寸

100x100<     更改長寬 只有當圖片長寬都超過規定的尺寸

100x100^>   更改長寬,當圖片長或寬超過規定的尺寸。高度和寬度比例保留最低值

100x100^<   更改長寬,只有當圖片長寬都超過規定的尺寸。高度和寬度比例保留最低值

100             按指定的寬度縮放,保持寬高比例

 x100          按指定高度縮放,保持寬高比

 

-gravity NorthWest, North, NorthEast, West, Center, East,  SouthWest, South, SouthEast截取用的定位指令,定位截取區域在圖片中的方位

 

-crop 200x200+0+0 截取用的截取指令 ,在用定位指令后,按后兩位的偏移值偏移截取范圍左上角的像素后,再按前兩位的數值,從左上角開始截取相應大小的圖片

 

+repage 去掉圖片裁減后的空白


-dissolve 30 設定組合圖片透明度dissolve示例


+/-profile * 去掉/添加圖片exif信息

下面記錄下一些比較復雜一點的指令:

convert +profile '*' [src]{file}.{ext} -quality 90 -resize '700>' /data/tony/watermark_1.png -gravity southeast -compose Dissolve -composite [out]{file}_1024x1024.{ext}

convert +profile '*' [src]{file}.{ext} /data/watermark_1.png -gravity southeast -geometry +10+10 -composite [src]{file}.{ext}

convert +profile '*' [src]{file}.{ext} -coalesce -resize '950x135>' [out]{file}_950x135.{ext}

convert +profile '*' [src]{file}.{ext} -resize '650x500>' miff:- | composite +profile '*' -dissolve 30 -gravity southeast /data/tony/watermark_1.png - [out]{file}_650x500.{ext}

convert [src]{file}.{ext} -quality 80 -resize '190>' -background white -gravity center -extent 190x +repage [out]{file}_width190.{ext}

【/bin/sh^M: bad interpreter: 沒有那個文件或目錄】
vim filename 
然后用命令 
:set ff? #可以看到dos或unix的字樣. 如果的確是dos格式的。 
然后用 
:set ff=unix #把它強制為unix格式的, 然后存盤退出。


###############################################
shell 除法計算
shell計算中使用除法,基本默認上都是整除。
比如:
num1=2
num2=3
num3=`expr $num1 / $num2`
這個時候num3=0 ,是因為是因為expr不支持浮點除法
解決的方法:
num3=`echo "scale=2; $num1/$num2" | bc`
使用bc工具,sclae控制小數點后保留幾位
還有一種方法
awk 'BEGIN{printf "%.2f\n",’$num1‘/’$num2‘}'
如果用百分比表示
awk 'BEGIN{printf "%.2f%\n",(’$num1‘/’$num2‘)*100}'

##############################################
convert +profile '*' -size 950x135 xc:#ffffff [src]{file}.{ext} -resize 950x135 -gravity center -compose Src-over -composite [out]{file}_950x135.{ext}
解讀:該指令會把gif的動態圖拆分成好幾張圖,


-coalesce
完全定義一個GIF動畫序列的每一幀的外觀,形成“電影膠片”動畫

根據它的-處置元數據覆蓋各圖像中的圖像序列,以再現一個動畫中的動畫序列中的每個點的外觀。有的圖片應該是相同的大小,並且為動畫繼續如預期般GIF動畫的工作分配適當的GIF處理設置。這樣的幀更容易查看和比高度優化的GIF圖像疊加處理。

該動畫可以重新優化使用層法“優化”處理后,雖然沒有保證恢復的GIF動畫優化是比原來更好。

##################################################
gif拿到的只是最后一幀的數據,所以gif的圖片在width和height值上有出入
convert +profile '*' tly2.gif -resize "600>" tly2_600.gif
C:\>identify tly2.gif
tly2.gif[0] GIF 341x186 341x186+0+0 8-bit sRGB 256c 191KB 0.000u 0:00.003
tly2.gif[1] GIF 194x173 341x186+128+13 8-bit sRGB 256c 191KB 0.000u 0:00.010
tly2.gif[2] GIF 192x175 341x186+130+11 8-bit sRGB 256c 191KB 0.000u 0:00.013
tly2.gif[3] GIF 80x177 341x186+131+9 8-bit sRGB 256c 191KB 0.000u 0:00.016
...
tly2.gif[30] GIF 164x175 341x186+122+11 8-bit sRGB 256c 191KB 0.000u 0:00.094
tly2.gif[31] GIF 145x172 341x186+122+14 8-bit sRGB 256c 191KB 0.000u 0:00.097
tly2.gif[32] GIF 195x167 341x186+122+19 8-bit sRGB 256c 191KB 0.000u 0:00.100
tly2.gif[33] GIF 196x174 341x186+121+12 8-bit sRGB 256c 191KB 0.000u 0:00.105

 

一、crop_center.sh

如果原圖寬>高:按寬*size/高縮放; 如果原圖寬<高:按高*size/寬縮放; 並居中裁剪size*size

 

#!/bin/bash

export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

filename=$1
filename2=$2
size=$3
resize="70"

wh=($(/usr/local/bin/identify -format "%w %h " $1))

if [ ${wh[0]} -gt ${wh[1]} ]
then
resize=$(echo "${wh[0]}*$size/${wh[1]}+1" | bc)
else
resize=$(echo "${wh[1]}*$size/${wh[0]}+1" | bc)
fi

/usr/local/bin/convert +profile "*" -quality 75 -resize ${resize}x${resize} $filename $filename2

/usr/local/bin/convert +profile "*" -gravity Center -crop ${size}x${size}+0+0 $filename2 $filename2

使用范例:/usr/local/bin/crop_center.sh [src]{file}.{ext} [out]{file}_small.{ext} 100

 

二、tony-convert-1.sh

原圖按1024x1024>等比縮放,並打水印於右下角;如果寬高都小於300,則直接使用原圖

 

#!/bin/sh

# we have 2 arguments: source file name and dest file name
if [ $# -ne 2 ] ; then 
  echo "Usage: tony-convert-1.sh [source path] [dest path]"
  echo "Example: ./tony-convert-1.sh src/1234.jpg out/1234_100x200.jpg"
  exit 1 
fi

srcFile="$1"
dstFile="$2"

index1=`expr index "${srcFile}" /`
index2=`expr index "${srcFile}" .`
file=`expr substr "${srcFile}" \`expr ${index1} + 1\` \`expr ${index2} - ${index1} - 1\``
ext=`expr substr "${srcFile}" \`expr ${index2} + 1\` \`expr length "${srcFile}" - ${index2}\``

#identify image num
picCount=`/usr/local/sbin/bin/bin/identify ${srcFile}|grep -c "${file}.${ext}"`
echo animate pictures: ${picCount}

if [ ${picCount} -gt 1 ] ; then
  echo "No handle for animate pictures"
  cp ${srcFile} ${dstFile} 
  exit 0 
fi

#identify image size
sizeMatrix=`/usr/local/sbin/bin/bin/identify -format "%wx%h %f\n" ${srcFile}`

index1=`expr index "${sizeMatrix}" x`
width=`expr substr "${sizeMatrix}" 1 \`expr ${index1} - 1\``
echo width: ${width}
index2=`expr \`expr index "${sizeMatrix}" ' '\` - ${index1} - 1`
height=`expr substr "${sizeMatrix}" \`expr ${index1} + 1\` ${index2}`
echo height: ${height}

if [ ${width} -lt 300 ] ; then
  if [ ${height} -lt 300 ] ; then
    echo Image size less than 300x300
    cp ${srcFile} ${dstFile}
    exit 0
  fi
fi

if [ `echo ${ext}|tr 'a-z' 'A-Z'` = "BMP" ] ; then
  index1=`expr index "${dstFile}" .`
  dstFile=`expr substr "${dstFile}" 1 \`expr ${index1} - 1\``.jpg 
fi

#convert image/usr/local/sbin/bin/bin/convert +profile '*' ${srcFile} -resize '1024x1024>' /data/tony/watermark_1.png -gravity southeast -compose Dissolve -composite  ${dstFile}

使用范例:/usr/local/sbin/bin/bin/tony-convert-1.sh [src]{file}.{ext} [out]{file}_1024x1024.{ext}

 

三、resizeByWidthOrHeight.sh:

寬大於高時:定寬等比縮放 高自適應;寬小於高時:定高等比縮放,寬自適應

 

#!/bin/sh

# we have 2 arguments: source file name and dest file name
if [ $# -ne 3 ] ; then 
  echo "Usage: resizeByWidthOrHeight.sh [source path] [widthxheigth] [dest path]"
  echo "Example: ./resizeByWidthOrHeight.sh src/1234.jpg 150x150 out/1234_150x2150.jpg"
  exit 1 
fi

srcFile="$1"
dstFile="$3"

destSize="$2"
destWidth=${destSize%x*}
destHeight=${destSize#*x}
echo "destSize: ${destWidth}x${destHeight}"

index1=`expr index "${srcFile}" /`
echo "index1: $index1"
index2=`expr index "${srcFile}" .`
echo "index2: $index2"
file=`expr substr "${srcFile}" \`expr ${index1} + 1\` \`expr ${index2} - ${index1} - 1\``
echo "file: $file"
ext=`expr substr "${srcFile}" \`expr ${index2} + 1\` \`expr length "${srcFile}" - ${index2}\``
echo "ext: $ext"
#identify image num
picCount=`/usr/local/sbin/bin/bin/identify ${srcFile}|grep -c "${file}.${ext}"`
echo animate pictures: ${picCount}

if [ ${picCount} -gt 1 ] ; then
  echo "No handle for animate pictures"
  cp ${srcFile} ${dstFile} 
  exit 0 
fi

#identify image size
sizeMatrix=`/usr/local/sbin/bin/bin/identify -format "%wx%h" ${srcFile}`

echo "index1: $index1"
width=${sizeMatrix%x*}
echo width: ${width}
height=${sizeMatrix#*x}
echo height: ${height}

newSize="${destWidth}x${destHeight}"
# width < height
if [ ${width} -lt ${height} ] ; then
  newSize="'x${destHeight}>'"
else
  newSize="'${destWidth}>'"
fi
echo "newSize: ${newSize}"

# resize
resizeCmd="/usr/local/sbin/bin/bin/convert ${srcFile} -quality 100 -resize ${newSize} ${dstFile}"
echo "resizeCmd: ${resizeCmd}"
eval "${resizeCmd}"

使用范例:/usr/local/sbin/bin/bin/resizeByWidthOrHeight.sh [src]{file}.{ext} 594x395 [out]{file}_594x395.{ext}

 

四、tony-convert-2.sh:

縮放規則:

目標圖寬<高時:定高等比壓縮,寬自適應;

原圖寬高比>目標圖寬高比時:定高等比壓縮,寬自適應;

否則:定寬等比壓縮,高自適應

裁剪規則:默認居中,若目標圖寬>=高,則居左上角

 

#!/bin/sh

# we have 2 arguments: source file name and dest file name
if [ $# -ne 3 ] ; then 
  echo "Usage: tony-convert-2.sh [source path] [widthxheigth] [dest path]"
  echo "Example: ./tony-convert-2.sh src/1234.jpg 150x150 out/1234_150x150.jpg"
  exit 1 
fi

srcFile="$1"
dstFile="$3"

destSize="$2"
destWidth=${destSize%x*}
destHeight=${destSize#*x}
echo "destSize: ${destWidth}x${destHeight}"

index1=`expr index "${srcFile}" /`
echo "index1: $index1"
index2=`expr index "${srcFile}" .`
echo "index2: $index2"
file=`expr substr "${srcFile}" \`expr ${index1} + 1\` \`expr ${index2} - ${index1} - 1\``
echo "file: $file"
ext=`expr substr "${srcFile}" \`expr ${index2} + 1\` \`expr length "${srcFile}" - ${index2}\``
echo "ext: $ext"
#identify image num
picCount=`/usr/local/sbin/bin/bin/identify ${srcFile}|grep -c "${file}.${ext}"`
echo animate pictures: ${picCount}

if [ ${picCount} -gt 1 ] ; then
  echo "No handle for animate pictures"
  cp ${srcFile} ${dstFile} 
  exit 0 
fi

#identify image size
sizeMatrix=`/usr/local/sbin/bin/bin/identify -format "%wx%h" ${srcFile}`

echo "index1: $index1"
width=${sizeMatrix%x*}
echo width: ${width}
height=${sizeMatrix#*x}
echo height: ${height}

if [ `echo ${ext}|tr 'a-z' 'A-Z'` = "BMP" ] ; then
  index1=`expr index "${dstFile}" .`
  dstFile=`expr substr "${dstFile}" 1 \`expr ${index1} - 1\``.jpg 
fi

srcRate=$(echo "scale=9; $width*1.0 / $height*1.0" | bc)
destRate=$(echo "scale=9; $destWidth*1.0 / $destHeight*1.0" | bc)

echo "srcRate: ${srcRate}"
echo "destRate: ${destRate}"

resizeCmd="/usr/local/sbin/bin/bin/convert +profile '*' ${srcFile} -resize '${destWidth}x${destHeight}^>' -grAvity Center -crop ${destWidth}x${destHeight}+0+0 +repage ${dstFile}"
newSize="${destWidth}x${destHeight}"
# width < height
if [ ${destWidth} -lt ${destHeight} ] ; then
  newSize="x${destHeight}>"
elif [ `echo "${srcRate} > ${destRate}" | bc` -eq 1 ] ; then
  newSize="x${destHeight}>"
else
  newSize="${destWidth}>"
fi

grAvityWay="Center"
# width < height
if [ ${destWidth} -ge ${destHeight} ] ; then
  grAvityWay="NorthWest"
fi

# resize
resizeCmd="/usr/local/sbin/bin/bin/convert +profile '*' ${srcFile} -resize '${newSize}' -grAvity ${grAvityWay} -crop ${destWidth}x${destHeight}+0+0 +repage ${dstFile}"
echo "resizeCmd: ${resizeCmd}"
eval "${resizeCmd}"

使用范例:/usr/local/sbin/bin/bin/tony-convert-2.sh [src]{file}.{ext} 170x105 [out]{file}_170x105.{ext}

 

五、tony-convert-3.sh: 
以destWidth表示目的圖寬,destHeight表示目的圖高,persentSize=destWidth/destHeight表示目的圖寬高比;
當原圖寬<destWidth且高<destHeight時,不截取,直接上傳; 
當原圖寬<destWidth,高>destHeight時,以原圖寬為寬居中裁剪 豎圖; 
當原圖寬>destWidth,高<destHeight時,以原圖高為高居中裁剪 橫圖; 
當原圖寬>destWidth,高>destHeight, 
{

當原圖寬/高>persentSize時,將原圖高壓縮為destHeight居中裁剪 
當原圖寬/高<persentSize時,將原圖寬壓縮為destWidth居中裁剪

}

 

!/bin/sh

# we have 2 arguments: source file name and dest file name
if [ $# -ne 3 ] ; then 
  echo "Usage: tony-convert-3.sh [source path] [widthxheigth] [dest path]"
  echo "Example: ./tony-convert-3.sh src/1234.jpg 750x375 out/1234_750x375.jpg"
  exit 1 
fi

srcFile="$1"
dstFile="$3"

destSize="$2"
destWidth=${destSize%x*}
destHeight=${destSize#*x}
echo "destSize: ${destWidth}x${destHeight}"

index1=`expr index "${srcFile}" /`
echo "index1: $index1"
index2=`expr index "${srcFile}" .`
echo "index2: $index2"
file=`expr substr "${srcFile}" \`expr ${index1} + 1\` \`expr ${index2} - ${index1} - 1\``
echo "file: $file"
ext=`expr substr "${srcFile}" \`expr ${index2} + 1\` \`expr length "${srcFile}" - ${index2}\``
echo "ext: $ext"

#identify image num
picCount=`/usr/local/sbin/bin/bin/identify ${srcFile}|grep -c "${file}.${ext}"`
echo animate pictures: ${picCount}

if [ ${picCount} -gt 1 ] ; then
  echo "No handle for animate pictures"
  cp ${srcFile} ${dstFile} 
  exit 0 
fi

#identify image size
sizeMatrix=`/usr/local/sbin/bin/bin/identify -format "%wx%h" ${srcFile}`

echo "index1: $index1"
width=${sizeMatrix%x*}
echo width: ${width}
height=${sizeMatrix#*x}
echo height: ${height}

#if [ `echo ${ext}|tr 'a-z' 'A-Z'` = "BMP" ] ; then
  #index1=`expr index "${dstFile}" .`
  #dstFile=`expr substr "${dstFile}" 1 \`expr ${index1} - 1\``.jpg 
#fi


#newSize="${destWidth}x${destHeight}"
heightPadding="0"
widthPadding="0"
persentSize=`echo "scale=4; ${destWidth}/${destHeight}" | bc`
echo "persentSize: ${persentSize}"

# quality
qualityCmd="/usr/local/sbin/bin/bin/convert ${srcFile} -quality 90 ${dstFile}"
echo "qualityCmd: ${qualityCmd}"
eval "${qualityCmd}"


# width > 750 && height <375  hengTu
if [ ${width} -gt ${destWidth} ] && [ ${height} -lt ${destHeight} ] ; then
  heightPadding=$[ ${destHeight}/2-${height}/2]
  echo heightPadding: ${heightPadding}
  flag=0
# width < 750 && height >375  shuTu
elif [ ${width} -lt ${destWidth} ] && [ ${height} -gt ${destHeight} ] ; then
  widthPadding=$[ ${destWidth}/2-${width}/2 ]
  echo widthPadding: ${widthPadding}
  flag=0
# width > 750 && height >375  resize and crop
elif [ ${width} -gt ${destWidth} ] && [ ${height} -gt ${destHeight} ] && [ $ [ ${width}/${height} ]>${persentSize} ]  ; then
  echo "resize by src's height then crop"
  newSize="'x${destHeight}>'"
  flag=1
elif [ ${width} -gt ${destWidth} ] && [ ${height} -gt ${destHeight} ] && [ $[ ${width}/${height} ]<${persentSize} ]  ; then
  echo "resize by src's width then crop"
  newSize="'${destWidth}>'"
  flag=1
fi
  
echo "newSize: ${newSize}"

# resize
if [ ${flag} -eq 1 ] ; then
  resizeCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -resize ${newSize} ${dstFile}"
  echo "resizeCmd: ${resizeCmd}"
  eval "${resizeCmd}"

  cropCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -gravity Center  -crop ${destSize}+0+0 +repage ${dstFile}"
else
  echo "only crop pic"
  cropCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -gravity Center  -crop ${destSize}+${widthPadding}+${heightPadding} +repage ${dstFile}"
fi

# crop
echo "cropCmd: ${cropCmd}"
eval "${cropCmd}"

使用范例:/usr/local/sbin/bin/bin/tony-convert-3.sh [src]{file}.{ext} 750x375 [out]{file}_750x375.{ext}

 

六、tony-convert-3_bg.sh: 
以destWidth表示目的圖寬,destHeight表示目的圖高,
當原圖寬<destWidth且高<destHeight時,不截取,直接上傳;
當原圖寬<destWidth,高>destHeight時,以原圖寬為寬居中裁剪 樹圖;
當原圖寬>destWidth,高<destHeight時,以原圖高為高居中裁剪 橫圖;
當原圖寬>destWidth,高>destHeight,
{
當原圖寬>高時,將原圖高壓縮為destHeight居中裁剪
當原圖寬<高時,將原圖寬壓縮為destWidth居中裁剪
}
所有情況都要居中補白

 

!/bin/sh

# we have 2 arguments: source file name and dest file name
if [ $# -ne 3 ] ; then 
  echo "Usage: tony-convert-3_bg.sh [source path] [widthxheigth] [dest path]"
  echo "Example: ./tony-convert-3_bg.sh src/1234.jpg 750x375 out/1234_750x375.jpg"
  exit 1 
fi

srcFile="$1"
dstFile="$3"

destSize="$2"
destWidth=${destSize%x*}
destHeight=${destSize#*x}
echo "destSize: ${destWidth}x${destHeight}"

index1=`expr index "${srcFile}" /`
echo "index1: $index1"
index2=`expr index "${srcFile}" .`
echo "index2: $index2"
file=`expr substr "${srcFile}" \`expr ${index1} + 1\` \`expr ${index2} - ${index1} - 1\``
echo "file: $file"
ext=`expr substr "${srcFile}" \`expr ${index2} + 1\` \`expr length "${srcFile}" - ${index2}\``
echo "ext: $ext"

#identify image num
picCount=`/usr/local/sbin/bin/bin/identify ${srcFile}|grep -c "${file}.${ext}"`
echo animate pictures: ${picCount}

if [ ${picCount} -gt 1 ] ; then
  echo "No handle for animate pictures"
  cp ${srcFile} ${dstFile} 
  exit 0 
fi

#identify image size
sizeMatrix=`/usr/local/sbin/bin/bin/identify -format "%wx%h" ${srcFile}`

echo "index1: $index1"
width=${sizeMatrix%x*}
echo width: ${width}
height=${sizeMatrix#*x}
echo height: ${height}

# quality
qualityCmd="/usr/local/sbin/bin/bin/convert ${srcFile} -quality 90 ${dstFile}"
echo "qualityCmd: ${qualityCmd}"
eval "${qualityCmd}"


# width > 750 && height <375  hengTu
flag=0
if [ ${width} -gt ${destWidth} ] && [ ${height} -lt ${destHeight} ] ; then
  heightPadding=$[ ${destHeight}/2-${height}/2]
  echo heightPadding: ${heightPadding}
  flag=0
# width < 750 && height >375  shuTu
elif [ ${width} -lt ${destWidth} ] && [ ${height} -gt ${destHeight} ] ; then
  widthPadding=$[ ${destWidth}/2-${width}/2 ]
  echo widthPadding: ${widthPadding}
  flag=0
# width > 750 && height >375  resize and crop
elif [ ${width} -gt ${destWidth} ] && [ ${height} -gt ${destHeight} ] && [ ${width} -gt ${height} ]  ; then
  echo "resize by src's height then crop"
  newSize="'x${destHeight}>'"
  flag=1
elif [ ${width} -gt ${destWidth} ] && [ ${height} -gt ${destHeight} ] && [ ${width} -lt ${height} ]  ; then
  echo "resize by src's width then crop"
  newSize="'${destWidth}>'"
  flag=1
fi
  
echo "newSize: ${newSize}"

# resize
if [ ${flag} -eq 1 ] ; then
  resizeCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -resize ${newSize} ${dstFile}"
  echo "resizeCmd: ${resizeCmd}"
  eval "${resizeCmd}"
  cropCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -gravity Center -crop ${destSize}+0+0 +repage -background white -compose Copy -gravity center -extent ${destSize} ${dstFile}"
else
  echo "only crop pic"
  cropCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -gravity Center -crop ${destSize}+0+0 +repage -background white -compose Copy -gravity center -extent ${destSize} ${dstFile}"
fi

# crop
echo "cropCmd: ${cropCmd}"
eval "${cropCmd}"

使用范例:【240x240大小,背景補白,1PX灰色邊框】 /usr/local/sbin/bin/bin/tony-convert-3_bg.sh [src]{file}.{ext} 240x240 [out]{file}_240x240.{ext}

 

七、tony-convert-4.sh: 

原圖寬大於destWidth:
{
高>destHeight時:定寬等比縮放 以原圖高為高居中裁剪;
否則:從左上角裁剪
}
原圖寬小於destWidth:
{
高>destHeight時:以原圖高為高居中裁剪;
否則:從左上角裁剪
}

 

#!/bin/sh

# we have 2 arguments: source file name and dest file name
if [ $# -ne 3 ] ; then 
  echo "Usage: tony-convert-4.sh [source path] [widthxheigth] [dest path]"
  echo "Example: ./tony-convert-4.sh src/1234.jpg 150x150 out/1234_150x150.jpg"
  exit 1 
fi

srcFile="$1"
dstFile="$3"

destSize="$2"
destWidth=${destSize%x*}
destHeight=${destSize#*x}
echo "destSize: ${destWidth}x${destHeight}"

index1=`expr index "${srcFile}" /`
echo "index1: $index1"
index2=`expr index "${srcFile}" .`
echo "index2: $index2"
file=`expr substr "${srcFile}" \`expr ${index1} + 1\` \`expr ${index2} - ${index1} - 1\``
echo "file: $file"
ext=`expr substr "${srcFile}" \`expr ${index2} + 1\` \`expr length "${srcFile}" - ${index2}\``
echo "ext: $ext"

#identify image num
picCount=`/usr/local/bin/identify ${srcFile}|grep -c "${file}.${ext}"`
echo animate pictures: ${picCount}

if [ ${picCount} -gt 1 ] ; then
  echo "No handle for animate pictures"
  cp ${srcFile} ${dstFile} 
  exit 0 
fi

#identify image size
sizeMatrix=`/usr/local/bin/identify -format "%wx%h" ${srcFile}`

echo "index1: $index1"
width=${sizeMatrix%x*}
echo width: ${width}
height=${sizeMatrix#*x}
echo height: ${height}

#if [ `echo ${ext}|tr 'a-z' 'A-Z'` = "BMP" ] ; then
  #index1=`expr index "${dstFile}" .`
  #dstFile=`expr substr "${dstFile}" 1 \`expr ${index1} - 1\``.jpg 
#fi


newSize="${destWidth}x${destHeight}"
# width < height
if [ ${width} -gt ${destWidth} ] ; then
  newSize="'${destWidth}>'"
  if [ ${height} -gt ${destHeight}] ; then
  heihgtPadding="'${destHeight}/2'"
  else
  heihgtPadding="'0'"
  fi
else
  newSize="'${width}>'"
  if [ ${height} -gt ${destHeight}] ; then
    heihgtPadding="'${destHeight}/2'"
  else
    heihgtPadding="'0'"
  fi
fi

echo "newSize: ${newSize}"

# resize
resizeCmd="/usr/local/sbin/bin/bin/convert ${srcFile} -quality 90 -resize ${newSize} ${dstFile}"
echo "resizeCmd: ${resizeCmd}"
eval "${resizeCmd}"
# add white ground
addWhiteCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -crop ${destSize}+0-${heihgtPadding} +repage ${dstFile}"
echo "addWhiteCmd: ${addWhiteCmd}"
eval "${addWhiteCmd}"

使用范例:【160*120裁剪或居中加水印】/usr/local/sbin/bin/bin/tony-convert-4.sh [src]{file}.{ext} 160x120 [out]{file}_160.{ext}

 

八、tony-convert-5.sh: 

寬大於高時:定寬等比縮放 高自適應;
寬小於高時:定高等比縮放,寬自適應;
並居中補白

 

#!/bin/sh

# we have 2 arguments: source file name and dest file name
if [ $# -ne 3 ] ; then 
  echo "Usage: tony-convert-5.sh [source path] [widthxheigth] [dest path]"
  echo "Example: ./tony-convert-5.sh src/1234.jpg 150x150 out/1234_150x2150.jpg"
  exit 1 
fi

srcFile="$1"
dstFile="$3"

destSize="$2"
destWidth=${destSize%x*}
destHeight=${destSize#*x}
echo "destSize: ${destWidth}x${destHeight}"

index1=`expr index "${srcFile}" /`
echo "index1: $index1"
index2=`expr index "${srcFile}" .`
echo "index2: $index2"
file=`expr substr "${srcFile}" \`expr ${index1} + 1\` \`expr ${index2} - ${index1} - 1\``
echo "file: $file"
ext=`expr substr "${srcFile}" \`expr ${index2} + 1\` \`expr length "${srcFile}" - ${index2}\``
echo "ext: $ext"
#identify image num
picCount=`/usr/local/bin/identify ${srcFile}|grep -c "${file}.${ext}"`
echo animate pictures: ${picCount}

if [ ${picCount} -gt 1 ] ; then
  echo "No handle for animate pictures"
  cp ${srcFile} ${dstFile} 
  exit 0 
fi

#identify image size
sizeMatrix=`/usr/local/bin/identify -format "%wx%h" ${srcFile}`

echo "index1: $index1"
width=${sizeMatrix%x*}
echo width: ${width}
height=${sizeMatrix#*x}
echo height: ${height}

#if [ ${width} -lt 300 ] ; then
#  if [ ${height} -lt 300 ] ; then
#    echo Image size less than 300x300
#    cp ${srcFile} ${dstFile}
#    exit 0
#  fi
#fi

if [ `echo ${ext}|tr 'a-z' 'A-Z'` = "BMP" ] ; then
  index1=`expr index "${dstFile}" .`
  dstFile=`expr substr "${dstFile}" 1 \`expr ${index1} - 1\``.jpg 
fi
newSize="${destWidth}x${destHeight}"
# width < height
if [ ${width} -lt ${height} ] ; then
  newSize="'x${destHeight}>'"
else
  newSize="'${destWidth}>'"
fi
echo "newSize: ${newSize}"

# resize
resizeCmd="/usr/local/sbin/bin/bin/convert ${srcFile} -quality 80 -resize ${newSize} ${dstFile}"
echo "resizeCmd: ${resizeCmd}"
eval "${resizeCmd}"
# add white ground
addWhiteCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -background white -gravity center -extent ${destSize} -gravity Center -crop ${destSize}+0+0 +repage ${dstFile}"
echo "addWhiteCmd: ${addWhiteCmd}"
eval "${addWhiteCmd}"

使用范例:【正方形圖】/usr/local/sbin/bin/bin/tony-convert-5.sh [src]{file}.{ext} 90x90 [out]{file}_90x90.{ext}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM