This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.
From: zentara <zentara@highstream.net>
Subject: Re: Saving an image from canvas
Newsgroups: comp.lang.perl.tk
Date: Sat, 20 Mar 2004 07:05:13 -0500
"ckumar" <chandrakumar_a@mailcity.com> wrote:
> I am having a canvas in that i drawing a graph with lines
> and some gif images.I like to save this graph in an jpeg
> or gif or some image format.Is this possible?If possible
> plz help me in saving the graph from a canvas.
Read perldoc Tk::Canvas, and search for postscript.
You can output a postscript file of your canvas like this.
It will give a transparent background unless you put a big
colored rectangle on the $canvas as a background, or you can
edit the ps file afterward.
You can then you Image::Magick to convert the ps file to jpg or
whatever.
Alternatively, you could just take a screenshot.
#!/usr/bin/perl
use Tk;
#to change the background color, edit the ps file
# 0.000 0.000 0.000 setrgbcolor AdjustColor
# fill
$width = 800;
$height = 500;
my $main = MainWindow->new();
my $canvas = $main->Canvas( -width=>$width, -height=>$height,
-background=>"black");
$canvas->pack( -expand=>1,-fill=>'both');
&create;
$canvas->update;
$main->update;
$main->Button(
-text => "Save",
-command => [sub {
$canvas->update;
my @capture=();
my ($x0,$y0,$x1,$y1)=$canvas->bbox('all');
@capture=('-x'=>$x0,'-y'=>$y0,-height=>$y1-$y0,-width=>$x1-$x0);
$canvas -> postscript(-colormode=>'color',
-file=>$0.'.ps',
-rotate=>0,
-width=>800,
-height=>500,
@capture);
}
] )->pack;
MainLoop;
sub create{
$canvas->createOval(100, 100, 600, 600,-fill=>'green')
}
__END__
===