Error: Flatten(ref)

PerlMagick is an object-oriented Perl interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning PerlMagick.
Post Reply
trident.cjr
Posts: 7
Joined: 2007-03-06T03:14:51-07:00

Error: Flatten(ref)

Post by trident.cjr »

I have spent some rather frustrating days converting ImageMagick code into PerlMagick and am now successfully able to convert a simple JPEG into the following transparent PNG:

Image

Unfortunately for use on our website I now need to convert it to a GIF on a solid background as per: The ImageMagick command is simplicity itself:

Code: Select all

convert a.png -background LightSteelBlue -flatten a_overlay.gif
However I am struggling to convert this into PerlMagick. My code currently looks like this:

Code: Select all

use Image::Magick;
my $gif = new Image::Magick;

$x = $gif->Read(filename=>'png:test.png');
warn "$x" if "$x";

$x = $gif->Flatten(background => 'LightSteelBlue');
warn "$x" if "$x";

$x = $gif->Write(filename=>'gif:test.gif');
warn "$x" if "$x";
This fails with "Usage: Flatten(ref)" on both the following systems:
  • Windows XP / ActiveState 5.8.8 / ImageMagick 6.3.3
  • Debian Linux / Perl 5.8.4 / ImageMagick 6.3.3
I have searched all documentation, forums, Google etc, but without success. I have even considered sticking with the PNG format and have gone through the whole "PNG in IE" fun and games; however I haven't resolved the basic problem that the PNG files are just too big. I have tried the various options to reduce file size (compress, depth, colors, quality) but the only one that seems to have any significant success in bringing down the size seems to be colors 256, which I feel is just too brutal, and in any case does not appear to be recognised by PerlMagick.

I am at the hair-tearing stage, so any assistance anyone is able to provide would be much appreciated.

Thank you.

Chris Roberts
trident.cjr
Posts: 7
Joined: 2007-03-06T03:14:51-07:00

Re: Error: Flatten(ref)

Post by trident.cjr »

I've received the following response on a separate mailing list:

Code: Select all

$x = $gif->Flatten(background => 'LightSteelBlue');
warn "$x" if "$x";
From the docs:

The Flatten() method flattens a set of images. For example,

Code: Select all

$p = $images->Flatten(background=>'none');
The sequence of images is replaced by a single image created by composing each image after the first over the first image.

a) Where is your 'sequence of images'?

b) Try Flatten with the 2nd image being a bkg
trident.cjr
Posts: 7
Joined: 2007-03-06T03:14:51-07:00

Re: Error: Flatten(ref)

Post by trident.cjr »

My new code is as follows:

Code: Select all

my $gif = new Image::Magick(size=>"400x300");

$x = $gif->Read('png:test.png');
warn "$x" if "$x";

$x = $gif->Read('xc:red'); # Does this qualify as a second image?
warn "$x" if "$x";

$x = $gif->Flatten(background => "none"); # Do I still need the background attribute?
warn "$x" if "$x";
 
$x = $gif->Write(filename=>'gif:' . 'test.gif');
warn "$x" if "$x";
Still gives me the same Flatten(ref) error! I've even tried creating a new image and Reading that in instead of the xc:red line, but still no joy. I tried using the Append statement after the two reads, but that failed with Image::Magick=ARRAY(0x197a72c).

I'm obviously well and truly off the mark and would be grateful for any guidance!

Thanks again

Chris
trident.cjr
Posts: 7
Joined: 2007-03-06T03:14:51-07:00

Re: Error: Flatten(ref)

Post by trident.cjr »

Further to my previous posts, my code is now:

Code: Select all

my $gifs = new Image::Magick(size => "200x150");
$x = $gifs->Read('xc:red', 'png:test.png');
warn "$x" if "$x";
$x = $gifs->Flatten();
warn "$x" if "$x";
$x = $gifs->Write('gif:test.gif');
warn "$x" if "$x";
Unfortunately the Flatten command now fails with: Image::Magick=ARRAY(0x835daf4).

Predictably enough, given the failure of the Flatten, I am left with an animated gif.

Interestingly, if I change the Flatten command to match the docs...

Code: Select all

$x = $gifs->Flatten(background => 'none');
.. I'm back to the Flatten(ref) error.

I have tried writing a background image and loading that instead of xc:red, but it behaved identically to the above.

I'm sure that this is probably just my lack of understanding of the Flatten command, but the docs are very brief on the subject, and I have been unable to find any working examples from which to crib. If anyone has any thoughts, I would appreciate it!

Thanks, Chris.
trident.cjr
Posts: 7
Joined: 2007-03-06T03:14:51-07:00

Re: Error: Flatten(ref)

Post by trident.cjr »

I appreciate that talking to yourself is the first sign of madness, but I think I've worked it out:

Code: Select all

use Image::Magick;
$gifs = new Image::Magick(size => "218x176");
$x = $gifs->Read('xc:red', 'png:test.png');
warn "$x" if "$x";
$gif = $gifs->Flatten();
$x = $gif->Write('gif:test.gif');
warn "$x" if "$x";
The problem was simply that I expected the Flatten command to act upon the existing IM object, I didn't appreciate that it returns a fresh object.

Chris Roberts
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Error: Flatten(ref)

Post by magick »

We updated the documentation (available tommorrow) to make it more clear that Flatten() returns an image object. The only clue was that the example was assigned to $p which throughout the documentation represents an image object by convention.
trident.cjr
Posts: 7
Joined: 2007-03-06T03:14:51-07:00

Re: Error: Flatten(ref)

Post by trident.cjr »

That's amazing thanks; I'm sure that will save a few grey hairs here and there!

Yes the fact that the $p was being used that way in the docs did occur to me; but unfortunately only once I had already started thinking along those lines.

Thanks again.

Chris.
maxgraphic
Posts: 3
Joined: 2007-12-17T02:22:32-07:00

Re: Error: Flatten(ref)

Post by maxgraphic »

This thread was helpful for me as I struggled with the same issue. The solution works great for me on PNGs, but not GIFs, which still Flatten with black as a background rather than white. If I resave the GIF as a PNG, it works, so format is the only difference.

Is there a different approach to accomplish this for GIFs with transparency? Leaving out the error checking, I'm doing:

Code: Select all

my $image = Image::Magick->new;
$image->Read(file => $fh);
$image->Read('xc:white');
$image = $image->Flatten;
Thanks.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Error: Flatten(ref)

Post by anthony »

If you are trying to remove transparency, then you have other options. flatten is only one.
Another good one to use is Border! if given a image sequence, it will preserve that image sequence!

See Remove Alpha Transparency
http://www.imagemagick.org/Usage/basics/#alpha_remove
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply