Page 1 of 1
Strange behaviour at flatten. Possible a bug?
Posted: 2011-07-09T15:19:14-07:00
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
After the command:
out2_w.jpg
Thanks, Sven
Re: Strange behaviour at flatten. Possible a bug?
Posted: 2011-07-09T15:34:22-07:00
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.
Re: Strange behaviour at flatten. Possible a bug?
Posted: 2011-07-09T15:43:20-07:00
by svewap
Thank you very much for your quick solution! The +repage argument solves the problem!
Re: Strange behaviour at flatten. Possible a bug?
Posted: 2011-07-09T18:37:13-07:00
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.