Create a PNG with a transparent background with PerlMagick
Posted: 2015-06-14T18:26:54-07:00
It took a good bit of groping the web to get enough pieces to put together code to create a PNG file with a transparent background. And then suddenly, it turned to white. No idea why. While it is stable, here's the recipe:
Brian
Code: Select all
use Image::Magick;
#use strict; # im.test.transparent.png.pl 2>&1 | grep -v "explicit package na"
$quanta=8; # 8 bits should do
$xyres='1200x800';
$ifn='hueg2.png';
$im = Image::Magick->new(size=>$xyres, depth=>$quanta);
$ie = $im->Set(alpha=>'Transparent');
$ie = $im->Read("xc:transparent"); # Color NONE == rgba(0, 0, 0, 0.0)
warn "$ie" if "$ie";
$ie=$im->Color(color=>"rgba(0,0,0,0.0)"); # "Color" whole img transparent!
warn "$ie" if "$ie";
$ie=$im->Draw(primitive=>'rectangle', points=>"2,2 1198,798",
stroke=>"rgba(2,0,255,1.0)", fill=>'none');
warn "$ie" if "$ie";
$ie = $im->Write(filename=>"PNG:$ifn");
warn "$ie" if "$ie";
printf("HG: IM=%s, xyres=$xyres, img=$ifn\n", $im);
undef $im;
- D:\music>identify hueg2.png
hueg2.png PNG 1200x800 1200x800+0+0 8-bit sRGB 1.92KB 0.000u 0:00.000
Brian