imagemagick图片处理基础使用
整理些常用的图片处理命令
主要使用ImageMagick, 另GraphicsMagick在处理大图片性能会比ImageMagick高, 但处理gif效率却比ImageMagick低很多
一、 基本
#查看图片其他信息
identify test.jpg
file test.jpg
二、 图片缩略
缩略命令有resize、 thumbnail等, thumbnail效率要高点
一些特殊字符解释:
%: 百分比
^: 指定以那边缩放倍数为准
!: 强制wxh
>: 只有大于指定值才缩放,且以缩放倍数大的边为准
<: 当w/h都小于指定值缩放,强制wxh
@: 图片在面积(wxh)的积, 面积的比例缩放
以上可组合
2.1 直接转格式
convert test.jpg test.webp
# png to ico
magick tech.png -background none -resize 32x32! -density 32x32! favicon.ico
2.2 缩放
#w/h相同倍数缩放
convert test.jpg -resize 50% test2.jpg
#等比缩放, 只指定w/宽,以w/宽为准, 相反以h/高为准
convert test.jpg -thumbnail 1080x test2.jpg
#等比缩放, 以缩放倍数大的为准
convert test.jpg -thumbnail 1080x1080 test2.jpg
#等比缩放, 指定以w/宽为准
convert test.jpg -thumbnail 1080^x300 test2.jpg
#强制图片绽放为1080x1080,图片会变形
convert test.jpg -thumbnail 1080x1080! test2.jpg
#等比缩放, 当w>1080 || h>300 缩放,以缩放倍数大的边为准
convert test.jpg -thumbnail 1080x300> test2.jpg
#等比缩放, 当w>1080 || h>300 缩放,指定以w/宽为准
convert test.jpg -thumbnail 1080^x300> test2.jpg
#gif 需要加coalesce, 防止git处理模糊
convert test.gif -coalesce -thumbnail 100x100 test2.gif
三、 图片水印
dissolve: 透明度
gravity: 位置,取值有NorthWest、North、NorthEast、West、Center、East、SouthWest、South、SouthEast
pointsize: 字体大小
fill: 字体填充色
3.1 图片水印
composite -compose Bumpmap -dissolve 80% -gravity SouthEast -geometry +10+10 \( wm_test.jpg -resize 90% \) test.jpg -alpha set out.jpg
3.2 文字水印
#先将文字转成图片
convert -size 200x200 xc:none -font Cochin.ttc -pointsize 26 -gravity SouthEast -fill black -annotate +0+0 "水印测试" wm_test.jpg
#无背景文字
convert -fill red -pointsize 66 label:"text watermark" -transparent white text_watermark.png
convert -fill "rgb(0,119,228)" -pointsize 66 label:"text watermark" -transparent white text_watermark.png
#合成图片
composite -compose Bumpmap -dissolve 90% -gravity SouthEast -geometry +10+10 wm_test.jpg test.jpg -alpha set out.jpg
四、 制作gif
convert -delay 50 img1.jpg img2.jpg img3.jpg img.gif
五、 图片合成
直接引用官方的例子
http://www.imagemagick.org/Usage/layers/#composite
六、 其它
图片裁剪、去除元信息、高斯模糊处理等,直接附上脚本处理
使用说明: 适配七牛的云处理 https://developer.qiniu.com/dora/api/the-advanced-treatment-of-images-imagemogr2
#!/bin/bash
if [ $# -lt 3 ]; then
>&2 echo "argument error"
exit 1;
fi
input=$1
fop=$2
tmpDir=$3
#定义变量
cur_time=$(date +%Y%m%d%H%M%S-%N)
convert_bin=/usr/local/bin/convert
#截取文件名
input_file_name=${input##*/}
base_input_file_name=${input_file_name%%.*}
suffix=${input_file_name##*.}
output="$tmpDir/imageMogr2-${cur_time}-${base_input_file_name}"
#改变分割符
OIFS="$IFS"
IFS='/'
read -a args <<< "${2}"
IFS="$OIFS"
thumbnail_index=0
crop_index=0
for (( i = 0; i < ${#args[@]}; i++)); do
case "${args[i]}" in
"auto-orient" )
auto_orient="-auto-orient"
;;
"thumbnail" )
thumbnail=${args[i+1]//p/%}
thumbnail=${thumbnail//r/^}
thumbnail=`echo $thumbnail | sed -e 's/^!//'`
thumbnail="-thumbnail $thumbnail"
thumbnail_index=$i
let i+=1
;;
"strip" )
strip="-strip"
;;
"gravity" )
gravity="-gravity ${args[i+1]}"
let i+=1
;;
"crop" )
crop=${args[i+1]//a/+}
crop=${crop//!/}
crop="-crop $crop"
crop_index=$i
let i+=1
;;
"rotate" )
rotate="-rotate ${args[i+1]}"
i=i+1
;;
"format" )
suffix=${args[i+1]}
let i+=1
;;
"blur" )
blur="-blur ${args[i+1]}"
let i+=1
;;
"interlace" )
if [ "${args[i+1]}" -eq "1" ]; then
interlace="-interlace Line"
fi
let i+=1
;;
"quality" )
quality="-quality ${args[i+1]}%"
let i+=1
;;
"size-limit" )
size_limit=${args[i+1]}
let i+=1
;;
esac
done
#判断先裁剪还是先缩略
if [ ${thumbnail_index} -gt ${crop_index} ]; then
thumbnail_crop="$crop $thumbnail"
else
thumbnail_crop="$thumbnail $crop"
fi
$convert_bin $input ${auto_orient} $gravity $thumbnail_crop $strip $rotate $blur $interlace $output"."$suffix
echo $output"."$suffix
Read other posts