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

复制代码

1#!/bin/bash 2 3export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin 4 5filename=$1 6filename2=$2 7size=$3 8resize="70" 9 10wh=($(/usr/local/bin/identify -format "%w %h " $1)) 11 12if [ ${wh[0]} -gt ${wh[1]} ] 13then 14resize=$(echo "${wh[0]}*$size/${wh[1]}+1" | bc) 15else 16resize=$(echo "${wh[1]}*$size/${wh[0]}+1" | bc) 17fi 18 19/usr/local/bin/convert +profile "*" -quality 75 -resize ${resize}x${resize} $filename $filename2 20 21/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,则直接使用原图

复制代码

1#!/bin/sh 2 3# we have 2 arguments: source file name and dest file name 4if [ $# -ne 2 ] ; then  5  echo "Usage: tony-convert-1.sh [source path] [dest path]" 6  echo "Example: ./tony-convert-1.sh src/1234.jpg out/1234_100x200.jpg" 7  exit 1  8fi 9 10srcFile="$1" 11dstFile="$2" 12 13index1=`expr index "${srcFile}" /` 14index2=`expr index "${srcFile}" .` 15file=`expr substr "${srcFile}" \`expr ${index1} + 1\` \`expr ${index2} - ${index1} - 1\`` 16ext=`expr substr "${srcFile}" \`expr ${index2} + 1\` \`expr length "${srcFile}" - ${index2}\`` 17 18#identify image num 19picCount=`/usr/local/sbin/bin/bin/identify ${srcFile}|grep -c "${file}.${ext}"` 20echo animate pictures: ${picCount} 21 22if [ ${picCount} -gt 1 ] ; then 23  echo "No handle for animate pictures" 24  cp ${srcFile} ${dstFile}  25  exit 0  26fi 27 28#identify image size 29sizeMatrix=`/usr/local/sbin/bin/bin/identify -format "%wx%h %f\n" ${srcFile}` 30 31index1=`expr index "${sizeMatrix}" x` 32width=`expr substr "${sizeMatrix}" 1 \`expr ${index1} - 1\`` 33echo width: ${width} 34index2=`expr \`expr index "${sizeMatrix}" ' '\` - ${index1} - 1` 35height=`expr substr "${sizeMatrix}" \`expr ${index1} + 1\` ${index2}` 36echo height: ${height} 37 38if [ ${width} -lt 300 ] ; then 39  if [ ${height} -lt 300 ] ; then 40    echo Image size less than 300x300 41    cp ${srcFile} ${dstFile} 42    exit 0 43  fi 44fi 45 46if [ `echo ${ext}|tr 'a-z' 'A-Z'` = "BMP" ] ; then 47  index1=`expr index "${dstFile}" .` 48  dstFile=`expr substr "${dstFile}" 1 \`expr ${index1} - 1\``.jpg  49fi 50 51#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:

宽大于高时:定宽等比缩放 高自适应;宽小于高时:定高等比缩放,宽自适应

复制代码

