格式转换

将图像转成 png 格式

1
$ convert rose.jpg rose.png

将 pdf 文档转成 png 格式,并设置精度为 300 dpi

1
$ convert letter.pdf -density 300 letter.png

缩放图像

保持高宽比

1
2
$ convert dragon.gif -resize 64x64 resize_dragon.gif
$ convert terminal.gif -resize 64x64 resize_terminal.gif

不保持高宽比

1
2
$ convert dragon.gif    -resize 64x64\!  exact_dragon.gif
$ convert terminal.gif -resize 64x64\! exact_terminal.gif

只缩小大图

1
2
$ convert dragon.gif    -resize 64x64\>  shrink_dragon.gif
$ convert terminal.gif -resize 64x64\> shrink_terminal.gif

只放大小图与此类似,但使用 < 标记。

按比例缩放

1
2
$ convert dragon.gif    -resize 50%  half_dragon.gif
$ convert terminal.gif -resize 50% half_terminal.gif

图像组合

图片拼接

拼接两张图

1
$ montage -background #336699 -geometry +4+4 rose.jpg red-ball.png montage.jpg

加点装饰:

1
$ montage -label %f -frame 5 -background #336699 -geometry +4+4 rose.jpg red-ball.png frame.jpg

拼接多张图

可以通过 -tile 选项指定图片排列的方式。

1
2
3
4
$ montage font_[1-7].gif  -tile 9x1  -geometry 16x16+1+1  tile_9x1.gif
$ montage font_[1-7].gif -tile 4x3 -geometry 16x16+1+1 tile_4x3.gif
$ montage font_[1-7].gif -tile 3x3 -geometry 16x16+1+1 tile_3x3.gif
$ montage font_1.gif -tile 2x3 -geometry 16x16+1+1 tile_2x3.gif

图层叠加

居中叠加

1
$ composite -gravity center  castle.gif  frame.gif  castle_button.gif

指定位置叠加

1
2
$ composite -geometry +31+105  hand_point.gif dragon.gif \
dragon_claw_pointed.jpg

图像水印

简单文字水印

先创建一个文字水印:

1
2
3
4
5
6
7
8
9
10
$ convert -size 300x50 xc:grey30 -font Arial -pointsize 20 -gravity center \
-draw "fill grey70 text 0,0 'Copyright'" \
stamp_fgnd.png
$ convert -size 300x50 xc:black -font Arial -pointsize 20 -gravity center \
-draw "fill white text 1,1 'Copyright' \
text 0,0 'Copyright' \
fill black text -1,-1 'Copyright'" \
+matte stamp_mask.png
$ composite -compose CopyOpacity stamp_mask.png stamp_fgnd.png stamp.png
$ mogrify -trim +repage stamp.png

再把水印添加进图像:

1
2
$ composite -gravity south -geometry +0+10 stamp.png  logo.jpg \
wmark_text_stamped.jpg

整幅图像加水印

1
2
3
4
5
$  convert -size 140x80 xc:none -fill grey \
-gravity NorthWest -draw "text 10,10 'Copyright'" \
-gravity SouthEast -draw "text 5,15 'Copyright'" \
miff:- |\
composite -tile - logo.jpg wmark_text_tiled.jpg

更 fancy 的组合特效

示例1

1
2
3
4
5
6
7
8
$ convert -page +5+5    holocaust_tn.gif \
-page +80+50 spiral_stairs_tn.gif \
-page +40+105 chinese_chess_tn.gif \
+page \
-alpha Set -virtual-pixel transparent \
-channel A -blur 0x10 -level 50,100% +channel \
\( -size 200x200 tile:tile_fabric.gif -alpha Set \) -insert 0 \
-background None -flatten overlap_canvas.jpg

示例2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
center=0   # Start position of the center of the first image.
# This can be ANYTHING, as only relative changes are important.

for image in ../img_photos/[a-m]*_orig.jpg
do

