Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep transparency with "-compose" and "-flatten" on GraphicsMagick

I have a series of PNG images with transparent backgrounds that I want to overlay onto a destination PNG image that also has a transparent background. For argument sake let's say that the series of images are :

  • Source images : img1.png, img2.png, img3.png
  • Destination image : dest.png.

Since I want to overlay all the images at once, I am going to use the convert command, with the compose switch as follows :

gm convert -compose Atop dest.png img1.png img2.png img3.png -flatten output.png

Seems simple enough, but the problem is that output.png looses it's transparency and I do not know how to keep it enabled. If I use the -background switch I can set what used to be the transparency to any color I want, but I cannot get it to go back to transparency.

Yes, I can subsequently call:

gm convert -transparency black ouput.png output2.png

but then any black on the actual image becomes transparent as well.

Any help here?

like image 536
Hoho-0714 Avatar asked Oct 03 '22 07:10

Hoho-0714


1 Answers

I faced the same issue, and I did a couple things to get it to work.

You should get what you want if you change your command to:

gm convert xc:transparent -compose Over img1.png img2.png img3.png -mosaic dest.png

You may want to use Atop of Over depending on the functionality you want out of the compose method.

Using mosaic instead of flatten combined with the xc:transparent argument caused it to keep their transparencies and produces an image with transparencies.

like image 149
Pineechio Avatar answered Oct 13 '22 11:10

Pineechio