1#!/bin/sh 2 3# we have 2 arguments: source file name and dest file name 4if [ $# -ne 3 ] ; then  5  echo "Usage: resizeByWidthOrHeight.sh [source path] [widthxheigth] [dest path]" 6  echo "Example: ./resizeByWidthOrHeight.sh src/1234.jpg 150x150 out/1234_150x2150.jpg" 7  exit 1  8fi 9 10srcFile="$1" 11dstFile="$3" 12 13destSize="$2" 14destWidth=${destSize%x*} 15destHeight=${destSize#*x} 16echo "destSize: ${destWidth}x${destHeight}" 17 18index1=`expr index "${srcFile}" /` 19echo "index1: $index1" 20index2=`expr index "${srcFile}" .` 21echo "index2: $index2" 22file=`expr substr "${srcFile}" \`expr ${index1} + 1\` \`expr ${index2} - ${index1} - 1\`` 23echo "file: $file" 24ext=`expr substr "${srcFile}" \`expr ${index2} + 1\` \`expr length "${srcFile}" - ${index2}\`` 25echo "ext: $ext" 26#identify image num 27picCount=`/usr/local/sbin/bin/bin/identify ${srcFile}|grep -c "${file}.${ext}"` 28echo animate pictures: ${picCount} 29 30if [ ${picCount} -gt 1 ] ; then 31  echo "No handle for animate pictures" 32  cp ${srcFile} ${dstFile}  33  exit 0  34fi 35 36#identify image size 37sizeMatrix=`/usr/local/sbin/bin/bin/identify -format "%wx%h" ${srcFile}` 38 39echo "index1: $index1" 40width=${sizeMatrix%x*} 41echo width: ${width} 42height=${sizeMatrix#*x} 43echo height: ${height} 44 45newSize="${destWidth}x${destHeight}" 46# width < height 47if [ ${width} -lt ${height} ] ; then 48  newSize="'x${destHeight}>'" 49else 50  newSize="'${destWidth}>'" 51fi 52echo "newSize: ${newSize}" 53 54# resize 55resizeCmd="/usr/local/sbin/bin/bin/convert ${srcFile} -quality 100 -resize ${newSize} ${dstFile}" 56echo "resizeCmd: ${resizeCmd}" 57eval "${resizeCmd}"

复制代码

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

四、tony-convert-2.sh:

缩放规则:

目标图宽<高时:定高等比压缩,宽自适应;

原图宽高比>目标图宽高比时:定高等比压缩,宽自适应;

否则:定宽等比压缩,高自适应

裁剪规则:默认居中,若目标图宽>=高,则居左上角

复制代码

1#!/bin/sh 2 3# we have 2 arguments: source file name and dest file name 4if [ $# -ne 3 ] ; then  5  echo "Usage: tony-convert-2.sh [source path] [widthxheigth] [dest path]" 6  echo "Example: ./tony-convert-2.sh src/1234.jpg 150x150 out/1234_150x150.jpg" 7  exit 1  8fi 9 10srcFile="$1" 11dstFile="$3" 12 13destSize="$2" 14destWidth=${destSize%x*} 15destHeight=${destSize#*x} 16echo "destSize: ${destWidth}x${destHeight}" 17 18index1=`expr index "${srcFile}" /` 19echo "index1: $index1" 20index2=`expr index "${srcFile}" .` 21echo "index2: $index2" 22file=`expr substr "${srcFile}" \`expr ${index1} + 1\` \`expr ${index2} - ${index1} - 1\`` 23echo "file: $file" 24ext=`expr substr "${srcFile}" \`expr ${index2} + 1\` \`expr length "${srcFile}" - ${index2}\`` 25echo "ext: $ext" 26#identify image num 27picCount=`/usr/local/sbin/bin/bin/identify ${srcFile}|grep -c "${file}.${ext}"` 28echo animate pictures: ${picCount} 29 30if [ ${picCount} -gt 1 ] ; then 31  echo "No handle for animate pictures" 32  cp ${srcFile} ${dstFile}  33  exit 0  34fi 35 36#identify image size 37sizeMatrix=`/usr/local/sbin/bin/bin/identify -format "%wx%h" ${srcFile}` 38 39echo "index1: $index1" 40width=${sizeMatrix%x*} 41echo width: ${width} 42height=${sizeMatrix#*x} 43echo height: ${height} 44 45if [ `echo ${ext}|tr 'a-z' 'A-Z'` = "BMP" ] ; then 46  index1=`expr index "${dstFile}" .` 47  dstFile=`expr substr "${dstFile}" 1 \`expr ${index1} - 1\``.jpg  48fi 49 50srcRate=$(echo "scale=9; $width*1.0 / $height*1.0" | bc) 51destRate=$(echo "scale=9; $destWidth*1.0 / $destHeight*1.0" | bc) 52 53echo "srcRate: ${srcRate}" 54echo "destRate: ${destRate}" 55 56resizeCmd="/usr/local/sbin/bin/bin/convert +profile '*' ${srcFile} -resize '${destWidth}x${destHeight}^>' -grAvity Center -crop ${destWidth}x${destHeight}+0+0 +repage ${dstFile}" 57newSize="${destWidth}x${destHeight}" 58# width < height 59if [ ${destWidth} -lt ${destHeight} ] ; then 60  newSize="x${destHeight}>" 61elif [ `echo "${srcRate} > ${destRate}" | bc` -eq 1 ] ; then 62  newSize="x${destHeight}>" 63else 64  newSize="${destWidth}>" 65fi 66 67grAvityWay="Center" 68# width < height 69if [ ${destWidth} -ge ${destHeight} ] ; then 70  grAvityWay="NorthWest" 71fi 72 73# resize 74resizeCmd="/usr/local/sbin/bin/bin/convert +profile '*' ${srcFile} -resize '${newSize}' -grAvity ${grAvityWay} -crop ${destWidth}x${destHeight}+0+0 +repage ${dstFile}" 75echo "resizeCmd: ${resizeCmd}" 76eval "${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居中裁剪

}