# Add 70 to the previous images relative offset to add to each image
#
center=`convert xc: -format "%[fx: $center +70 ]" info:`

# read image, add fluff, and using centered padding/trim locate the
# center of the image at the next location (relative to the last).
#
convert -size 500x500 "$image" -thumbnail 240x240 \
-set caption '%t' -bordercolor Lavender -background black \
-pointsize 12 -density 96x96 +polaroid -resize 30% \
-gravity center -background None -extent 100x100 -trim \
-repage +${center}+0\! MIFF:-

done |
# read pipeline of positioned images, and merge together
convert -background skyblue MIFF:- -layers merge +repage \
-bordercolor skyblue -border 3x3 overlapped_polaroids.jpg

示例3

1
2
3
4
5
6
7
8
9
10
11
12
13
$ convert thumbnail.gif \
-bordercolor white -border 6 \
-bordercolor grey60 -border 1 \
-bordercolor none -background none \
\( -clone 0 -rotate `convert null: -format '%[fx:rand()*30-15]' info:` \) \
\( -clone 0 -rotate `convert null: -format '%[fx:rand()*30-15]' info:` \) \
\( -clone 0 -rotate `convert null: -format '%[fx:rand()*30-15]' info:` \) \
\( -clone 0 -rotate `convert null: -format '%[fx:rand()*30-15]' info:` \) \
-delete 0 -border 100x80 -gravity center \
-crop 200x160+0+0 +repage -flatten -trim +repage \
-background black \( +clone -shadow 60x4+4+4 \) +swap \
-background none -flatten \
poloroid_stack.png

示例4

1
2
3
4
5
6
7
8
9
10
$ convert -size 150x150 xc:none -background none \
-fill white -stroke grey60 \
-draw "rectangle 0,0 130,100" thumbnail.gif \
-geometry +5+5 -composite -rotate -10 \
-draw "rectangle 0,0 130,100" thumbnail.gif \
-geometry +5+5 -composite -rotate -10 \
-draw "rectangle 0,0 130,100" thumbnail.gif \
-geometry +5+5 -composite -rotate +10 \
-trim +repage -background LightSteelBlue -flatten \
poloroid_spread.gif

图片装饰

添加边框

1
$ convert thumbnail.gif   -mattecolor peru  -frame 9x9+3+3  framed.gif

添加阴影

柔化阴影

1
2
$  convert rose: \( +clone  -background navy  -shadow 80x3+5+5 \) +swap \
-background none -layers merge +repage shadow.png

硬阴影

通过改变 -shadow 的第二个变量 sigmal 来调节柔化程度。如果为 0 ,则产生硬阴影(hard shadow)。

1
2
$ convert rose: \( +clone  -background navy  -shadow 60x0+4+4 \) +swap \
-background none -layers merge +repage shadow_hard.png

控制阴影方向

1
2
$ convert rose: \( +clone -background navy -shadow 80x3-5+5 \) +swap \
-background none -layers merge +repage shadow_other.png

montage 命令结合

1
2
$ montage balloon.gif medical.gif present.gif shading.gif \
-tile x1 -frame 5 -shadow -geometry +5+5 frame_shadow.jpg

注意这个方法只能产生普通的柔化阴影效果,无法控制阴影的颜色、柔化程度、位置等。

添加文本标签

在图像下方添加标签

1
2
$ convert dragon.gif   -background Khaki  label:'Faerie Dragon' \
-gravity Center -append anno_label.jpg

在图像上方添加标签

1
2
$  convert dragon.gif   -background Orange  label:'Faerie Dragon' \
+swap -gravity Center -append anno_label2.jpg

利用 montage 命令

1
2
3
$ montage -label "Faerie Dragon" dragon.gif \
-font Candice -pointsize 15 \
-frame 5 -geometry +0+0 anno_montage2.jpg

更炫的效果

1
2
$ convert -caption "Faerie Dragon" dragon.gif -gravity center \
-background black +polaroid anno_polaroid.png

深入阅读

Comments