Page 1 of 1

Transparent PNG overlay onto colored background.

Posted: 2007-06-15T16:52:05-07:00
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.

Re: Transparent PNG overlay onto colored background.

Posted: 2007-06-15T19:10:10-07:00
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?

Re: Transparent PNG overlay onto colored background.

Posted: 2007-06-16T00:11:55-07:00
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

Re: Transparent PNG overlay onto colored background.

Posted: 2007-06-18T10:15:00-07:00
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

Re: Transparent PNG overlay onto colored background.

Posted: 2007-06-18T11:02:33-07:00
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

Re: Transparent PNG overlay onto colored background.

Posted: 2007-06-18T11:16:17-07:00
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.

Re: Transparent PNG overlay onto colored background.

Posted: 2007-06-18T12:34:52-07:00
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).

Re: Transparent PNG overlay onto colored background.

Posted: 2007-06-18T12:37:03-07:00
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.