复制代码

1!/bin/sh 2 3# we have 2 arguments: source file name and dest file name 4if [ $# -ne 3 ] ; then  5  echo "Usage: tony-convert-3.sh [source path] [widthxheigth] [dest path]" 6  echo "Example: ./tony-convert-3.sh src/1234.jpg 750x375 out/1234_750x375.jpg" 7  exit 1  8fi 9 10srcFile="$1" 11dstFile="$3" 12 13destSize="$2" 14destWidth=${destSize%x*} 15destHeight=${destSize#*x} 16echo "destSize: ${destWidth}x${destHeight}" 17 18index1=`expr index "${srcFile}" /` 19echo "index1: $index1" 20index2=`expr index "${srcFile}" .` 21echo "index2: $index2" 22file=`expr substr "${srcFile}" \`expr ${index1} + 1\` \`expr ${index2} - ${index1} - 1\`` 23echo "file: $file" 24ext=`expr substr "${srcFile}" \`expr ${index2} + 1\` \`expr length "${srcFile}" - ${index2}\`` 25echo "ext: $ext" 26 27#identify image num 28picCount=`/usr/local/sbin/bin/bin/identify ${srcFile}|grep -c "${file}.${ext}"` 29echo animate pictures: ${picCount} 30 31if [ ${picCount} -gt 1 ] ; then 32  echo "No handle for animate pictures" 33  cp ${srcFile} ${dstFile}  34  exit 0  35fi 36 37#identify image size 38sizeMatrix=`/usr/local/sbin/bin/bin/identify -format "%wx%h" ${srcFile}` 39 40echo "index1: $index1" 41width=${sizeMatrix%x*} 42echo width: ${width} 43height=${sizeMatrix#*x} 44echo height: ${height} 45 46#if [ `echo ${ext}|tr 'a-z' 'A-Z'` = "BMP" ] ; then 47  #index1=`expr index "${dstFile}" .` 48  #dstFile=`expr substr "${dstFile}" 1 \`expr ${index1} - 1\``.jpg  49#fi 50 51 52#newSize="${destWidth}x${destHeight}" 53heightPadding="0" 54widthPadding="0" 55persentSize=`echo "scale=4; ${destWidth}/${destHeight}" | bc` 56echo "persentSize: ${persentSize}" 57 58# quality 59qualityCmd="/usr/local/sbin/bin/bin/convert ${srcFile} -quality 90 ${dstFile}" 60echo "qualityCmd: ${qualityCmd}" 61eval "${qualityCmd}" 62 63 64# width > 750 && height <375  hengTu 65if [ ${width} -gt ${destWidth} ] && [ ${height} -lt ${destHeight} ] ; then 66  heightPadding=$[ ${destHeight}/2-${height}/2] 67  echo heightPadding: ${heightPadding} 68  flag=0 69# width < 750 && height >375  shuTu 70elif [ ${width} -lt ${destWidth} ] && [ ${height} -gt ${destHeight} ] ; then 71  widthPadding=$[ ${destWidth}/2-${width}/2 ] 72  echo widthPadding: ${widthPadding} 73  flag=0 74# width > 750 && height >375  resize and crop 75elif [ ${width} -gt ${destWidth} ] && [ ${height} -gt ${destHeight} ] && [ $ [ ${width}/${height} ]>${persentSize} ]  ; then 76  echo "resize by src's height then crop" 77  newSize="'x${destHeight}>'" 78  flag=1 79elif [ ${width} -gt ${destWidth} ] && [ ${height} -gt ${destHeight} ] && [ $[ ${width}/${height} ]<${persentSize} ]  ; then 80  echo "resize by src's width then crop" 81  newSize="'${destWidth}>'" 82  flag=1 83fi 84   85echo "newSize: ${newSize}" 86 87# resize 88if [ ${flag} -eq 1 ] ; then 89  resizeCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -resize ${newSize} ${dstFile}" 90  echo "resizeCmd: ${resizeCmd}" 91  eval "${resizeCmd}" 92 93  cropCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -gravity Center  -crop ${destSize}+0+0 +repage ${dstFile}" 94else 95  echo "only crop pic" 96  cropCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -gravity Center  -crop ${destSize}+${widthPadding}+${heightPadding} +repage ${dstFile}" 97fi 98 99# crop 100echo "cropCmd: ${cropCmd}" 101eval "${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居中裁剪
}
所有情况都要居中补白

