ImageMagick – How to Crop and Resize Image to Fit A5 PDF

image manipulationimagemagick

I have an image (png, size 4288×2848) that I want to fit in a landscape a5 pdf with a 1cm border.

I know I can do it with ImageMagick but don't know how.

Best Answer

A5 landscape size is 210mmx148mm.
With a 1cm border (i.e. 10mm) that means the image should be 200mm wide and 138mm high hence an aspect ratio of 1.45 - and your original image (4288x2848) is slightly wider: 1.51 aspect ratio. You'll have to crop it first to 4130x2848 so as to have the same aspect ratio.
You could do that by simply using -extent e.g.

convert in.png -extent 4130x2848 out.png

will crop the image to the desired size leaving the top left corner unaffected
or you could instead crop relative to gravity e.g. center:

convert in.png -gravity center -crop 4130x2848+0+0 +repage out.png

whichever suits you better...
Once you have the cropped image you can convert it to pdf; for a page size A5 e.g. @ 300px per inch (which is equivalent to 118px per cm) the size of the canvas is 2480x1748 however if you subtract the 10mm border which is 118px your image should be 2362x1630 and the remaining is the white background so again, with the help of -extent:

convert out.png -resize 2362x1630 -background white -gravity center -extent 2480x1748 out.pdf
Related Question