Re: loading an image from data, not file



On Wed, 19 Jan 2005 11:59:26 -0500
muppet <scott asofyet org> wrote:


On Jan 19, 2005, at 10:03 AM, zentara wrote:

Anyways, the code below works fine when loading the image  
new_from_file, but all I get is a bunch of colored pixels when trying  
new_from_data.

I'm guessing my "pack" is wrong, since the error I get is
 Argument "M-^?M-XM-^?M-`\0^PJFIF\0^A^B\0\0d\0d\0\0M-^?M-l\...
" isn't numeric in pack at ./image-viewport-from-data line 31.

The JFIF is the dead giveaway.  You're passing JPEG-encoded data rather  
than uncompressed image data.

Use Gtk2::Gdk::PixbufLoader if you need to load image data in a  
compressed format (e.g. from a stream or whatever).  Example here:
http://mail.gnome.org/archives/gtk-perl-list/2004-August/msg00017.html

Thanks muppet !  I read that msg, but the concept didn't sink in. But now
it does.  Here is a complete working script for anyone else looking for
an example.

#!/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();

}

__END__



-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]