- @{[ $img->getcolors ]} == @imgpal
- or die "incompatible palette size\n";
- $img->setcolors(colors => \@imgpal);
- $img->write(data => \my $imgdata, type => 'png');
+ do {
+ if ($reindex) {
+ $img->to_paletted({
+ make_colors => 'none',
+ colors => \@imgpal,
+ translate => 'closest',
+ });
+ }
+ else {
+ @{[ $img->getcolors ]} == @imgpal
+ or die "incompatible palette size\n";
+ $img->setcolors(colors => \@imgpal);
+ $img;
+ }
+ }->write(data => \my $imgdata, type => 'png');