Strange behaviour at flatten. Possible a bug?

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
User avatar
svewap
Posts: 2
Joined: 2011-07-08T19:19:18-07:00
Authentication code: 8675308

Strange behaviour at flatten. Possible a bug?

Post by svewap »

I want to add a background color to a transparent image. But the content is moved after processing.

Code: Select all

convert -flatten -background white out2.png out2_w.jpg
For example:
out2.png
Image
After the command:
out2_w.jpg
Image
convert --version
Version: ImageMagick 6.6.5-8 2011-02-14 Q16 http://www.imagemagick.org
Thanks, Sven
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Strange behaviour at flatten. Possible a bug?

Post by fmw42 »

try

convert out2.png +repage -background white -flatten out2_w.jpg

see http://www.imagemagick.org/Usage/basics/#why

looks like your png has a virtual canvas offset that needs to be removed by +repage

identify -verbose out2.png
...
Page geometry: 156x281+145-1
Origin geometry: +145-1
...

the above command works fine for me on IM 6.7.1.0 Q16 Mac OSX Tiger.

if that does not work for you upgrade your IM as PNG has undergone quite some changes.
User avatar
svewap
Posts: 2
Joined: 2011-07-08T19:19:18-07:00
Authentication code: 8675308

Re: Strange behaviour at flatten. Possible a bug?

Post by svewap »

Thank you very much for your quick solution! The +repage argument solves the problem!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Strange behaviour at flatten. Possible a bug?

Post by anthony »

The problem is that -flatten is a 'image layering' operation. It obeys the virtual canvas and offset of the image. In this case the virual canvas is the same size as the original image, but the image also has an offset pushing the image off to one side.

In other words the image is not contained by the virtual canvas!

The +repage removes (resets) the virtual canvas size and offset, so that the virtual canvas matches the image, without any offset.

An alternative that will do the same thing is -layers TrimBounds though like other layer image operators it trims the bounds to match multiple images. But for one image, it is the same result. WARNING: it is not the same as -trim, as it does not look at the actual image contents.

Using -mosaic layering command would expand the virtual canvas so as to ensure the image is contained within the canvas but without changing its offset (in positive directions only as a virtual canvas is purely a size starting at +0+0 by definition).

And finally the -layers merge would fill in the background, but keep the images virtual offset position, It will also fix the canvas size appropriately. With one image it would just 'fill out the background' only.

One final possibility is -border 0 which will use border color to fill in the background, and not change anything else at all! It will not even 'flatten' multiple images into a single image, which all the other layering operators will do.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply