How to insert white space with imagemagick?

My ImageMagick version is '6.7.8-0 2012-07-04 Q16'. According to the docu the answer of @kev command should work:

 convert in.png -gravity east -extent 520x352 out.png

However, like with most ImageMagick questions, you can achieve the same goal with different means. You could use montage like this:

 montage null: in.png -tile 2x1 -geometry +17+0 out1.png

This uses the special 'null:' image to concatenate it with the in.png.


It is true that with convert you need to re-compute the values you need to pass to -extent for each input picture that's got a different size.

First use identify -format to get the dimensions of the image:

 identify -format '%Wx%H' in.png

This should return something like:

 449x352

Ok, now you would need to add your wanted 71 pixels, to get the final 520x352 value. But you don't need to do that calculation in your own brains:

ImageMagick to the rescue!, and its magic calculation capabilities... :-)

You can tell the identify -format command to do that calculation for you:

 identify -format '%[fx:W+71]x%H'

This should now give you a result of:

 520x352

So assuming you want to just pad/add a 'white strip' of 71 pixels width to the left of any picture, you can use the following single commandline:

 convert \
    in.png \
   -gravity east \
   -background white \
   -extent $(identify -format '%[fx:W+71]x%H' in.png) \
    out2.png

Voila! One commandline (which encapsulates 2 commands, to be honest) and you can let this one loose on all your PNGs, JPEGs, GIFs,... in a directory to auto-magickally add your 71pixels white strip to each of them:

 for i in *.png *.jpeg *jpg *.gif; do
    convert \
       ${i} \
      -gravity east \
      -background white \
      -extent $(identify -format '%[fx:W+71]x%H' ${i}) \
       $(convert ${i} -format "71-pixels-padded-left---%t.%e" info:)
 done

For each image its output remains the same filetype. Of course you can enforce all output to be PNG (or whatever you want). Just replace the %t.%e part of the command with %t.png...


As I didn't want the images flush with the right side, I had to use a different approach (ImageMagick's composite tool):

convert -size 500x500 xc:white canvas.png
convert canvas.png in.png -geometry +200+200 -composite out.png

-size should be the final image size you want, canvas.png is a blank white canvas, in.png would be the image you want to pad, and -geometry is the positioning offset.


Documentation: http://www.imagemagick.org/Usage/crop/#extent

convert in.png -gravity east -extent 500x352 out.png

Tags:

Imagemagick