Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Imager drawing question

by bradcathey (Prior)
on Sep 27, 2005 at 19:16 UTC ( [id://495513]=perlquestion: print w/replies, xml ) Need Help??

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

Fellow Monasterians,

I've been using Imager to size and optimize uploaded photos without any problems. But now I'm trying to draw graphics and nothing is happening. Does it write to screen or only to a file? Do I have to create an image that it writes to?

I've read all the doc I could find, but can't find an actual working example for something as simple as drawing a box or line. I just need a little nudge to get me going. Here's what I have:

use Imager; my $img=Imager->new(xsize=>400,ysize=>300,channels=>4); #needed? $img->line(color=>'#444444', x1=>10, x2=>100, y1=>20, y2=>50, aa=>1, e +ndp=>1 ); ...missing something?...

The above produces a wonderful blank screen. Thanks.

"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Replies are listed 'Best First'.
Re: Imager drawing question
by ikegami (Patriarch) on Sep 27, 2005 at 20:30 UTC
    Does it write to screen or only to a file?

    Neither really. It creates and modifies images in memory. It has a method to write what's in memory to an image file, but none to write to the screen. You'd need some sort of GUI library (Tk, Win32::GUI, etc) to do so.

    (As far as I can see)

Re: Imager drawing question
by Xaositect (Friar) on Sep 27, 2005 at 19:37 UTC

    I think you're looking for the $img->write() method. From the synopsis on the basic Imager pod, you can do $img->write(file=>$filename)

    Imager::Files seems to document how to write your object, and there's a section for "Producing an image from a CGI script", if that's what you're interested in.

    Xaositect -
Re: Imager drawing question
by davidrw (Prior) on Sep 27, 2005 at 19:51 UTC
    I've used this to make a blank white greyscale image:
    my $image = Imager->new( xsize=>$X, ysize=>$Y, channels=>1 ); $image->box(filled=>1, color=> Imager::Color->new(255,255,255) );
    You might try passing the Imager::Color object instead of '#444444' to the $img->line() method...
Re: Imager drawing question
by tonyc (Friar) on Sep 28, 2005 at 02:31 UTC

    You don't really provide a lot of context - is this code in a CGI script? are you trying to display it from Tk?

    The changes you make to an Imager object won't be displayed or written anywhere until you call the write method.

    There's both sample code and documentation showing how to return an image from a CGI script.

    If you want to see what an image looks like from the console, for example, if you're fiddling with the code and want to see what you're producing, write the image to a file and then use some sort of image viewer to view the image.

    Another option is to use Tk to view the image, but it's kind of clumsy:

    #!perl -w use strict; use Tk; use Tk::Photo; use MIME::Base64; use Tk::PNG; use Imager; my $image = Imager->new(xsize=>100, ysize=>100); # draw something simple here, you'll probably do something more comple +x $image->box(filled=>1, color=>'blue'); $image->box(filled=>1, color=>'red', xmin=>20, ymin=>20, xmax=>79, ymax=>79); my $image_data; $image->write(data =>\$image_data, type=>'png') or die "Cannot save image: ", $image->errstr; # supplying binary data didn't work, so we base64 encode it $image_data = encode_base64($image_data); my $main = MainWindow->new; my $tk_image = $main->Photo(-data => $image_data); $main->Label(-image=>$tk_image)->pack; MainLoop;

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-06-20 16:37 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.