For example: An image which is 400×300 pixels shrunk to fit a 100×100 pixel box will normal (with the above) have a size of 100×75 pixels. We want to add some padding borders to the top and bottom of the image (and to the sides to be sure) to make the final thumbnail image always 100×100 pixels in size.
There are a number of ways to do this, and as of IM v6.3.2 the best way is using the “-extent” option.
convert -define jpeg:size=200x200 hatching_orig.jpg -thumbnail '100x100>' \
-background skyblue -gravity center -extent 100x100 pad_extent.gif
Before IM v6.3.2 the best way was to add extra borders and do a centered “-crop” on the image after adding a large border around the image.
convert -define jpeg:size=200x200 hatching_orig.jpg -thumbnail '100x100>' \
-bordercolor skyblue -border 50 \
-gravity center -crop 100x100+0+0 +repage pad_crop.gif
The “+repage” operator is important to remove any ‘virtual canvas’ or ‘page’ information that crop preserves. IM v5 users need to use “-page +0+0” instead.
As of IM version 6.2.5, you can also use a Viewport Crop, and flatten the result onto a background color.
convert -define jpeg:size=200x200 hatching_orig.jpg -thumbnail '100x100>' \
-gravity center -crop 120x120+0+0\! \
-background skyblue -flatten pad_view.gif
Another method to pad out an image is to overlay the thumbnail onto a background image (actual image, solid color or tiled canvas) that is the right size, in this case the 128×128 “granite:” built-in image.
convert -define jpeg:size=200x200 hatching_orig.jpg -thumbnail '100x100>' \
granite: +swap -gravity center -composite pad_compose.gif
图片合并的方法
composite -define jpeg:size=200x200 -resize '100x100>' -gravity
center hatching_orig.jpg background.gif composite.gif
This method is probably the best method to use with older versions of IM (such as IM v5), though the “-composite” operation will need to be done by the separate “composite” command, rather than the above single command method.