Transparent PNG overlay onto colored background.

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
losttime

Transparent PNG overlay onto colored background.

Post by losttime »

I have a PNG32 file: http://www.outsiderin.com/images/extern ... a2001a.png

It is an outline of a t-shirt. The area that makes up the t-shirt is transparent while the area outside the shirt is white. What I want to do is create a colored background for this PNG and turn my transparent shirt into any color shirt I want.

Sounds easy enough.

I'll try this command . . .

Code: Select all

convert aa2001a.png  -background Red -flatten  test.jpg
This is my result: http://www.outsiderin.com/images/external/im/test.jpg.

I can't seem to get anywhere with this. Any pointers? I'm not sure I have a complete grasp of how IM interprets PNGs and their alpha channels, so maybe something is wrong with my image. I'm up for anything.
losttime

Re: Transparent PNG overlay onto colored background.

Post by losttime »

I recreated my PNG file to confirm that it really was a 32bit file. Remarkably, the file size was 4 times larger, so I'm thinking there may have been a problem with the other file.

The new file can be found here: http://www.outsiderin.com/images/extern ... a2001b.png

I'm using the same command (with the png file name modified to use the new file), and the result is better, but still not working as I'd expect.

Here is the result: http://www.outsiderin.com/images/external/im/testb.jpg

It seems to be just a JPG version of the PNG file. I can't seem to get the colored background. Any help?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Transparent PNG overlay onto colored background.

Post by Bonzo »

This will work the only thing is you need to ensure the -size is ok everytime:

Code: Select all

convert -size 450x400 xc:red aa2001a.png -composite tshirt.jpg
losttime

Re: Transparent PNG overlay onto colored background.

Post by losttime »

Thanks for the suggestion. Sadly, it did not work. The result looks exactly like testb.jpg that I posted above.

This may be a good time to mention that using the convert command with -composite has never seemed to work properly for me.

I am using IM version 6.0.7
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Transparent PNG overlay onto colored background.

Post by el_supremo »

IIRC composite didn't work well in V6.0.x. I'm using 6.3.2 and your command produces a red shirt on a white background.
Upgrade your IM and then try the command again.

Pete
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Transparent PNG overlay onto colored background.

Post by Bonzo »

You could try this:

Code: Select all

composite -gravity center aa2001a.png  -size 450x400 xc:red tshirt1.jpg
I always tend to use convert as I can do everything I want to with it.

Thats interesting Pete; I have the latest version on my PC where I am trying this.
losttime

Re: Transparent PNG overlay onto colored background.

Post by losttime »

Bonzo: The composite command works great. Thanks! I've verified that it works with hex color values too. Now I'm just going to have to figure out the best way to work with blank shirt files of different sizes.

Pete: I'd love to upgrade to the current IM, but I'm on a shared host and they probably won't do it for me (though I'll ask).
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Transparent PNG overlay onto colored background.

Post by Bonzo »

If you were on a later version you could use an fx comand to set the -size to match the original image although I have never been able to get this to work for me using php.

If you are using this with php you can use getimagesize and put the width and height into variables then use these variables in the -size command.

Untested:

Code: Select all

$size = getimagesize("aa2001a.png ");
exec("composite -gravity center aa2001a.png  -size {$size[0]}x{$size[1]} xc:red tshirt1.jpg")
You can also get the image size with IM but you need to be able to get that into a variable. I did it once and will have to look back over my posts and see how I did it.
Post Reply