[IM7] Easy way to align & append images with overlap?

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?".
sas
Posts: 44
Joined: 2009-11-28T16:35:46-07:00
Authentication code: 8675309

[IM7] Easy way to align & append images with overlap?

Post by sas »

-append makes it easy to vertically append two images such that they're horizontally aligned to the center:

Code: Select all

magick granite: rose: -background transparent -gravity South -append result.png
Image

But now I want to have some overlap between the two images, say 10 pixels.
After some fiddIing, I found a method that works:

Code: Select all

magick \( rose: -set 'option:h1' '%[fx:H-10]' -alpha Set \) \( granite: -gravity south -background transparent -splice '0x%[h1]' \) +swap -gravity South -compose Over -composite result.png
Image

But that's really cumbersome. Is there no built-in way to facilitate this?
If all calculated offsets were known beforehand, "-page" and "-layers merge" could be used, but I want it to work as a single command without having to know image dimensions beforehand.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [IM7] Easy way to align & append images with overlap?

Post by fmw42 »

Use -mosaic with appropriate -page offsets

This seems to work:

Code: Select all

im7 magick granite: rose: \
-set option:offsets '+%[fx:(u.w-v.w)/2]+%[fx:u.w-10]' \
\( -page '%[offsets]' -clone 1 \) \
-delete 1 -mosaic result.png

sas
Posts: 44
Joined: 2009-11-28T16:35:46-07:00
Authentication code: 8675309

Re: [IM7] Easy way to align & append images with overlap?

Post by sas »

That works, thanks.
It still requires messing around with "fx" and variables, though...

I think I once saw a post by an IM developer during IM6 times, saying that a simple mechanism form aligning layers relative to each other (with gravity and offsets and everything) might be implemented in IM7. Has this never come to fruition, then?
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: [IM7] Easy way to align & append images with overlap?

Post by GeeMack »

sas wrote:If all calculated offsets were known beforehand, "-page" and "-layers merge" could be used, but I want it to work as a single command without having to know image dimensions beforehand.
A command like this should provide the result you're looking for...

Code: Select all

magick granite: rose: -alpha on -background none -gravity south \
   \( -clone 0,1 -append \) \( -clone 2,1 -geometry +0+10 -composite -chop 0x10 \) -delete 0--2 result.png
That doesn't require knowing any of the image sizes in advance, and it doesn't depend on any FX expressions or variables. It should work with versions of IM at least as far back as 6.7.7 (... using "convert" for IM6 and "magick" for IM7 of course). It works on Windows cmd or *nix shell (... by tweaking the escapes and continued line syntax of course). And it doesn't matter which image is larger than the other.

It won't work if there are transparent areas in the overlay image because the destination image will show through after the "-composite", but it could be modified a bit to make that work, too.

It may not be as slick or tidy as what you'd like, but I don't think there's a way to do what you want without using "-composite" in some way or another.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: [IM7] Easy way to align & append images with overlap?

Post by snibgo »

A slightly simpler variation on GeeMack's solution:

Code: Select all

convert granite: rose: -background None ( -clone 0 -gravity south -chop 0x10 -clone 1 -append ) -delete 1 -layers merge out.png
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: [IM7] Easy way to align & append images with overlap?

Post by GeeMack »

snibgo wrote:

Code: Select all

convert granite: rose: -background None ( -clone 0 -gravity south -chop 0x10 -clone 1 -append ) -delete 1 -layers merge out.png
That's even simpler, yep. The catch would be, if the second image is wider than the first, the layers won't line up properly because "-layers merge" doesn't obey "-gravity". Working from what you did there (in concept), but creating the output image with "-composite" instead, keeps those parts lined up with "-gravity".

Code: Select all

convert granite: rose: -gravity south -background none ^
   ( -clone 1 -chop 0x10 ) ( -clone 0,2 -append ) -delete 0,2 +swap -composite output.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: [IM7] Easy way to align & append images with overlap?

Post by snibgo »

GeeMack wrote:The catch would be ...
Ah, yes, thanks.
snibgo's IM pages: im.snibgo.com
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: [IM7] Easy way to align & append images with overlap?

Post by anthony »

Update IM Examples Layering, Append with Overlap
http://www.imagemagick.org/Usage/layers/#append_overlap
Give it a couple of hours to update.

However I would point out that a calculated FX solution while 'not as simple' will also let you append with overlap a whole sequence of images. All the offsets are relative to the previous image (using its calculated position and size). This works for any images, or any (and variable) size, and in any direction! This is wny it is a more 'general' solution.

In this case next image is overlaps by 8 pixels then moved down 4 pixels.

Code: Select all

