-gravity center: am I thinking about it wrong?

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
omgoleus
Posts: 7
Joined: 2011-08-25T07:50:19-07:00
Authentication code: 8675308

-gravity center: am I thinking about it wrong?

Post by omgoleus »

I am trying to write a script that will take a fixed template image, and an arbitrary user-supplied overlay image, and place the overlay image centered in a frame space on the template image. This seems like exactly the kind of thing that should be straightforward with ImageMagick, and indeed convert -composite seems like it's supposed to simply and effectively do exactly what I want. However, something strange is going on which is making it impossible for me to get the overlay image to stay centered.

You can see example files here:
http://brainimaging.waisman.wisc.edu/~p ... kQuestion/

As a demonstration, I ran the following series of commands:
convert Ticket.png -gravity center dPerlman.jpg -geometry 30x30+520+118 -composite test1.png
convert Ticket.png -gravity center dPerlman.jpg -geometry 60x60+520+118 -composite test2.png
convert Ticket.png -gravity center dPerlman.jpg -geometry 120x120+520+118 -composite test3.png
convert Ticket.png -gravity center dPerlman.jpg -geometry 240x240+520+118 -composite test4.png

So I'm taking the same template image, and the same overlay image, and the same coordinates, and the same -gravity center option. The only thing that is changing is the size of the overlay. I would think that the output of these four commands should have the overlay image different sizes, but always centered in the same spot. However, the image moves around with the scaling:

http://brainimaging.waisman.wisc.edu/~p ... /test1.png
http://brainimaging.waisman.wisc.edu/~p ... /test2.png
http://brainimaging.waisman.wisc.edu/~p ... /test3.png
http://brainimaging.waisman.wisc.edu/~p ... /test4.png

Note that the center of the overlay image is in a different place in each of those. It's not a huge movement, but it's enough that the output is definitely not going to be centered for input overlay images of arbitrary dimensions.

Here's my version info, I tried it with both of these and got the same results:
convert -version
Version: ImageMagick 6.5.4-7 2012-05-07 Q16 OpenMP http://www.imagemagick.org
Copyright: Copyright (C) 1999-2009 ImageMagick Studio LLC

convert -version
Version: ImageMagick 6.7.1-1 2011-07-21 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2011 ImageMagick Studio LLC
Features: OpenMP



So, first of all, is this problem occurring simply because I'm not correctly understanding something about how gravity center is supposed to work? Or is this actually a bug in this version of imagemagick? And, most importantly, how can I get this to keep the centers centered?

Thanks in advance for any and all help!
Last edited by omgoleus on 2012-07-15T11:20:31-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -gravity center: am I thinking about it wrong?

Post by fmw42 »

-gravity center should go after the second image .e.g. before -geometry. the offsets in -geometry will be relative to the center of the first image -- that is the second image center will be placed at the center of the first image and then translated by the offsets relative to the center of the first image. Also you will get image resizing by specifying WxH in -geometry.

If that does not help, let me know and I will test your images.
omgoleus
Posts: 7
Joined: 2011-08-25T07:50:19-07:00
Authentication code: 8675308

Re: -gravity center: am I thinking about it wrong?

Post by omgoleus »

Hmm, that seems logical, but it made no difference:


Trinley:StudyEssentials omgoleus$ convert Ticket.png dPerlman.jpg -gravity center -geometry 30x30+520+118 -composite test1.png
Trinley:StudyEssentials omgoleus$ convert Ticket.png dPerlman.jpg -gravity center -geometry 60x60+520+118 -composite test2.png
Trinley:StudyEssentials omgoleus$ convert Ticket.png dPerlman.jpg -gravity center -geometry 120x120+520+118 -composite test3.png
Trinley:StudyEssentials omgoleus$ convert Ticket.png dPerlman.jpg -gravity center -geometry 240x240+520+118 -composite test4.png


produces identical images.

Thanks for your help!
-dave
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -gravity center: am I thinking about it wrong?

Post by fmw42 »

convert Ticket.png dPerlman.jpg -gravity center -geometry 30x30+520+118 -composite test1.png

Works fine for me on IM 6.7.8.2 Q16 Mac OSX Snow Leopard. You have created a 30x30 version of dPerlman.jpg. Then its center is placed at the center of Ticket.png, then it is shifted 520 in X and 118 in Y from the center of Ticket.png.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: -gravity center: am I thinking about it wrong?

Post by anthony »

You have it almost right.

Gravity does not center the image, it centers the point the image is off set from!

the reason your images are not centered is you also have a -geometry offset!!!


NOTE: gravity cenetr currently has too effect. set the 'zero' offset point on the destination, but also set the 'handle' of overlay to its center. That second part can be thought of as a 'centered' justification, and really should be a separate setting (with a fall back default to gravity center, if undefined). At the moment it isn't. In IMv7 it will be (not yet) a separate setting.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
omgoleus
Posts: 7
Joined: 2011-08-25T07:50:19-07:00
Authentication code: 8675308

Re: -gravity center: am I thinking about it wrong?

Post by omgoleus »

I don't want the images centered; I want the handle of overlay to be the center so that the -geometry offset is measured from the center of the underlay image to the center of the overlay image. According to what you're saying, that is what I should be getting with this, but if you look at the images I linked, that is not what is happening. Even under the interpretation that you're describing, the fact that the four versions of the overlay don't have their centers lined up doesn't make any sense to me. Can you explain to me why it is working like that?

Thanks!

-dave
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: -gravity center: am I thinking about it wrong?

Post by anthony »

OKAY.

