Linear alpha mask for merging overlapping pictures

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
Goose997
Posts: 3
Joined: 2018-02-20T11:25:44-07:00
Authentication code: 1152

Linear alpha mask for merging overlapping pictures

Post by Goose997 »

Good evening!

I am using the following ImageMagick version on Windows:

Code: Select all

Version: ImageMagick 7.0.7-22 Q16 x64 2018-01-22 http://www.imagemagick.org
I am trying to merge overlapping pictures. The pictures are 800x800, with a 200 px overlap on the height. I want to overlay the images and merge them via a linear alpha mask. I am creating a mask with a linear alpha on the bottom of the first image (p1.png) and top of the second image (p2.png) as follows:

Code: Select all

magick -size 800x600 xc:none none.png
magick -size 800x200  gradient:none-black  gradient.png
convert -page +0+0 none.png -page +0+600 gradient.png  -layers merge +repage temp.png
convert temp.png -alpha copy mask.png
convert p1.png mask.png -alpha Off -compose CopyOpacity -composite masked.png
magick -size 800x200  gradient:black-none  gradient-top.png
convert -page +0+0 gradient-top.png -page +0+200 none.png  -layers merge +repage temp.png
convert temp.png -alpha copy top-mask.png
convert p2.png top-mask.png -alpha Off -compose CopyOpacity -composite masked-1.png
convert -page +0+0 masked.png -page +0+600 masked-1.png  -layers merge +repage merged.png
The top image p1.png:
https://imgur.com/8vYyuIj
The bottom image p2.png:
https://imgur.com/pu7hPWA
And the resultant merged image merged.png:
https://imgur.com/3ySjaRE

How can I fix middle part (overlap) of the final image? It seems to be "undersaturated"? My theory is that the two linear gradients going in opposite directions should add up leaving the original colours after overlapping the pictures?

Help is appreciated, I am quite new to ImageMagick.

Thanks
Goose.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Linear alpha mask for merging overlapping pictures

Post by GeeMack »

Goose997 wrote: 2018-02-20T11:50:18-07:00How can I fix middle part (overlap) of the final image? It seems to be "undersaturated"? My theory is that the two linear gradients going in opposite directions should add up leaving the original colours after overlapping the pictures?
Edited to add: The images "p1" and "p2" you linked were JPGs. If you meant to upload PNG files, the hosting service may have converted them. The rest of my comment below would apply if the input images have a 200 pixel high fade-to-white on the bottom or top edge. If the input images have transparency already, the command I suggested would probably have to be modified.

At a command prompt with Windows 10 and IM 7.0.7-23, I get a result like you describe by using just your input images "p1.jpg" and "p2.jpg", and this single command...

Code: Select all

magick p1.jpg p2.jpg -set page 800x1400+0+%[fx:t*600] -compose add -layers merge output.png
 
In a BAT script you'd need to make that single percent sign "%" into a double "%%".

It may be even simpler if you are able to start with images before that fade-to-white is applied to the edges.
Goose997
Posts: 3
Joined: 2018-02-20T11:25:44-07:00
Authentication code: 1152

Re: Linear alpha mask for merging overlapping pictures

Post by Goose997 »

GeeMack wrote: 2018-02-20T13:44:00-07:00 Edited to add: The images "p1" and "p2" you linked were JPGs. If you meant to upload PNG files, the hosting service may have converted them. The rest of my comment below would apply if the input images have a 200 pixel high fade-to-white on the bottom or top edge. If the input images have transparency already, the command I suggested would probably have to be modified.
It seems the hosting service converted PNG to JPG. The fade-to-white on the picture is the alpha mask from the PNG.

Thanks for the help. It does the alignment and some form of merge? However, it creates a pink/purple smudge on some parts of the image. I will experiment further with the command line you gave me and see where it gets me to.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Linear alpha mask for merging overlapping pictures

Post by GeeMack »

Goose997 wrote: 2018-02-20T20:23:03-07:00It seems the hosting service converted PNG to JPG. The fade-to-white on the picture is the alpha mask from the PNG.
If the input images fade to transparent at the edges, you might try adding "-background white -alpha off" right after reading in the images.

Code: Select all

magick p1.png p2.png -background white -alpha off ^
   -set page 800x1400+0+%[fx:t*600] -compose add -layers merge output.png
That should convert the transparent fades to white fades, then continues with the rest of my example command.
Goose997
Posts: 3
Joined: 2018-02-20T11:25:44-07:00
Authentication code: 1152

Re: Linear alpha mask for merging overlapping pictures

Post by Goose997 »

Thanks for help: I found the secret, I should add "-compose linearburn". This leaves no transparency.
Post Reply