Create a PNG with a transparent background with PerlMagick

PerlMagick is an object-oriented Perl interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning PerlMagick.
Post Reply
BrianP007
Posts: 49
Joined: 2013-12-13T09:54:14-07:00
Authentication code: 6789

Create a PNG with a transparent background with PerlMagick

Post by BrianP007 » 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:

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

User avatar
fmw42
Posts: 25742
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Create a PNG with a transparent background with PerlMagick

Post by fmw42 » 2015-06-14T18:43:43-07:00

I moved this post to the PerlMagick forum. That seems the most appropriate place for this question.

Post Reply