复制代码

1!/bin/sh 2 3# we have 2 arguments: source file name and dest file name 4if [ $# -ne 3 ] ; then  5  echo "Usage: tony-convert-3_bg.sh [source path] [widthxheigth] [dest path]" 6  echo "Example: ./tony-convert-3_bg.sh src/1234.jpg 750x375 out/1234_750x375.jpg" 7  exit 1  8fi 9 10srcFile="$1" 11dstFile="$3" 12 13destSize="$2" 14destWidth=${destSize%x*} 15destHeight=${destSize#*x} 16echo "destSize: ${destWidth}x${destHeight}" 17 18index1=`expr index "${srcFile}" /` 19echo "index1: $index1" 20index2=`expr index "${srcFile}" .` 21echo "index2: $index2" 22file=`expr substr "${srcFile}" \`expr ${index1} + 1\` \`expr ${index2} - ${index1} - 1\`` 23echo "file: $file" 24ext=`expr substr "${srcFile}" \`expr ${index2} + 1\` \`expr length "${srcFile}" - ${index2}\`` 25echo "ext: $ext" 26 27#identify image num 28picCount=`/usr/local/sbin/bin/bin/identify ${srcFile}|grep -c "${file}.${ext}"` 29echo animate pictures: ${picCount} 30 31if [ ${picCount} -gt 1 ] ; then 32  echo "No handle for animate pictures" 33  cp ${srcFile} ${dstFile}  34  exit 0  35fi 36 37#identify image size 38sizeMatrix=`/usr/local/sbin/bin/bin/identify -format "%wx%h" ${srcFile}` 39 40echo "index1: $index1" 41width=${sizeMatrix%x*} 42echo width: ${width} 43height=${sizeMatrix#*x} 44echo height: ${height} 45 46# quality 47qualityCmd="/usr/local/sbin/bin/bin/convert ${srcFile} -quality 90 ${dstFile}" 48echo "qualityCmd: ${qualityCmd}" 49eval "${qualityCmd}" 50 51 52# width > 750 && height <375  hengTu 53flag=0 54if [ ${width} -gt ${destWidth} ] && [ ${height} -lt ${destHeight} ] ; then 55  heightPadding=$[ ${destHeight}/2-${height}/2] 56  echo heightPadding: ${heightPadding} 57  flag=0 58# width < 750 && height >375  shuTu 59elif [ ${width} -lt ${destWidth} ] && [ ${height} -gt ${destHeight} ] ; then 60  widthPadding=$[ ${destWidth}/2-${width}/2 ] 61  echo widthPadding: ${widthPadding} 62  flag=0 63# width > 750 && height >375  resize and crop 64elif [ ${width} -gt ${destWidth} ] && [ ${height} -gt ${destHeight} ] && [ ${width} -gt ${height} ]  ; then 65  echo "resize by src's height then crop" 66  newSize="'x${destHeight}>'" 67  flag=1 68elif [ ${width} -gt ${destWidth} ] && [ ${height} -gt ${destHeight} ] && [ ${width} -lt ${height} ]  ; then 69  echo "resize by src's width then crop" 70  newSize="'${destWidth}>'" 71  flag=1 72fi 73   74echo "newSize: ${newSize}" 75 76# resize 77if [ ${flag} -eq 1 ] ; then 78  resizeCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -resize ${newSize} ${dstFile}" 79  echo "resizeCmd: ${resizeCmd}" 80  eval "${resizeCmd}" 81  cropCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -gravity Center -crop ${destSize}+0+0 +repage -background white -compose Copy -gravity center -extent ${destSize} ${dstFile}" 82else 83  echo "only crop pic" 84  cropCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -gravity Center -crop ${destSize}+0+0 +repage -background white -compose Copy -gravity center -extent ${destSize} ${dstFile}" 85fi 86 87# crop 88echo "cropCmd: ${cropCmd}" 89eval "${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时:以原图高为高居中裁剪;
否则:从左上角裁剪
}