convert font_[0-9].gif \
           -set page '+%[fx:u[t-1]page.x+u[t-1].w-8]+%[fx:u[t-1]page.y+4]' \
           -background none -layers merge +repage show:
Image

See results and more detailed description in (when it updates)...
http://www.imagemagick.org/Usage/layers/#layer_calc_inc
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: [IM7] Easy way to align & append images with overlap?

Post by GeeMack »

anthony wrote:Update IM Examples Layering, Append with Overlap
It looks like everyone overlooked one of the simpler approaches to the OP...

Append with Space or Overlap Using "Smush"

A more flexible counterpart to the "-append" operation is "-smush". It takes one argument and acts on the current stack of two or more images. Instead of just connecting images edge to edge like "-append", "-smush" also lets you connect images separated by some amount of space or even with an overlap. The "-smush" operator obeys the "-gravity" setting, so given an argument of zero, "-smush" should mimic "-append" exactly.

Like "-append", the "-smush" option attaches a sequence of images together vertically, while the plus form "+smush" attaches them horizontally.

To connect two or more images side by side with space between them, use a positive number as an argument to "+smush"...

Code: Select all

convert granite: rose: -background none -gravity east +smush 10 smush_space.gif
Image

Using a negative argument to "-smush" will cause the images to overlap by that much...

Code: Select all

convert granite: rose: -background none -gravity south -smush -10 smush_overlap.gif
Image

The "-smush" operation overlaps the images from left to right or top to bottom. It's simple to alter that behavior with combinations of "-flip" or "-flop", and "-reverse"...

Code: Select all

convert granite: rose: gradient:[128x] -background none \
   -gravity center -reverse -flip -smush -10 -flip smush_backward.gif
Image

Any empty space surrounding or between images will be filled with the "-background" color.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [IM7] Easy way to align & append images with overlap?

Post by fmw42 »

I never saw that feature. Good catch!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: [IM7] Easy way to align & append images with overlap?

Post by snibgo »

Thanks, GeeMack. I always wondered what "-smush" did.

The documentation http://www.imagemagick.org/script/comma ... .php#smush says simply...
smush an image sequence together.
... which isn't massively helpful. I suggest the documentation is expanded, along these lines:
This is a more flexible version of "-append", joining the images in the sequence top-to-bottom (-smush) or left-to-right (+smush), with a gap between images of the specified offset.

If the offset is negative, images will overlap by that amount.

"-smush" respects "-gravity". Any empty space will be filled with the "-background" color.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [IM7] Easy way to align & append images with overlap?

Post by fmw42 »

I will try to fix the docs tomorrow.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: [IM7] Easy way to align & append images with overlap?

Post by anthony »

That is a good find.. I Remember when the operator was added.

Though it does more than just append with a overlap. It appends images with shape, and allowing one shape to fit into another shape.
I believe it will also 'slide' images around a little to try and make them fit together better too. Perhaps a bit more is needed in the docs

But this also means the operator does a LOT more work, that the previous append techniques, and could be with less overall control.

Before I could set a good example. I would need some sort of example images to use.

Hmmm... the letters A and V should work well for this...

Append A and V

Code: Select all

convert -pointsize 72 -background none \
      -fill red label:A -fill blue label:V \
      +append av_append.png
Image

Smush A and V

Code: Select all

convert -pointsize 72 -background none \
      -fill red label:A -fill blue label:V \
      +smush 0 av_smush.png
Image

Overlap using smush

Code: Select all

convert -pointsize 72 -background none \
      -fill red label:A -fill blue label:V \
      +smush -20 av_smush_2.png
Image

Now for the issue... images can slide around a bit to try to get them 'closer' when using smush...

Code: Select all

convert -background none \
       -pointsize 72 -fill red label:A \
       -pointsize 36 -fill blue label:V \
       +smush 0 av_smush_slide.png
Image

I have added examples such as shown above to the "Layering Images" Examples Pages, just under append. When it uploads...
http://www.imagemagick.org/Usage/layers/#smush
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: [IM7] Easy way to align & append images with overlap?

Post by fmw42 »

I have modified the docs regarding smush.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: [IM7] Easy way to align & append images with overlap?

Post by anthony »

Hmm... I was wrong, smush does not slide the images up and down!
Swapping the A and V in the previous example (second image remaining smaller) and the 'A' does not slide to the bottom, but remains at the top according to gravity.

Code: Select all

convert -background none \
        -pointsize 72 -fill red label:V \
        -pointsize 36 -fill blue label:A \
        +smush 0 av_smush_no_slide.png
Image

So smush is an append that places later images as close as possible to the previous images, while ignoring transparency, with a argument specifying a offset (gap or overlap amount).

NOTE: however it is not simple append.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply