Search code examples
transparencylayerimagemagick-convert

Imagemagick stack images overlapping with transparency


I am fighting this for two days now... without success. Some task I have done with Gimp for some time but I like to make it automated now for standard processing.

So the result should look like this https://i.sstatic.net/wDUc0.jpg Kind of one image in front and the two or three behind it with some transparency to fade the layers out (optional). There are always the same image dimension in one group. But the dimensions can differ to other groups. Example: one group to combine is each image 124x112px, another group is each 188x80px and the next is 164x160px.

I have tried something like

convert image1.png image2.png image3.png -set page '+%[fx:u[t-1]page.x+u[t-1].w-256]+%[fx:u[t-1]page.y+16]' -background none -layers merge +repage output.png

which was working fine (except the optional transparency) for some files. I don't really require a procentual offset, it would be ok if each layer is offset by like 30x5px. But the output image should be relational to the input image.

So, in short.. really puzzled right now how to achieve this. Hope someone can help. I've also studied the PHP Imagick Extension https://www.php.net/manual/de/book.imagick.php There I had the following approach which was even worse to the above

  $imagick->newimage(200, 112, '#ffffffff');
  foreach ($uploadfiles as $file) {    
    $im = new Imagick($file);
    $imagick->compositeimage($im, Imagick::COMPOSITE_DEFAULT, 30, 5);
  }
  $imagick->writeImage($output);

It seems it didn't offset it at all.

Anyone has a helping hand for me?


Solution

  • This works for me in Imagemagick 6.

    Input:

    enter image description here

    convert lena.png \( lena.png -duplicate 2 -alpha set -channel a -evaluate set 75% \) lena.png -set page '+%[fx:u[t-1]page.x+u[t-1].w-128]+%[fx:u[t-1]page.y+16]' -background none -layers merge +repage output.png
    


    enter image description here

    The first and last images are opaque and the middle 3 are 75% opaque (25% transparent)

    ADDITION:

    If you only want a fixed X offset of 30 and no Y offset, then use

    convert lena.png \( lena.png -duplicate 2 -alpha set -channel a -evaluate set 75% \) lena.png \
    -set page '+%[fx:u[t-1]page.x+30]+0' -background none -layers merge +repage output.png
    


    enter image description here