复制代码

1#!/bin/sh 2 3# we have 2 arguments: source file name and dest file name 4if [ $# -ne 3 ] ; then  5  echo "Usage: tony-convert-4.sh [source path] [widthxheigth] [dest path]" 6  echo "Example: ./tony-convert-4.sh src/1234.jpg 150x150 out/1234_150x150.jpg" 7  exit 1  8fi 9 10srcFile="$1" 11dstFile="$3" 12 13destSize="$2" 14destWidth=${destSize%x*} 15destHeight=${destSize#*x} 16echo "destSize: ${destWidth}x${destHeight}" 17 18index1=`expr index "${srcFile}" /` 19echo "index1: $index1" 20index2=`expr index "${srcFile}" .` 21echo "index2: $index2" 22file=`expr substr "${srcFile}" \`expr ${index1} + 1\` \`expr ${index2} - ${index1} - 1\`` 23echo "file: $file" 24ext=`expr substr "${srcFile}" \`expr ${index2} + 1\` \`expr length "${srcFile}" - ${index2}\`` 25echo "ext: $ext" 26 27#identify image num 28picCount=`/usr/local/bin/identify ${srcFile}|grep -c "${file}.${ext}"` 29echo animate pictures: ${picCount} 30 31if [ ${picCount} -gt 1 ] ; then 32  echo "No handle for animate pictures" 33  cp ${srcFile} ${dstFile}  34  exit 0  35fi 36 37#identify image size 38sizeMatrix=`/usr/local/bin/identify -format "%wx%h" ${srcFile}` 39 40echo "index1: $index1" 41width=${sizeMatrix%x*} 42echo width: ${width} 43height=${sizeMatrix#*x} 44echo height: ${height} 45 46#if [ `echo ${ext}|tr 'a-z' 'A-Z'` = "BMP" ] ; then 47  #index1=`expr index "${dstFile}" .` 48  #dstFile=`expr substr "${dstFile}" 1 \`expr ${index1} - 1\``.jpg  49#fi 50 51 52newSize="${destWidth}x${destHeight}" 53# width < height 54if [ ${width} -gt ${destWidth} ] ; then 55  newSize="'${destWidth}>'" 56  if [ ${height} -gt ${destHeight}] ; then 57  heihgtPadding="'${destHeight}/2'" 58  else 59  heihgtPadding="'0'" 60  fi 61else 62  newSize="'${width}>'" 63  if [ ${height} -gt ${destHeight}] ; then 64    heihgtPadding="'${destHeight}/2'" 65  else 66    heihgtPadding="'0'" 67  fi 68fi 69 70echo "newSize: ${newSize}" 71 72# resize 73resizeCmd="/usr/local/sbin/bin/bin/convert ${srcFile} -quality 90 -resize ${newSize} ${dstFile}" 74echo "resizeCmd: ${resizeCmd}" 75eval "${resizeCmd}" 76# add white ground 77addWhiteCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -crop ${destSize}+0-${heihgtPadding} +repage ${dstFile}" 78echo "addWhiteCmd: ${addWhiteCmd}" 79eval "${addWhiteCmd}"

