Page 1 of 1

MagickFlattenImages stopped working as expected

Posted: 2012-05-15T03:28:56-07:00

I am using MagickWand in my python application (using pythonmagicjwand).

One thing that my application does is taking an image, resize it and saves the resized image to disk.
Since I also works with PNG files which might have transparency in them, after the resize, I am setting the white background and use MagickFlattenImages so I can get a white background.

This worked perfectly with the following setup:
ii imagemagick 8: image manipulation programs
ii libmagickcore3 8: low-level image manipulation library
ii libmagickcore3-extra 8: low-level image manipulation library - extra codecs
ii libmagickwand3 8: image manipulation library

ii libpng12-0 1.2.44-1 PNG library - runtime

Since I moved to ImageMagick 6.6.8-6 (from source), libpng-1.5.1:
Calling MagickFlattenImages makes the resized image black - the entire image is black rectangle not only the background.

Running convert from the command line yields the required result but it seems that the MagickFlattenImages API was broken.

I also tried using the newer API MergeImageLayers with FlattenLayer method.
This also occurs with the latest version: ImageMagick 6.7.6-9, libpng-1.5.10.

Please assist....

Re: MagickFlattenImages stopped working as expected

Posted: 2012-05-20T20:24:37-07:00
by anthony
Some sample code and sample image?

Re: MagickFlattenImages stopped working as expected

Posted: 2012-05-31T03:08:08-07:00
Here is a Python sample code:

Code: Select all

from pythonmagickwand.image import Image

icon = Image('/tmp/image.jpg')
icon.thumbnail(100, 100)
flattenIcon = icon.flatten()'/tmp/icon_image.jpg')
Sample file can be found here: ... E0yQzNLQWM

save it to /tmp/image.jpg

PythonMagickWand can be found at:

And installed by (make sure svn is installed)

Re: MagickFlattenImages stopped working as expected

Posted: 2012-05-31T17:27:41-07:00
by anthony
JPEG images have no alpha. Nor do they have multiple images.

Flatten will in such a case do its job, but the result will be exactly as the original -- no change.

I assume you have extra processing steps between the thumbnail and the flatten.
Or perhaps may read PNG or GIF images and save as JPG?

If you want to get rid of transparency the new way to do this is to use the Alpha Operator method "remove".
I do not know the function name in Magick API.

This also does a background color underlay, but does it without extra image creation or full composition, but just direct changes to the given image. - A lot faster and memory efficient.

See Command line examples on -alpha remove

Flatten does the same thing for single images (with transparency). But that its not its primary job, just a secondary side effect.

Re: MagickFlattenImages stopped working as expected

Posted: 2012-06-18T04:49:15-07:00
Thanks Anthony. You were most helpful.