Newbie here.
This command-line incantation does what I want to my gif which has transparent areas:
convert image.gif -background \#FFFFFF -flatten image.jpg
I am having trouble figuring out how to do this in Perl. I've tried every combination of set, flatten, background, matte, etc. that I could find by googling, but no matter what, I always get a ~default neutral gray~ background color of #c8d0d3 to replace the transparent parts of the gif (and on another version of IM we use the background seems to have turned black but I have not verified this). I know jpg doesn't support transparency, but I want the color that replaces the transparent part to be ~white~. Here's what I have that does not work:
my $file = shift or die;
my $error;
my $img = Image::Magick->new();
$error=$img->Read($file) and die $error;
@$img=($img->[0]); #flatten
$file=~s/gif$/jpg/;
$img->Set(background => 'white');
$img->Flatten();
$error=$img->Write(
filename=>$file,
) and die $error;
Thanks to anyone who can help.
Choosing a background color when converting from gif to jpg
Re: Choosing a background color when converting from gif to jpg
the only progress i've made is that a friend discovered that the neutral gray appears to come from the image itself, as it appears to be one of the indexed colors. (if you run this on a simpler indexed image, the transparent part is converted to one of the indexed colors of the simpler image (not gray)). i tried to change the mode of the image to RGB by using "quantize" because it seemed like it might be the thing to do, but that didn't have any effect either:
my $file = shift or die;
my $error;
my $img = Image::Magick->new();
$error=$img->Read($file) and die $error;
@$img=($img->[0]); #flatten
$file=~s/gif$/jpg/;
$img->Quantize(colorspace=>'rgb'); #doesn't seem to help
$img->Set(background => 'white');
$img->Flatten();
$error=$img->Write(
filename=>$file,
) and die $error;
my $file = shift or die;
my $error;
my $img = Image::Magick->new();
$error=$img->Read($file) and die $error;
@$img=($img->[0]); #flatten
$file=~s/gif$/jpg/;
$img->Quantize(colorspace=>'rgb'); #doesn't seem to help
$img->Set(background => 'white');
$img->Flatten();
$error=$img->Write(
filename=>$file,
) and die $error;
Re: Choosing a background color when converting from gif to jpg
To update anyone who's following this, I've written some code that seems like it should work, and sometimes works, but sometimes doesn't, and I've written a hack that seems to fix the cases where it doesn't work. I don't know if I've found an imagemagick bug, or a corrupted gif, or if there's just some subtle point i'm missing. i'll try to post more on this later as i figure more out, including the actual image file that is giving me the problem.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Choosing a background color when converting from gif to jpg
If a background setting has NOT been defined in IM, then IM will use the 'background' meta-data entry of the image being processed. However only a few image formats provide such a setting, If it isn't defined, or in the image meta-data, it will then default to white.
When a -background (command line API) is set, then this overrides the default setting, and the image meta-data setting, for images currently in memory, and images that are read in later. The same thing happens for some of the other color settings like bordercolor and fill, though these are even less common in image file format meta-data.
GIF defines a background color as part of its normal meta-data content, and is ment to be used as the background color for some of the disposal settings. All GIF animation programs of today however ignore this and just use transparency as the 'background' for disposal purposes. As a consequence you generally can not rely on the meta-data setting defined in the image file to be sensible.
It would be nice if you can use -set background {color} to set a specific (and different) background color (or border) for individual images, but that is an option that is currently available.
EG: this does not work which if it did would use the default (image defined) background color for image 1 and a blue background color for image 2.
The only time I have managed to set a different background color for one image and ont the other was with this command where the first image had a blur background and the send had a default background (not pink). It is unknwon why this happened as it does not make sense.
When a -background (command line API) is set, then this overrides the default setting, and the image meta-data setting, for images currently in memory, and images that are read in later. The same thing happens for some of the other color settings like bordercolor and fill, though these are even less common in image file format meta-data.
GIF defines a background color as part of its normal meta-data content, and is ment to be used as the background color for some of the disposal settings. All GIF animation programs of today however ignore this and just use transparency as the 'background' for disposal purposes. As a consequence you generally can not rely on the meta-data setting defined in the image file to be sensible.
It would be nice if you can use -set background {color} to set a specific (and different) background color (or border) for individual images, but that is an option that is currently available.
EG: this does not work
Code: Select all
convert image1.gif \( image2.gif -set background blue \) -shadow 100x0 miff:- | display
The only time I have managed to set a different background color for one image and ont the other was with this command
Code: Select all
convert -background blue image1.gif -set background pink image2.gif -shadow 100x0 miff:- | display
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: Choosing a background color when converting from gif to jpg
thanks for getting back to me!
my solution (for the moment) was giving up and using the shell from within perl (note that this command is also attaching a watermark, but i had the same issues with or without the watermark):
this is working fine for now, but it's slow.
my solution (for the moment) was giving up and using the shell from within perl (note that this command is also attaching a watermark, but i had the same issues with or without the watermark):
Code: Select all
#perlmagick isn't working for this, so use the shell instead
my $cmd="convert $gif_location -background white -flatten watermark.png -geometry +$wmk_x+$wmk_y -composite $jpg_location";
#print "$cmd\n";
system($cmd);
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Choosing a background color when converting from gif to jpg
There is a PerlMagick Flatten that can be used. I just do not know the syntax myself. I am expert at command line usage, and know a lot about core workings, just not that much about other API's
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/