Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Perl::Tk blur screenshot of window and raise it

by pashanoid (Scribe)
on Sep 08, 2011 at 05:55 UTC ( [id://924726]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Bretheren, I'm trying to implement an android-shutdown-like effect on a main window when a user opens a dialog. The main window should be 1. captured, 2. blurred, 3. displayed .Seems like a simple thing but for some reason I can't quite get it working. Here are the bits of code I'm trying to use:

my $screen = $mw->Photo(-format => 'Window', -data => oct($c->id)); #$ +c is my canvas my $pathname = '/home/pasha/dial.xpm'; my $image = Image::Magick->new(magick=>'XPM'); $image->Read(data=>$screen); #$blurred->write($pathname, -format => 'JPG'); #$screen->write($pathname, -format => 'XPM'); $image->Blur(geometry=>"0x0.8"); $image->Write(filename=>'/home/pasha/screen.xpm');

I'd rather not mess with saving to a file at all. Thank you!

Replies are listed 'Best First'.
Re: Perl::Tk blur screenshot of window and raise it
by zentara (Archbishop) on Sep 08, 2011 at 10:00 UTC
    I'd rather not mess with saving to a file at all.

    In the abscense of a full working code snippet, here is an example that should show you the tricks needed, especially Image::Magick's BlobtoImage() and ImagetoBlob() methods. IM calls the scalar output of images blobs, and with base64encoding you can put them directly into Tk Photo objects, with no intermediate disk file. This example finds .jpgs in the current directory and resizes them upon a window resize, with an optional charcoal effect.

    In your code, after the screenshot, load the binary screenshot $screen to a IM object with -data ( as you show) , work on it's effects, then redisplay it using a base64encoded ImagetoBlob(). Also notice the <visibility> binding, which you may or may not need.

    UPDATE: see second response below, I don't believe IM can Read( -data), ie. a binary screenshot in a scalar. You need BlobtoImage, to load it.

    #!/usr/bin/perl -w use strict; use Tk; use Tk::JPEG; use Image::Magick; use MIME::Base64; my $val = 0; my $im = Image::Magick->new; # a single object for thumbnails my $c; my $dir = "."; my @fl = <*.jpg>; print "@fl\n"; my $main = new MainWindow; $main->Label( -text => "Resize Window to see resizing and charcoal eff +efct", )->pack(); my $photo_obj = $main->Photo(-file => '' ); my $display = $main->Label(-width => 200, -height => 200, -image => $photo_obj, )->pack(-fill=>'both', -expand=>1); &loadpic( $fl[0] ); my $frame = $main->Frame()->pack(); my $nxt = $frame->Button(-text => "Next", -command => \&nextp)->pack(-side=>'left'); $frame->Button(-text => 'exit', -command => sub{destroy $main} )->pack(-side=> 'left'); $frame->Checkbutton( -onvalue => 1, -offvalue => 0, -text => 'Charcoal', -variable => \$val)->pack(-side=> 'left'); #$main->bind("<Visibility>", sub { $nxt->invoke }); $main->bind("<Visibility>", \&nextp); MainLoop; sub loadpic { my $filename = shift; $photo_obj->blank; #clear out old jpg my ($h,$w) = ($display->height, $display->width); # # how to scale the pic? # Create new size version $im->Read($filename); $im->Scale( geometry => $w.'x'.$h ); if($val){ $im->Charcoal('0x1') } #Tk needs base64 encoded image files my $data = encode_base64( $im->ImageToBlob() ); undef @$im; # blank $im object $photo_obj->put($data); $display->configure(-image => $photo_obj); } sub nextp { push (@fl,shift(@fl)); #circular list my $f = $fl[0]; loadpic("$dir/$f"); }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Perl::Tk blur screenshot of window and raise it
by zentara (Archbishop) on Sep 08, 2011 at 10:13 UTC
    I just noticed, and I havn't tested it, but I'm not sure whether an ImageMagick object works with Read( -data ....), I think you may be confusing it with another Image module. You probably need BlobtoImage()
    #!/usr/bin/perl use warnings; use strict; use Image::Magick; my $blob = `import jpg:-`; #my $blob = `import -`; #postscript produced #print "$blob\n"; # now $blob is in memory and you can do what you # want with it. my $output = Image::Magick->new(magick=>'jpg'); $output->BlobToImage( $blob ); $output->Resize(geometry=>'160x120'); $output->Write( $0.'.jpg'); # png works too

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Perl::Tk blur screenshot of window and raise it
by Anonymous Monk on Sep 08, 2011 at 06:47 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-24 11:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found