Page 1 of 1

Center image by extending only one dimension

Posted: 2017-11-02T10:15:22-07:00
by jamadagni
I would like to know how to tell IM to extend only one dimension (X or Y as per my wish), center the image in it and fill the rest with background colour. For example:

Code: Select all

convert -size 1000x1000 xc:white rose: -gravity Center -composite out.png
...will certainly center the rose in the middle of the 1000x1000 white field. But say I want to specify that the rose should be centered in a white field of:
  1. 1000 px width and height same as rose, or
  2. width same as rose and 1000 px height
then how do I do it? Preferably the solution should work with other gravity options such as N, S, E, W too (though the NE, NW etc would be not meaningful). Thanks!

Re: Center image by extending only one dimension

Posted: 2017-11-02T10:19:50-07:00
by fmw42
If you want to shift the position relative to the gravity, then add -geometry +Xoffset+Yoffset after -gravity .

You can also just use -extent. See viewtopic.php?f=1&t=33012

Re: Center image by extending only one dimension

Posted: 2017-11-02T13:13:35-07:00
by GeeMack
jamadagni wrote: 2017-11-02T10:15:22-07:00But say I want to specify that the rose should be centered in a white field of:
  1. 1000 px width and height same as rose, or
  2. width same as rose and 1000 px height
then how do I do it?
As fmw42 mentioned, you can use "-extent" in a fairly simple command like this...

Code: Select all

convert rose: -background white -gravity center -extent 1000x0 out.png
That would create your first example with the rose centered in a white canvas of 1000 pixels wide and the height of the rose. Using zero in that geometry argument tells "-extent" to just use that dimension from the input image. Of course you'd swap the width and height arguments to make the canvas 1000 pixels high and the width of the rose.

Re: Center image by extending only one dimension

Posted: 2017-11-02T16:11:06-07:00
by fmw42
You actually do not need the 0 for height. IM will take the full image height. So you can just do

Code: Select all

convert rose: -background white -gravity center -extent 1000x out.png

Re: Center image by extending only one dimension

Posted: 2017-11-03T01:33:04-07:00
by jamadagni
💐🙏 to both of you!