Page 1 of 1

Create a PNG with a transparent background with PerlMagick

Posted: 2015-06-14T18:26:54-07:00
by BrianP007
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:

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
I am not sure whether every line is necessary, but it is workin' so I'm not fixin'

Brian

Re: Create a PNG with a transparent background with PerlMagick

Posted: 2015-06-14T18:43:43-07:00
by fmw42
I moved this post to the PerlMagick forum. That seems the most appropriate place for this question.