Page 1 of 1

Horizontally center an image over another

Posted: 2014-07-11T23:02:13-07:00
by jamespharvey
I originally tried -gravity center -page +{x},+{y}, and discovered -gravity doesn't affect -page. I found a forum post with a similar issue, and have studied all the links and am just barely missing getting it right despite having tried all sorts of combinations.

What I'm trying to do is create two layers. (This is a reduced case from what I'm actually doing, I know there's simpler ways to achieve just what's shown here.)

Layer 1 - A white background
Layer 2 - A text box with a bit of space around it and a black line around it.

I want Layer 2 to be positioned centered horizontally, but at a specific y coordinate. I don't care if it flows down from the specified y coordinate or is vertically centered on it - I can handle either of those cases.

This is my most recent try:

Code: Select all

convert \
   \( -size 1000x1000 xc:white -write mpr:bg +delete \) \
   \( -size 0x0 -background "#68EBFE" -fill black -pointsize 24 label:"This is a label which I want centered" -bordercolor "#68EBFE" -border 10x10 -bordercolor black -border 1x1 -write mpr:textLabel +delete \) \
   \( mpr:bg -gravity center -geometry +500+50 mpr:textLabel -composite -write output.jpeg +delete \) \
   null: null:
Almost works, but it's centered vertically on the entire canvas rather than around the y offset I'm trying to give of 50. The geometry argument doesn't seem to do anything. My intention with it is to specify that I want "mpr:textLabel" to be centered around the point at 500,50. (Centered horizontally on the whole canvas, but centered vertically around y=50.)

Re: Horizontally center an image over another

Posted: 2014-07-12T00:57:07-07:00
by Bonzo
As you say -geometry does not seem to have any effect; from memory this has been a problem; you need to specify your IM version as there may have been a fix.
The other way may be to have two separate commands and composite them.
Hopefully somebody with more experience will drop by later.

Re: Horizontally center an image over another

Posted: 2014-07-12T03:33:39-07:00
by snibgo
When using "-geometry" to compose two images together, it needs to come after the images, not before.

"-gravity North" will centre horizontally, at the top. So an offset is relative to that position, which I think is what is asked for.

Translated to Windows BAT syntax:

Code: Select all

%IM%convert ^
   ( -size 1000x1000 xc:white -write mpr:bg +delete ) ^
   ( -size 0x0 -background "#68EBFE" -fill black -pointsize 24 label:"This is a label which I want centered" -bordercolor "#68EBFE" -border 10x10 -bordercolor black -border 1x1 -write mpr:textLabel +delete ) ^
   ( mpr:bg mpr:textLabel -gravity North -geometry +0+50 -composite -write output.jpeg +delete ) ^
   null: null:
All those "mpr:"s can be simplified:

Code: Select all

%IM%convert ^
   -size 1000x1000 xc:white ^
   ( -size 0x0 -background "#68EBFE" -fill black -pointsize 24 label:"This is a label which I want centered" -bordercolor "#68EBFE" -border 10x10 -bordercolor black -border 1x1 ) ^
   -gravity North -geometry +0+50 -composite ^
   output.jpeg
As the background is a plain colour, "-extent" is faster. Note that the sign is changed, "-50".

Code: Select all

%IM%convert ^
   ( -size 0x0 -background "#68EBFE" -fill black -pointsize 24 label:"This is a label which I want centered" -bordercolor "#68EBFE" -border 10x10 -bordercolor black -border 1x1 ) ^
   -background White ^
   -gravity North -extent 1000x1000+0-50 ^
   output.jpeg
EDIT: My commands centre horizontally, but are offset from the top by 50 pixels. If you want it centred vertically at y=50, use "-gravity center" with a Y-offset of -450.