70
convert input.png -extent 100x100 -gravity center -background white output.png 

If the input is 50x50 the surrounding background is white. Can I somehow set this to transparent without declaring any color within input as transparent?

2 Answers 2

133

Use this instead:

convert \ input.png \ -background none \ -gravity center \ -extent 100x100 \ output.png 

Note well: The order of the parameters is significant! (To convince yourself, just put -background none at the end of the parameters instead of the start...)


Updated: Thanks to @jesmith who noticed that the commandline I originally provided does no longer work as intended. More recent versions of convert additionally require that the -gravity center is called before -extent 100x100. (This was one of the changes introduced to one ImageMagick's most recent versions [at the time of originally writing this answer]).

Sign up to request clarification or add additional context in comments.

2 Comments

Indeed the order is important, and you got it wrong! You need to put -gravity before -extent, or it will have no effect. Try -gravity SouthEast to see what I mean.
@noɥʇʎPʎzɐɹC: Thx -- fixed.
12

Kurt's note is ironically spot on, order matters greatly. Kurt's command results in gravity not being applied to the extent, so the transparent 'border' will all be to the bottom and/or right of the image.

Moving gravity before extent will correctly create equal transparent 'borders' on all applicable sides.

convert input.jpg -background none -gravity Center -extent 100x50 output.png

1 Comment

+1 and thanks for your additional answer pointing out the now required command line order for -gravity center. I hadn't seen it, nor @jesmith's comment earlier, though he had pointed it out already in May '13.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.