Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Using GD on MS Windows

by SparkyEE (Initiate)
on Aug 29, 2006 at 14:57 UTC ( #570184=perlquestion: print w/replies, xml ) Need Help??

SparkyEE has asked for the wisdom of the Perl Monks concerning the following question:

Hello! This newbie is trying to use the GD module with an ActiveState Perl installation. I've had good luck with using Tk for making GUIs, and graphs, but Tk isn't so good for PRINTING or SAVING a canvas. So.. I'm trying out GD. But the "hello world" code found on the web doesn't spawn a window, it just dumps junk to the console... Actually no GD example I've found actually displays anything. What am I missing? The code is below. Thanks much!!
#!/usr/bin/perl -w use GD; # create a new image $image = new GD::Image(100,100); # allocate some colors $white = $image->colorAllocate(255,255,255); $black = $image->colorAllocate(0,0,0); $red = $image->colorAllocate(255,0,0); $blue = $image->colorAllocate(0,0,255); # make the background transparent and interlaced $image->transparent($white); $image->interlaced('true'); # Put a black frame around the picture $image->rectangle(0,0,99,99,$black); # Draw a blue oval $image->arc(50,50,95,75,0,360,$blue); # And fill it with red $image->fill(50,50,$red); # make sure we are writing to a binary stream binmode STDOUT; # Convert the image to PNG and print it on standard # output print $image->png;

Replies are listed 'Best First'.
Re: Using GD on MS Windows
by cdarke (Prior) on Aug 29, 2006 at 15:28 UTC
    It looks like your example was probably designed to be used with CGI. The final print statement writes the image to STDOUT, which is the console. If you run your script and redirect to a file, then double click on the output from Windows Explorer you will find it is a valid png image. For example: > dot.png
    GD creates an image file, it does not display it.
      Thank you! and Ugh.. So I'd have to simultaneously draw the canvas using Tk and draw the image in GD. Are there any other options to display a graph and then save it using one module??

      Win32::Gui - no drawing function?
      Imager - doesn't spawn a desktop window (like GD)
      Magick - Do i really have to force users to install another application?
      GD - does not display
      Tk - does not save canvas?

        The following way should work with Tk (haven't tested it):

        use MIME::Base64; # ... your GD code from above my $imageData = $image->png; # save $imageData to file with binmde... well you know that... open( my $PNG, ">", $imageFilename ) or die ...; binmode( $PNG ); print $PNG $imageData; close( $PNG ) or die ...; # convert $image to tk photo my $imageData64 = MIME::Base64::encode_base64( $imageData ); my $photo = $mw->Photo( -format => 'png', -data => $imageData64, ); # do something with $photo...

        I used a similar way for a little slideshow I wrote...

        Best regards,
        perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8."

        GraphViz works well for me on Win32, and there's Tk::GraphViz, though I've never used it.


        You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.
        I don't think your reply, at Re^2: Using GD on MS Windows , follows either from your original post, nor from the excellent answer, above.

        Just what is it that you want to achieve?

        • Create an image to be saved as a .png file?
        • Draw a graph, live and atop the windows desktop, from some (streaming?) input data?

        If the former, you have the answer above, and came close with your adaptation of the original sample code (which could be edited to specify a file write, rather than output to STDPUT which must then be redirected. If the latter, you may which to ppm -s for the various Graph:: modules.

Re: Using GD on MS Windows
by odha57 (Monk) on Aug 29, 2006 at 20:54 UTC
    I tried out Tk with GD somewhile back on linux, but ended up using a web server due to the access required. Here are a couple of snippets for you from my hacking around. I made this work by having GD output to a png file, and then with Tk presenting that file. The modules used were: Tk, Tk::PNG, and GD::Graph using the area format (GD::Graph::area). First making a graph with GD:
    ##### build a cpu graph... $poop = 'CPU - Wait IO data from '.$startdate.' - '.$enddate; # a fancy title for our graph @data = (\@dateline, \@graphcpu_usr, \@graphcpu_sys, \@graphcpu_wio ); + # this is an array of arrays. The first one is the X axis + # which is the time, the others are the Y axis my $data = GD::Graph::Data->new() or die GD::Graph::Data->error; $data->copy_from(\@data); + # this tells the graph to get the data from the data array my $my_graph = new GD::Graph::area(400,300); + # and this says what type (area) and it's size $my_graph->set( + # and set up all the particulars... r_margin => 1, x_label => 'Time', y_label => 'CPU Utilization', title => $poop, y_min_value => 0, y_tick_number => 8, y_label_skip => 1, x_ticks => 1, x_label_skip => $x_label_skip, x_labels_vertical => 1, x_label_position => 1/2, cumulate => 1, transparent => 1, )or warn $my_graph->error; open (IMG, ">dbcpu.png") or die "Can't open up the dpcpu graph file"; binmode IMG; $my_graph->set( dclrs => [ qw(lgreen lblue lred) ] ); + # here is the colors in the graph $my_graph->set_legend("usr","sys","wio" ); + # and the legend text $my_graph->set_title_font(GD::Font->MediumBold); print IMG $my_graph->plot($data)->png(); + # and this plots the image out to a png format close IMG;
    And here is where in TK the graph is displayed:
    $fr = $mw->Frame(-relief => 'groove', -borderwidth => 5)->pack( -side => 'left', -expand => 1 ); # the canvas has scroll bars and we sized the viewable part at 625x800 $canvas = $fr->Scrolled('Canvas', -height => 625, -width =>800,)->pack( -side => 'right', -expand => 1 ); # here we define the cpu graph and where we find it $dbcpu = $canvas->Photo(-format => 'png', -file => '/home/mjacobs/scripts/perl/dbcpu.png', -palette => '16/16/16'); # and this actually puts the image on the canvas $dbcpu_gr = $canvas->createImage(300,250, -image => $dbcpu, -tags => "dbcpu", -anchor => "center");

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://570184]
Approved by prasadbabu
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2021-06-24 16:37 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (130 votes). Check out past polls.