Search code examples
imagemagickimagemagick-convert

Imagemagick create image and place image inside with max size


I have trimmed .pngs and need them placed on a canvas (3000x3000 px) BUT with a max size.

convert -size 3000x30000 xc:transparent test.png -gravity south -composite -size 2200x2200 result.png

The code I have now works, but the sizing of the image is off. My canvas is 3000x3000px as intended, but the image placed on the canvas doesn't have the correct size. It should have a max width/height of 2200px and if possible be scaled up, if they are to small in height.


Solution

  • This ImageMagick command will resize the input image to 2200 pixels on the longer side while maintaining its aspect, then create a 3000x3000 transparent canvas, then swap the input image and the canvas, and finish by compositing the resized input image onto the transparent canvas...

    convert input.png -resize 2200x2200 \
       -size 3000x3000 xc:none +swap -gravity south -composite result.png
    

    For Windows change that continued line backslash "\" to a caret "^". For ImageMagick v7 use "magick" instead of "convert".