20

What parameters does "convert" (?) need to get this OUTPUT from this INPUT?

INPUT:

enter image description here

OUTPUT:

enter image description here

4 Answers 4

9

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.

22

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...

9

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

convert in.png -gravity east -extent 500x352 out.png
5
  • Thanks!! But what if my picture is not 500x352 - because the size varies? Jul 3, 2012 at 22:44
  • 1
    I just show you how to.
    – kev
    Jul 3, 2012 at 22:58
  • @kev: Your command doesn't exactly do what @gasko peter wanted. You should have used -extend 520x352. +1 for the 'right direction' though :-P Jul 9, 2012 at 18:45
  • sed 's#-extend#-extent#g' Aug 6, 2012 at 20:15
  • I had to add -background transparent to preserve a transparent background.
    – tremby
    Jul 4, 2016 at 23:27
0

I have this command defined in my .bash_profile. It will automatically compute the final image size from the padding you want in width and height (works with zeros):

# arithmetic with awk
calc() { awk "BEGIN { print $* }"; }

# pad an image
img_pad() {
    local width height pw ph 
    [ $# -lt 3 ] && { echo "Usage: img_pad <Input> <WxH> <Output>"; return; }

    # parse current size and padding
    IFS=x read width height <<< "$(identify "$1" | awk '{print $3}')"
    IFS=x read pw ph <<< "$2"

    # new widht and height
    width=$(calc "$width + 2*$pw")
    height=$(calc "$height + 2*$ph")

    echo "Converting '$1' to '$3' with size ${width}x${height} (+w=${pw}, +h=${ph})."
    convert "$1" -gravity center -extent "${width}x${height}" "$3"
}

Example usage:

# pad 50px left and right, no vertical padding
img_pad in.png 50x0 out.png 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.