Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monks

Tk Screen and Canvas Screenshots

by you!!!
 | log zentara out | zentara | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | Library | 
 | Obfuscation | Poetry | Cool Uses For Perl | Snippets | Code | Q&A | 
 | Perl News | Reviews | Tutorials | Newest Nodes | Offering Plate | 

on Jun 04, 2004 at 13:56 GMT+11 ( #361299=snippet: print w/ replies, xml ) Need Help??
Reputation: 19

Description: One of the most asked questions in Tk is "how do I print out my canvas, or at least get a screenshot?" Well the answers I've always seen is export it using the "postscript function". Well, that is trickier to use than one would hope for.

Here is a simple demo of grabbing screenshots of the root window, the mainwindow, and just the canvas. It is linux only and uses Tk::WinPhoto, a basic part of Tk.

#!/usr/bin/perl
use warnings;
use strict;
use Tk;
use Tk::WinPhoto;
use Tk::JPEG;

my $mw = tkinit;

my $canv = $mw->Canvas(width => 300, height => 200)->pack();
# Create line inside the canvas  
$canv->create ('line',1, 1, 100, 100, -fill=>'red');

$canv->createRectangle(10,20,30,40, -fill=>'blue' );

my $fullbutton = $mw->Button(-text=>'Full Screen Capture',
                             -command => \&full_capture,
                            )->pack;

my $mainbutton = $mw->Button(-text=>'MainWindow Capture',
                             -command => \&mw_capture,
                            )->pack;

my $canvbutton = $mw->Button(-text=>'Canvas Capture',
                             -command => \&canv_capture,
                            )->pack;


MainLoop;

sub full_capture{

my @id = grep{$_ =~ 'Window id'} split("\n",`xwininfo -root`);
my @ids = split(' ',$id[0]);
(my $id) = grep{$_ =~ /0x/} @ids;

my $image = $mw->Photo(-format => 'Window',
#                    #    -data => oct($mw->id)
#                    #    -data =>  oct('0xa00022')
                          -data =>  oct($id)
                       );

my $pathname = './rootwindow.'.time.'.jpg';
$image->write($pathname, -format => 'JPEG');

}

#########################################################
sub mw_capture{

my $image = $mw->Photo(-format => 'Window',
                        -data => oct($mw->id)
#                    #  -data =>  oct('0xa00022')
                     #  -data =>  oct($id)
                       );

my $pathname = './mainwindow.'.time.'.jpg';
$image->write($pathname, -format => 'JPEG');

}
##########################################################

sub canv_capture{

my $image = $mw->Photo(-format => 'Window',
                        -data => oct($canv->id)
                       );

my $pathname = './canvas.'.time.'.jpg';
$image->write($pathname, -format => 'JPEG');

}
##############################################################

Edit your snippet here
comment on Tk Screen and Canvas Screenshots
Download Code
Re: Tk Screen and Canvas Screenshots
by kragen on Jun 04, 2004 at 16:12 GMT+11
Reputation: 11
    That's pretty clever. I didn't know about this Window format for Photo. Is it new?

    PNG might be a better choice than JPEG for storing Canvas screenshots; Canvases are likely to contain large areas of solid color and sharp color boundaries, which PNG handles beautifully and JPEG handles terribly.

 [reply]
Reputation: 7
      No it's not new, its just buried way down in Mastering PerlTk's chapter on images. It's been in TCL for along time. I guess people just havn't discovered it's full usefulness.

      Maybe the ease of ImageMagick's import program, has prevented more people from digging around for it. It's best use is to grab screenshots of Tk sub-widgets like a Canvas. Import won't do that without "rectangular-mouse-selection" which is kind of "inexact". And Canvas's export to postscript function has some glitches, especially when individual postscript elements are not visible. A straight pixmap shot is so much cleaner.

      Just put "use Tk::PNG;" at the top, and adjust the code accordingly to get png snapshots.


      I'm not really a human, but I play one on earth. flash japh
 [reply]

Back to Snippets Section

XP Nodelet
You have 21 votes left today.
Node Status
Node Type: snippet [id://361299]
help
Chatterbox
Elgon directs talexb in the direction of Wassercrats...
<demerphq> A little while ago a bunch of us were discussing the use of redo and bare blocks, my sandpit code has such a loop and IMO is acceptable use, im curious what you or how you would have done the same.
<demerphq> L~R I dont really follow your logic. If they have never edited their pads they will see nothing at all on their scratchpad. they cant use their scratchpad without editing it (caveate pmdevil)
<davido> I remember that discussion.
<demerphq> demerphq's sandpit see the BUILD: tag.
<Limbic~Region> demerphq - It isn't something I feel strongly about but there is no indicators for a brand new user to the site what a scratch pad is, what it is used for, or that there are private/public sections - maybe that should be better documented
<Limbic~Region> or perhaps it is and I should ask the SiteDocClan where to find the info
<tye> gah! You'd think perldoc.com would have better taste than to have one of those drop-down menus with no button.
<demerphq> hmm, well given that a new user will see an edit link next to their scratchpad on their home view i reckon they wont stay confused for long

How do I use this? | Other CB clients