Then that is will need to wait for IMv7 and the separation of Justification (overlay handle) from Gravity (placement handle and offset)

OR

DIY the calculation yourself.

A nice way to do that is to use -set page with FX expressions with -flatten -mosaic, or -layers merge
No 'simple' example available, only complex looped laying of multiple images (using -repage)
http://www.imagemagick.org/Usage/layers/#layer_examples

OR use distort to do translations, which can even do sub-pixel (fractional pixel offset) positioning.
For example see...
http://www.imagemagick.org/Usage/annotating/#distort
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -gravity center: am I thinking about it wrong?

Post by fmw42 »

I really am not following why you even put in the offsets if you want the two images centered with the resizing. just do


convert Ticket.png dPerlman.jpg -gravity center -geometry 30x30+0+0 -composite test1.png


That works for me. The second (overlay) image is resized to 30x30 and then its center is aligned with the center of the first (background) image.


Your offsets in -geometry are what is shifting the results off of center.

Sorry, if either my post above with the offsets or this post is not what you want, then please clarify exactly what is you want for the result as it is not clear to me.
omgoleus
Posts: 7
Joined: 2011-08-25T07:50:19-07:00
Authentication code: 8675308

Re: -gravity center: am I thinking about it wrong?

Post by omgoleus »

OK, I'm sorry, I thought that my intention was clear from the code I posted.

I want the overlay image to be placed so that its center is located over a fixed point on the underlay image. You'll notice that there's a circular frame on the underlay image of the lottery ticket. I want the overlay image to be:
1. scaled to fit inside that frame, then
2. placed centered on that frame.

The coordinates portion of the geometry string in my command was determined by trial and error to indicate the center of the frame, namely: +520+118 from the center of the lottery ticket image.

According to the documentation, and also according to the statements of people in this thread, the command I wrote should have the following effect:

1. Scale the overlay image to the size specified by the first part of the geometry string.
2. Place the center of the overlay image at the point +520+118 relative to the center of the underlay image.

If the command were actually working that way, then the overlay image should always have its center at the same place. However, if you look at the sample images I provided, it is very clear that the center of the overlay image is in a different point in each one. Also, I did not demonstrate here, but if you were to feed it a different overlay image, you would see that the center ends up in a very different place. I could provide an example image so you could see that, or you could just give it any image yourself.

I appreciate that a few people are taking the time to look into this. I assure you that I have a very good understanding of how -gravity center and geometry strings are supposed to work. So far, it appears to me that there is a bug in the program, but I have not wanted to assume that without asking if there were some other factor I was missing. Perhaps there is something in the headers of the overlay image which is bizarrely causing convert to think the center of the image is somewhere other than the center of the image? I don't know. But if you look carefully at the code I have provided and the output it produces, and think about it a bit, you should be able to see that there is something strange going on. If it does not look strange to you, I would very much appreciate an explanation of why it's actually what is expected!

In summary, the problem is that if you look at the 4 example images, the OVERLAYS are not all co-centered. There is nothing in the commands that should be moving them around relative to each other; the commands should put all the overlays centered on the same point (namely +520+118). But they are not all centered on the same point.

Thanks!
-dave
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -gravity center: am I thinking about it wrong?

Post by fmw42 »

Yes, you are right. I had not looked at the larger resize images. However, your offset is wrong. It should +510 not +520, though my measurement was not perfect, just a rough estimate from some symmetric point in the middle.

Compare

Incorrect:
convert Ticket.png dPerlman.jpg +repage -strip -gravity center -geometry 240x240+510+118 -composite test1.png

Correct:
convert Ticket.png \( dPerlman.jpg -resize 240x240 \) +repage -strip -gravity center -geometry +510+118 -composite test2.png


Note I generally do not use the scaling option in -geometry and use the parenthesis method in preference.

see
http://www.imagemagick.org/Usage/basics/#parenthesis

http://www.imagemagick.org/Usage/layers/#convert

"Now the "-geometry" is is a very special operator that not only sets an overlay position for the next "-composite" operation, it also "-resize" the last image (and only the last image) in the current image sequence."
omgoleus
Posts: 7
Joined: 2011-08-25T07:50:19-07:00
Authentication code: 8675308

Re: -gravity center: am I thinking about it wrong?

Post by omgoleus »

OK, thanks! The parentheses version does, indeed, work correctly. So my problem is solved.

Also, your version of the command still works even if I leave out the +repage -strip part. But I'll leave that in as a precaution.

I'm still mystified about what the sequence of operations is when I provide the rescale and center as part of the same -geometry string. I would be very appreciative if anyone could explain what imagemagick is doing there that causes the scaled images to be lined up on a point that is not the center, nor any other significant point I can think of. It still seems like a bug to me.

In any case, I am able to move forward with my project, so I very much appreciate the help!

-dave
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -gravity center: am I thinking about it wrong?

Post by fmw42 »

omgoleus wrote:OK, thanks! The parentheses version does, indeed, work correctly. So my problem is solved.

Also, your version of the command still works even if I leave out the +repage -strip part. But I'll leave that in as a precaution.

I'm still mystified about what the sequence of operations is when I provide the rescale and center as part of the same -geometry string. I would be very appreciative if anyone could explain what imagemagick is doing there that causes the scaled images to be lined up on a point that is not the center, nor any other significant point I can think of. It still seems like a bug to me.

In any case, I am able to move forward with my project, so I very much appreciate the help!

-dave
I put in the +repage and -strip just to be safe even though it is not needed in your case. They were added in case there was some virtual canvas being set that I could not see.

I reported the initial problem on the bugs forum.
Post Reply