Transparent colour goes black when png/gif resized and conve

MagickWand for PHP is an object-oriented PHP interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning MagickWand for PHP.
Post Reply
c0ntax

Transparent colour goes black when png/gif resized and conve

Post 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?
Crewone

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

Post by Crewone »

I have the same problem. Did you find a solution?
c0ntax

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

Post 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.
Crewone

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

Post by Crewone »

Is there nobody who can look at this? I can provide code and example images...
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

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

Post 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).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
c0ntax

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

Post 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.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

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

Post 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
c0ntax

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

Post by c0ntax »

I can't seem to find an mention of the MagickSetImageOpacity function in the documentation (http://www.magickwand.org/).
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

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

Post by magick »

We added MagickSetImageOpacity() to MagickWand for PHP 1.0.6 beta available sometime tomorrow. Thanks.
c0ntax

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

Post by c0ntax »

Excellent, thanks! :D
av01d
Posts: 6
Joined: 2013-10-02T03:24:45-07:00
Authentication code: 6789

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

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

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

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply