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

Hi All, I am trying to re-write an older perl/Tk script that would download an image from a webcam and display it. I would like to use Gtk2 but there is very little example code and documentation available for it ( that I can find - any links would be appreciated ). This bit of code almost does what I need it to minus two things. 1. The image does not update. 2. The image does not scale when the window is resized. One other thing, I would really like to avoid writing the data to a temp file if I can, I would rather download the image data and pipe it directly to Gtk2::Image. Any help would be greatly appreciated !! Thank You ! -T
#!/usr/bin/perl $|=1; use strict; use Gtk2 '-init'; use MIME::Base64; use LWP::Simple; my $load_timeout = undef; my $CAM_URL = "http://www.livecam.com.au/livecam1/livecam.jpeg?mu +="; my $window = Gtk2::Window->new('toplevel'); $window->signal_connect(delete_event => \&delete_event); $window->signal_connect(destroy => sub { Gtk2->main_quit; }); $window->set_border_width(10); my $image = Gtk2::Image->new(); $window->add ($image); $image->show; $window->show; $window->show_all; do_load(); Gtk2->main; sub do_load { getstore($CAM_URL.time(),"/tmp/latest.jpg" ); $image->set_from_file("/tmp/latest.jpg"); $image->show; $window->show; unlink("/tmp/latest.jpg"); $load_timeout = Glib::Timeout->add (6500, \&do_load); }; 0;

Replies are listed 'Best First'.
Re: Gtk2::Image Reloading Refreshing and scaling
by zentara (Cardinal) on Aug 09, 2005 at 18:15 UTC
    Automatic scaling of the image when the window is resized is not going to be easy to do.

    To refresh, setup a timer:

    my $timer = Glib::Timeout->add (100,\&show_refresh); # timer will run until callback returns false

    P.S. You don't need Mime::Base64 for a Gtk2 image.

    To load an image from data try this (I load from a file, but it is converted to scalar data before using) :

    #!/usr/bin/perl use strict; use warnings; use Gtk2 '-init'; my $mw = Gtk2::Window->new; my $vp = Gtk2::Viewport->new(undef, undef); $mw->add($vp); &load_image; $mw->signal_connect('destroy', sub { Gtk2->main_quit }); Gtk2->main; ########################################################### sub load_image { my $file = shift || 'bridget-1.jpg'; my $image_data; open (FH,"< $file"); read( FH, $image_data, -s FH ); close FH; my $loader = Gtk2::Gdk::PixbufLoader->new; $loader->write ($image_data); $loader->close; my $pixbuf = $loader->get_pixbuf; my $image = Gtk2::Image->new_from_pixbuf ($pixbuf); my $pb = $image->get_pixbuf; my ($x, $y) = ($pb->get_width, $pb->get_height); $vp->add($image); $mw->set_title("$file ${x}x${y}"); $mw->show_all(); }
    If you can deal with a temp file, there is a fantastic image viewer called Gtk2::ImageViwer. It has mouse zoom and pan, but it only loads from a file.
    #!/usr/bin/perl ###################################################################### + # Trivial use of Dov Grobgeld's GtkImageViewer. ###################################################################### + use Gtk2; use Gtk2::ImageViewer; Gtk2->init; $window = Gtk2::Window->new(); $window->signal_connect('destroy', sub { Gtk2->main_quit }); my $file = shift || 'bridget-1.jpg'; $imgv = new_from_file Gtk2::ImageViewer($file); $window->add($imgv); $window->show_all(); main Gtk2;

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