Page 1 of 1

Transparent colour goes black when png/gif resized and conve

Posted: 2007-08-08T09:01:48-07:00
by c0ntax
I wonder if anyone out there knows if the following is a bug or 'feature'. I've found that when loading in a gif with a transparent background (Such as this http://www.phrenetic.org/images/rabbit_tattoo.gif), if I resize the image and then change it's format to a jpeg, the outputted image's transparent background is converted to black. If I don't resize, it stays white. Very odd. Is there a way to stop this from happening?

Here's an example of what I'm doing

Code: Select all

$mwh = NewMagickWand();
MagickReadImage($mwh, 'rabbit_tattoo.gif');

MagickResizeImage($mwh, 369, 501, MW_LanczosFilter, 0.5);

MagickSetFormat($mwh, 'jpeg');

header('Content-type: ' . MagickGetMimeType($mwh));
MagickEchoImageBlob($mwh);
If I comment out the resize line then the image (as a jpeg) will be displayed correctly with the white background. If the resize is left in, then it comes out as black.

I've also tried setting the background colour to red in the hopes that even when outputting the image as a GIF, the transparent colour will fall through to the red, but this doesn't seem to have any effect either. E.g.

Code: Select all

$pwh = NewPixelWand('#ff0000');
MagickSetImageBackgroundColor($mwh, $pwh);
Any ideas? Or does this sound like a bug that I should maybe submit to the developers?

Re: Transparent colour goes black when png/gif resized and conve

Posted: 2007-09-19T01:25:03-07:00
by Crewone
I have the same problem. Did you find a solution?

Re: Transparent colour goes black when png/gif resized and conve

Posted: 2007-09-19T01:34:51-07:00
by c0ntax
Sadly not. I'm still hoping that someone comments on this thread with a solution. It looks like this is an actual bug with ImageMagick though so I might see what I can do about getting this officially submitted as a bug.

Re: Transparent colour goes black when png/gif resized and conve

Posted: 2007-09-27T00:20:37-07:00
by Crewone
Is there nobody who can look at this? I can provide code and example images...

Re: Transparent colour goes black when png/gif resized and conve

Posted: 2007-12-13T18:05:55-07:00
by anthony
JPEG does not save transparency, so it goes 'black' the undefined color hidden by the transparency!!!

Flatten your image before saving to JPEG to replace transparency with an appropriate background color, or overlay it on another non-transparent image. Just setting a background color does nothing but change the background setting (it is not an operator, only a setting).

PNG can save semi-transparency, GIF only boolean or on/off transparency (an limited color table).

Re: Transparent colour goes black when png/gif resized and conve

Posted: 2007-12-14T02:41:01-07:00
by c0ntax
I finally found out how to do it. It's quite simple although a little annoying to have to do as I feel that ImageMagick should use the images background colour and not black when converting to an image format that does not support transparencies...

Code: Select all

$width = 369;
$height = 501;
$mwh = NewMagickWand();
MagickReadImage($mwh, 'rabbit_tattoo.gif');

$background_pwh = MagickGetImageBackgroundColor($mwh);
$colour_string = PixelGetColorAsString($background_pwh);

$mwh2 = NewMagickWand();
MagickNewImage($mwh2, $width, $height, $colour_string);

MagickResizeImage($mwh, $width, $height, MW_LanczosFilter, 0.5);

MagickCompositeImage($mwh2, $mwh, MW_OverCompositeOp, 0, 0);

MagickSetFormat($mwh2, 'jpeg');

header('Content-type: ' . MagickGetMimeType($mwh2));
MagickEchoImageBlob($mwh2);
Now, if only there was a command that could tell me if an image contained any semi/full transparent pixels so that I only performed this hack when required... Iterating through all the pixels in an image searching for transparencies seems a trifle expensive.

Re: Transparent colour goes black when png/gif resized and conve

Posted: 2007-12-14T14:00:36-07:00
by el_supremo
I tried this with MagickWand (6.3.5 Q8) and C. The sequence:

Code: Select all

	MagickReadImage(m_wand,"rabbit_tattoo.gif");
	MagickFlattenImages(m_wand);
	MagickWriteImage(m_wand,"rabbit_tattoo.jpg");
generates an image with white background but:

Code: Select all

	MagickReadImage(m_wand,"rabbit_tattoo.gif");
	MagickResizeImage(m_wand,369,501,LanczosFilter,0.5);
	MagickFlattenImages(m_wand);
	MagickWriteImage(m_wand,"rabbit_tattoo.jpg");
generates an image with black background and doing flatten before resize also produces a black background.
However, if I force the GIF to be opaque first, then this:

Code: Select all

	MagickReadImage(m_wand,"rabbit_tattoo.gif");
	MagickSetImageOpacity(m_wand,1.0);
	MagickResizeImage(m_wand,369,501,LanczosFilter,0.5);
	MagickWriteImage(m_wand,"rabbit_tattoo.jpg");
produces a white background. If you are going to write the image as a JPG then presumably you can always force the opacity to be on.

Pete

Re: Transparent colour goes black when png/gif resized and conve

Posted: 2007-12-17T02:41:53-07:00
by c0ntax
I can't seem to find an mention of the MagickSetImageOpacity function in the documentation (http://www.magickwand.org/).

Re: Transparent colour goes black when png/gif resized and conve

Posted: 2007-12-17T09:30:27-07:00
by magick
We added MagickSetImageOpacity() to MagickWand for PHP 1.0.6 beta available sometime tomorrow. Thanks.

Re: Transparent colour goes black when png/gif resized and conve

Posted: 2007-12-18T06:04:59-07:00
by c0ntax
Excellent, thanks! :D

Re: Transparent colour goes black when png/gif resized and conve

Posted: 2017-11-23T05:26:13-07:00
by av01d
I know this is an ancient thread, but I came here via Google, so others might as well.
Here's how to do this in recent versions of IM:

Code: Select all

convert image.png -background white -alpha remove -resize 50% image.jpg

Re: Transparent colour goes black when png/gif resized and conve

Posted: 2017-11-23T16:34:06-07:00
by anthony
Better still look at which has a number of different examples of the problem and how to fix it/

IM Usage Examples, Removing Transparency from Images
http://www.imagemagick.org/Usage/masking/#remove
The section of example immediately above that introduces the -alpha image processing operators.

Also the section on JPEG file format
http://www.imagemagick.org/Usage/formats/#jpg