Center image by extending only one dimension

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
jamadagni
Posts: 11
Joined: 2013-12-06T19:03:10-07:00
Authentication code: 6789

Center image by extending only one dimension

Post by jamadagni » 2017-11-02T10:15:22-07:00

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!

User avatar
fmw42
Posts: 22087
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Center image by extending only one dimension

Post by fmw42 » 2017-11-02T10:19:50-07:00

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

User avatar
GeeMack
Posts: 469
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Center image by extending only one dimension

Post by GeeMack » 2017-11-02T13:13:35-07:00

jamadagni wrote:
2017-11-02T10:15:22-07:00
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?
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.

User avatar
fmw42
Posts: 22087
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Center image by extending only one dimension

Post by fmw42 » 2017-11-02T16:11:06-07:00

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

jamadagni
Posts: 11
Joined: 2013-12-06T19:03:10-07:00
Authentication code: 6789

Re: Center image by extending only one dimension

Post by jamadagni » 2017-11-03T01:33:04-07:00

💐🙏 to both of you!

Post Reply