复制代码

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

八、tony-convert-5.sh:

宽大于高时:定宽等比缩放 高自适应;
宽小于高时:定高等比缩放,宽自适应;
并居中补白

复制代码

1#!/bin/sh 2 3# we have 2 arguments: source file name and dest file name 4if [ $# -ne 3 ] ; then  5  echo "Usage: tony-convert-5.sh [source path] [widthxheigth] [dest path]" 6  echo "Example: ./tony-convert-5.sh src/1234.jpg 150x150 out/1234_150x2150.jpg" 7  exit 1  8fi 9 10srcFile="$1" 11dstFile="$3" 12 13destSize="$2" 14destWidth=${destSize%x*} 15destHeight=${destSize#*x} 16echo "destSize: ${destWidth}x${destHeight}" 17 18index1=`expr index "${srcFile}" /` 19echo "index1: $index1" 20index2=`expr index "${srcFile}" .` 21echo "index2: $index2" 22file=`expr substr "${srcFile}" \`expr ${index1} + 1\` \`expr ${index2} - ${index1} - 1\`` 23echo "file: $file" 24ext=`expr substr "${srcFile}" \`expr ${index2} + 1\` \`expr length "${srcFile}" - ${index2}\`` 25echo "ext: $ext" 26#identify image num 27picCount=`/usr/local/bin/identify ${srcFile}|grep -c "${file}.${ext}"` 28echo animate pictures: ${picCount} 29 30if [ ${picCount} -gt 1 ] ; then 31  echo "No handle for animate pictures" 32  cp ${srcFile} ${dstFile}  33  exit 0  34fi 35 36#identify image size 37sizeMatrix=`/usr/local/bin/identify -format "%wx%h" ${srcFile}` 38 39echo "index1: $index1" 40width=${sizeMatrix%x*} 41echo width: ${width} 42height=${sizeMatrix#*x} 43echo height: ${height} 44 45#if [ ${width} -lt 300 ] ; then 46#  if [ ${height} -lt 300 ] ; then 47#    echo Image size less than 300x300 48#    cp ${srcFile} ${dstFile} 49#    exit 0 50#  fi 51#fi 52 53if [ `echo ${ext}|tr 'a-z' 'A-Z'` = "BMP" ] ; then 54  index1=`expr index "${dstFile}" .` 55  dstFile=`expr substr "${dstFile}" 1 \`expr ${index1} - 1\``.jpg  56fi 57newSize="${destWidth}x${destHeight}" 58# width < height 59if [ ${width} -lt ${height} ] ; then 60  newSize="'x${destHeight}>'" 61else 62  newSize="'${destWidth}>'" 63fi 64echo "newSize: ${newSize}" 65 66# resize 67resizeCmd="/usr/local/sbin/bin/bin/convert ${srcFile} -quality 80 -resize ${newSize} ${dstFile}" 68echo "resizeCmd: ${resizeCmd}" 69eval "${resizeCmd}" 70# add white ground 71addWhiteCmd="/usr/local/sbin/bin/bin/convert ${dstFile} -background white -gravity center -extent ${destSize} -gravity Center -crop ${destSize}+0+0 +repage ${dstFile}" 72echo "addWhiteCmd: ${addWhiteCmd}" 73eval "${addWhiteCmd}"

复制代码

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

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

PHP imagick 实现 PDF 转 图片

1、ImageMagick安装官网(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.imagemagick.org%2Fscript%2Finstallsource.php)tarzxvfImageMagick.tar.gzC/usr/lo

ImageMagick简介、GraphicsMagick、命令行使用示例

ImageMagick资料\ImageMagick官网(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.image