Allways black transparency in JPEG images

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
MacStrix

Allways black transparency in JPEG images

Post by MacStrix »

Hello,

I have an image with transparency. And I want to save it as JPEG. I am using C. Everything works fine, except that transparent image places are black in output image. And I could not figure out how to change it, lets say to white.

Tried this:

Code: Select all

PixelWand *myPixelWand = NewPixelWand();
		PixelSetColor(myPixelWand,"rgba(255,255,255,255)");
		PixelSetOpacity(myPixelWand, 1.0); // tried 0.0 and 1.0
		MagickSetImageMatteColor(saveWand, myPixelWand);
		DestroyPixelWand(myPixelWand);
Does not work.

Code: Select all

PixelWand *myPixelWand = NewPixelWand();
		PixelSetColor(myPixelWand,"rgba(255,255,255,255)");
		PixelSetOpacity(myPixelWand, 1.0); // tried 0.0 and 1.0
		MagickSetImageBackgroundColor(saveWand, myPixelWand);
		DestroyPixelWand(myPixelWand);
Does not work also.

By the way: MagickGetImageMatte(saveWand) returns MagickTrue;

Any help would be very appreciate.

p.s.using ImageMagick 6.3.4
Last edited by MacStrix on 2007-09-02T11:30:39-07:00, edited 1 time in total.
MacStrix

Re: Allways black transparency

Post by MacStrix »

Here are files I am using:

source: http://img250.imageshack.us/img250/1439 ... e01rj1.png

result: http://img235.imageshack.us/img235/1918 ... e08vv4.jpg

Black area of result image should be white (in my case).
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Allways black transparency in JPEG images

Post by el_supremo »

This will do it:

Code: Select all

convert input.png ( +clone -channel a -separate -negate ) +swap -composite output.jpg
It extracts the matte channel, negates it so that what was transparent is now white and then composites the original png over the top.
If you want a colour other than white, you'd have to replace the white in the negated matte with something else. I'm not sure how to do that though (yet).
Pete
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Allways black transparency in JPEG images

Post by anthony »

Actually the simplest way is to -flatten the image before saving to JPG.

Code: Select all

convert image.png  -background white -flatten image.jpg
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Allways black transparency in JPEG images

Post by el_supremo »

Of course! Thanks Anthony.

Pete
Post Reply