Hello, I am very new to perl so hopefully this is something simple. I have a script that reads a data file and creates a png image based on the data for each record in the file. The first pass through the loop the image created in 56kb, all the rest of the images are 145kb. In the while loop, I am creating a new image object for each pass. I am guessing I need to destroy the image object before creating the new one but I am not sure how.

When I first built this, every image after the first one had a black background and I had to place a white rectangle on the image first to make the background white. I think the black background is where the extra size is coming from.

Any suggestions would be appreciated.

Thanks

George

Code snippet is below:

#!/usr/bin/perl use GD; # size of image $maxx = 600; $maxy = 500; $numGrids = 33; # A background inmage to add open (PNG,"gradient/river.png") || die; $mapImage = newFromPng GD::Image(\*PNG) || die; close PNG; open(FILENAME,"gradient/current_r") || die "open failed"; { my @tmp; my @data; my $line1 = <FILENAME>; $count=0; while ($line2 = <FILENAME>) { @tmp = split(' ', $line2); @PRESOBS = @tmp; # create a new image $im = new GD::Image($maxx+50,$maxy); # enough for y axis label +s $im->trueColor(1); # allocate some colors $white = $im->colorAllocate(255,255,255); $black = $im->colorAllocate(0,0,0); #$red = $im->colorAllocate(255,0,0); #$blue = $im->colorAllocate(0,0,255); $grey = $im->colorAllocate(128, 128, 128); #$green= $im->colorAllocate(0, 255, 0); # make the background transparent and interlaced $im->transparent($white); $im->interlaced('true'); # hack added to images after the first one do not have a black backgro +und $im->filledRectangle(0,0,$maxx+50, $maxy, $white); .... Code here to draw the image elements #Load map image $im->copy($mapImage, 0, $maxy/2+1, 0,0,600,250); # Put a black frame around the picture $im->rectangle(0,0,$maxx-1,$maxy-1,$black); $im->line(0, $maxy/2, $maxx, $maxy/2, $black); $png_data = $im->png; $hour = sprintf("%02d", $PRESOBS[0]); $xnum = sprintf("%d", $count); my $file = "EtaGradient". $xnum.".png"; open(DISPLAY,">$file") || die "open failed"; binmode DISPLAY; print DISPLAY $png_data; close DISPLAY; print "Output file = ".$file."\n"; $count++; } close (FILENAME); }

In reply to Images created in a loop grow by gtm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.