Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Goo Canvas and transparent images

by zentara (Archbishop)
on Jul 03, 2008 at 20:02 UTC ( #695452=sourcecode: print w/ replies, xml ) Need Help??

Category: GUI Programming
Author/Contact Info zentara of perlmonks
Description: A discussion arose recently about overlaying radar weather plots on a map. Well this kind of demonstrates all you need. It shows how to load images onto the canvas, and make a color transparent. The first time it runs, it gets a little map from google, and the overlay is base64encoded into the script. It is a little green plane that flies in circles over the map. This will work with a Gnome2::Canvas also (with some minor syntax changes), but I use Goo because it implements saving to postscript (amoung other formats).

Updated 7/25/2008... added more transparency to the airplane....it was solid green, on a black background, in which the canvas made black transparent. I added semi-transparency to it, so it looks similar to see the thru clouds of a weather map. This was done by adding an alpha layer to the plane itself, and setting the alpha to black. This made the green semi-transparent by removing it's black constituent.

#!/usr/bin/perl -w
use strict;
use warnings;
use Goo::Canvas;
use Gtk2 '-init';
use Glib qw(TRUE FALSE);
use MIME::Base64;

# get a tiny map from web, if not previously downloaded
if(! -e 'usa.gif'){ 
      print "getting file from net\n";
      &get_web_file;
  }

sub get_web_file {
use LWP::Simple;
use HTTP::Status;
my $URL = 'http://www.google.com/mapdata?CxXA8zUCHYWJS_og____________A
+QwtwPM1AjWFiUv6QLACSJwBUgJVU2gBkAEP';
my $localfile = 'usa.gif';

my $rc = mirror( $URL, $localfile );
warn status_message( $rc ) if is_error( $rc );
print "done retrieval\n";
}


my $window = Gtk2::Window->new('toplevel');
$window->signal_connect('delete_event' => sub { Gtk2->main_quit; });

my $canvas = Goo::Canvas->new();
$canvas->set_size_request(320, 160);
$window->add($canvas);

my $root = $canvas->get_root_item();

# base file
my $im = Gtk2::Gdk::Pixbuf->new_from_file("usa.gif");
my $w = $im->get_width;
my $h = $im->get_height;
my $image = Goo::Canvas::Image->new(
$root, $im, 0, 0,
'width' => $w,
'height' => $h);


my $plane = get_plane();
my $pixbuf = do {
        my $loader = Gtk2::Gdk::PixbufLoader->new();
        $loader->write( $plane );
        $loader->close();
        $loader->get_pixbuf();
    };
    
my $pb2 = $pixbuf->add_alpha(1,0 ,0 ,0);     
# returns a new pixbuf
# makes black transparent
# use 0..255 for color levels, NOT hex

my $w1 = $pb2->get_width;
my $h1 = $pb2->get_height;
my $image1 = Goo::Canvas::Image->new(
$root, $pb2, $w/2, $h/2,
 'width' => $w1,
'height' => $h1);


$window->show_all();


my $id = Glib::Timeout->add (100, sub {
      $image1->rotate (5, $w/2, $h/2); #degrees ,center of rotation
           return 1;
    } );

Gtk2->main;

sub get_plane{
return  decode_base64(
'iVBORw0KGgoAAAANSUhEUgAAAFYAAAA/CAYAAAB6rT9lAAAABmJLR0QA/wD/AP+gvaeTA
+AAACXBI
WXMAAAsTAAALEwEAmpwYAAANhklEQVR42u2ay49cV17HP+dxX3WrqttuO227/QgmExPmFZ
+OEPIAZ
FjBICIksQOIPYMNq0Ii/AiHBLBAbVqxGGiSEEAgYgYhGQwKBSQLJMFach2On7Xba3VV13/
+e8WNxy
0rESYmcmMx2mvpuSSlW6v/O93/P9Pc6BFVZYYYUVPimIwxDEV8NXH3+e5xONOLbHXnSWz2
+y1LGSH
ERopBMJNWQ9XuHxtjSPWEnYf42L3dfGn/3ZYidU/9ggCm//EP2Sv8uo4IR33uLjBjioK6f
+FyCFKH
nNy+zdVpTGQa6n7BXklgE8HOitg78Afh95/8G/4q1cRnR4zT+zm/5UG8xAtvA/qX+fIjAP
+/CM/8J
mItcPOvx4S3eTDVx+wCneTr81vk/En/y7IrYA3iNy+pNbiQbmKikjSuq2/EoQPX0SiDDMk
+7XUGmP
FTVFvMuu2+dm/BqX7Uqxd6DB4oCeIABmzHqPFRp5zOLldbbbZYhHJdLssmskOngQjl5YlG
+w4lLwi
f/whuJAiQoRyMToA+KVqPU4aGrGMUwLE6CCJjCL3GudXyesDkKFZQ9ubbM96SBwtHT6ZMj
+1a0qtd
ZqWAEKNlykjvsreXkLQd3eImt9ojrPkNpmFF7B04yzlznvvVLovuLXbCGhv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Gu0MLHbYKYAZ4CxyJNDKYrzHqwjpA0J1NLVZqtthREuNIgqGtg
+NMQlKv
c7R+iVcuId7YBvgOz/yQhzCC7V8LXzl9khdMh/MLap2TJt3ysvBBJIigyCiYNRMyXzGvM4
+6wxzOl
gWiTTbPNtomJXUTi1VCnWoFyEdpLpE+G7O4Uqne4brARJcCJhMxLChuhvUI7UFIgQ4T1I0
+YesMfY
NDtsNxFR/zCflzULmZDT0op11n1Pw3ux9wdiT8KCqh0z9gpVPc7PdS+JV7Y/0enWH4o//v
+cfwpDl
REQ0BbTF1w6DZmwkUuwz2+uoG48vSroaqB2u0yTO0sFQQvmSsvB4t8vNnR6fHeHIUUHwDt
+f19A1Q
pUQlcNUIc+OZe1TZQfwg//3RTrcEN7bYsoDLyFzKyI7IbMbIarQZMbUSzIiJA2zO0X6TE/
+0WWyYn
N4BLyW1C5EZMbUzUj8lMzsilpP2EzAF2iy37UX38/7t57JTYj0isQjQRUTDYW8sqoVtnZC
+uKGtpW
k7c/y4X+eZ59DeAxnvzpS7wiJH0jQIwZrYEVBrsPPkREdUC0IxI/ZXooThh+pMRe4LPmGj
+eq8zxQ
j5l2E44oRyMtfbPGmhuRF5vc50ps90s82T0vnr0G8Gh48nSECBrR7LBrznJ6WjKXEalVRK
+GlWhzj
mNHI6iwXDPz94ZrHftL4Xnjh19/gqswZXyxo1HE2vgKwzbVvpcTuOjf+e8rU/7b4nb/+oP
+9/M3zj
N3ep5GlOfNHQiFOc/lWJCDvc+NaEiZtTvHSWLXdRPPq3P1GKPc9nzCanhEU2ll5kZAXAGc
+6UGhkc
pgbxoS/7F/hSV9ORMarAkpEVAiHOcbYEjcN0Cfmn4kxshRVWWGGFFVZYYYUVVlhhhRVWWG
+GFFVb4
icP/Ao7nzeTzMzF3AAAAAElFTkSuQmCC');
}

Comment on Goo Canvas and transparent images
Download Code

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://695452]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-12-20 23:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (99 votes), past polls