Search code examples
imagemagickimagemagick-convert

ImageMagick: Distort and Overlay


I have several pictures of a landscape. Using the ImageMagick CLI on OSX, I would like to distort and overlay them properly. I have looked for distortion coordinates between several of the pictures and a reference picture. I fail to understand the diference between -distort and +distort and how it plays with +repage. When I use -distort, the output has the desired offset but it's incomplete (it needs to be bigger). When I use +distort, I get the full image but it's missing the offset. Reading the documentation I understand that I could do without the offset if I did the overlay composition in the same command before the offset information is lost but what's happening is that the distort is being applied to both the reference and the distorted images.

This is the result of using -distort: enter image description here

This is the result of using +distort: enter image description here

The offset of the -distort result would work once I apply it as an overlay (here using the composite in a separate command, but it's missing a big chunk of the picture. enter image description here

When I tried to consolidate it in a single command this is the result I get: enter image description here

This is the command I'm currently using:

convert base.jpg overlay.jpg
 -matte -virtual-pixel transparent -distort Perspective '961,1695 1856,2461   2279,1520 3185,2303   3564,2173 4441,2970   1547,2817 2441,3594'
 -compose blend -define compose:args=50,100 -composite result.jpg

I understand I could use parenthesis there but I fail to see where should I put them.

Thanks!

Update: this is the result of the overlay when using +distort either in two steps or in a single step as recommended by Mark. enter image description here


Solution

  • The solution was to use -flatten instead of -composite.

    convert base.jpg \( b.jpg -matte -virtual-pixel transparent +distort Perspective '961,1695 1856,2461   2279,1520 3185,2303   3564,2173 4441,2970   1547,2817 2441,3594' \) -compose blend -define compose:args=100,50 -flatten result.jpg
    

    Turns out that -composite ignores the image offsets whereas -flatten works with layers and uses the offset information.

    The suggestion came from this thread: http://www.imagemagick.org/discourse-server/viewtopic.php?t=20157

    This is the documentation to flatten (link broken in the discussion above): http://www.imagemagick.org/Usage/layers/#flatten

    enter image description here