Layering Images with convert -composite with gravity center

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
tomasikp
Posts: 2
Joined: 2012-01-27T04:10:58-07:00
Authentication code: 8675308

Layering Images with convert -composite with gravity center

Post by tomasikp »

Image
background
Image
overlay

Code: Select all

composite -gravity center overlay.png  background.jpg  result1.jpg
Image
result1.jpg

Code: Select all

convert -composite background.jpg overlay.png -gravity center result2.jpg
Image
result2.jpg

Code: Select all

convert -composite background.jpg -gravity center tool_marker.png  result3.jpg
Image
result3.jpg


How can I achieve the results from result1 while using convert as the executable rather than composite?

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

Re: Layering Images with convert -composite with gravity cen

Post by anthony »

You can start by using the operators in the right order. That is set the 'settings' first.
"Composite" command is 'read all settings then apply ONE operation, type of command (traditional UNIX)
"Convert" is a 'do options as you see them', with MULTIPLE operations possible. (script-like command)

Code: Select all

convert  background.jpg  tool_marker.png -geometry +50+50 -composite result4.jpg
Note the +50+50 is the location of the top-left corner of the 'tool_marker.png" image. You will need to subtract the 'pin-point' location in that image to get it to pin point in the right location.

Gravity Center (if given BEFORE the -composite operation that uses it), aligns the center of BOTH images.

In IMv7 I have a 'justification' setting planned. That will position the image using gravity, then the overlay image using justification relative to that position, BUT it will not position the image to a specific point!

Hmmm that may not be a bad idea as a way to implement justification, and now is the time for such ideas. I will need to think about it.

Eg: -geometry and -gravity locates the position on the background image, while -justification locates the point in the overlay image that is to be aligned with that location! (all will need to be integers, as only image distortions can do sub-pixel positioning of images).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
tomasikp
Posts: 2
Joined: 2012-01-27T04:10:58-07:00
Authentication code: 8675308

Re: Layering Images with convert -composite with gravity cen

Post by tomasikp »

thank you for the quick reply Anthony. Sorry about not using the operators in the right order. Justification sounds like a great setting!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Layering Images with convert -composite with gravity cen

Post by anthony »

It will be in IMv7 but that is still in development. Mean time the best way is to forget gravity, and just set geometry equal to location for pin minus location pf pin-point in the overlay image.

For example hmmm... Th pin-point on overlay image is +18+41 from the top left corner.
so to put at pin at.... "Accademia" which is at +160+283
you subtract and get a composition offset of +142+242

Code: Select all

convert venice.jpg pin.png -geometry +142+242 -composite venice_pinned.jpg
Image

If you are putting in lots of pins, a better way is to 'layer' the images. This uses a '-page' or virtual image offsets to place overlay images... See
http://www.imagemagick.org/Usage/layers/#flatten
For example...

Code: Select all

convert venice.jpg \
            \( pin.png -repage +142+242 \) \
            \( +clone -repage +236+125 -modulate 100,100,33.3  \) \
            \( +clone -repage +280+184 -modulate 100,100,133.3  \) \
            -flatten  venice_landmarks.jpg
Image

To avoid reading in multiple colored 'pins' I clone the existing pint, re-position it, then recolor using 'modulate' to rotate the non-gray colors. For details see Color Modifications, Modulate
http://www.imagemagick.org/Usage/color_mods/#modulate

Also see the Basic's section, about command line order, settings, operators, etc...
http://www.imagemagick.org/Usage/basics/
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: Layering Images with convert -composite with gravity cen

Post by fmw42 »

I don't know if this makes the computation easier, but if you fill out your pin image with transparency so that the pin point is at the center, then you can use -gravity center -geometry ... But then you have to measure the offsets for -geometry relative to the center of the background image.

However, if you have lots of pins to place, using -page -flatten is likely going to be easier and perhaps faster in the long run.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Layering Images with convert -composite with gravity cen

Post by anthony »

Added the above as well as an expanded 'data file to pins' example into IM Examples...
http://www.imagemagick.org/Usage/layers/#layer_pins

This would make a great example where the map is 'layed flat' and pins added in a sort of 3-D view.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply