Professional Web Applications Themes

Image::Magick and Tk::Photo - PERL Modules

I am trying to read in a bunch of images and manipulate them using Image::Magick, then display them using Tk::Photo. I would like to do this without creating a temp file for each image, since there can be quite a few of them. However, I can't make Tk::Photo understand the data returned by Image::Magick's ImageToBlob() function. Here is the way I had hoped it would work: #!perl use strict; use warnings; use Tk; use Image::Magick; my $image = Image::Magick->new(); $image->read('c:\Doents and Settings\ericm\My Doents\My Pictures\earthris.gif'); # Do various Image::Magick manipulations here... # ... my $blob = $image->ImageToBlob(); # Set up Tk ...

  1. #1

    Default Image::Magick and Tk::Photo


    I am trying to read in a bunch of images and manipulate them using
    Image::Magick, then display them using Tk::Photo. I would like to do this
    without creating a temp file for each image, since there can be quite a few
    of them.

    However, I can't make Tk::Photo understand the data returned by
    Image::Magick's ImageToBlob() function. Here is the way I had hoped it would
    work:

    #!perl

    use strict;
    use warnings;

    use Tk;
    use Image::Magick;

    my $image = Image::Magick->new();
    $image->read('c:\Doents and Settings\ericm\My Doents\My
    Pictures\earthris.gif');

    # Do various Image::Magick manipulations here...
    # ...

    my $blob = $image->ImageToBlob();

    # Set up Tk windows
    my $main = MainWindow->new();
    my $photo = $main->Photo('img', -format=>'GIF', -data=>$blob );
    $main->Label('-image' => 'img', -height=>200, -width=>200)->pack;

    MainLoop;

    ###################################

    This code generates the error "couldn't recognize image data at
    C:/Perl/site/lib/Tk/Image.pm line 21." in the call to $main->Photo.
    If I create a temp file with $image->write() and read it in using the
    $main->Photo(-file=>'...') syntax, it works fine.

    Any suggestions?

    Thanks in advance.

    -Eric


    Eric McDaniel Guest

  2. #2

    Default Re: Image::Magick and Tk::Photo


    "Eric McDaniel" <ericmcdanielhome.com> wrote in message
    news:DS54b.233206$It4.111413rwcrnsc51.ops.asp.att .net...
    >
    > I am trying to read in a bunch of images and manipulate them using
    > Image::Magick, then display them using Tk::Photo. I would like to do this
    > without creating a temp file for each image, since there can be quite a few
    > of them.
    >
    > However, I can't make Tk::Photo understand the data returned by
    > Image::Magick's ImageToBlob() function. Here is the way I had hoped it would
    > work:
    >
    > #!perl
    >
    > use strict;
    > use warnings;
    >
    > use Tk;
    > use Image::Magick;
    #-------------------------#
    use Mime::Base64;
    #-------------------------#
    >
    > my $image = Image::Magick->new();
    > $image->read('c:\Doents and Settings\ericm\My Doents\My
    > Pictures\earthris.gif');
    >
    > # Do various Image::Magick manipulations here...
    > # ...
    >
    > my $blob = $image->ImageToBlob();
    >
    > # Set up Tk windows
    > my $main = MainWindow->new();
    > my $photo = $main->Photo('img', -format=>'GIF', -data=>$blob );
    #-------------------------#
    my $photo = $main->Photo('img', -format=>'GIF', -data=>encode_base64($blob) );
    #-------------------------#


    #######################
    > $main->Label('-image' => 'img', -height=>200, -width=>200)->pack;
    >
    > MainLoop;
    >
    > ###################################
    >
    > This code generates the error "couldn't recognize image data at
    > C:/Perl/site/lib/Tk/Image.pm line 21." in the call to $main->Photo.
    > If I create a temp file with $image->write() and read it in using the
    > $main->Photo(-file=>'...') syntax, it works fine.
    >
    > Any suggestions?
    You have to convert the data to base64 format for Tk to recognize it.

    Jack


    Jack D. Guest

  3. #3

    Default Re: Image::Magick and Tk::Photo


    "Jack D." <goodcall__1hotmail.com> writes:
    > "Eric McDaniel" <ericmcdanielhome.com> wrote:
    > > I am trying to read in a bunch of images and manipulate them using
    > > Image::Magick, then display them using Tk::Photo. I would like to do this
    > > without creating a temp file for each image, since there can be quite a few
    > > of them.
    > >
    > > However, I can't make Tk::Photo understand the data returned by
    > > Image::Magick's ImageToBlob() function.
    > use Mime::Base64;
    [...]
    > my $photo = $main->Photo('img', -format=>'GIF', -data=>encode_base64($blob) );
    [...]
    > You have to convert the data to base64 format for Tk to recognize it.
    Ah man, thanks much for the answer (not to mention the timely question
    from Eric). I was just struggling with that issue myself. The
    doentation for Tk::Photo leaves much to be desired.

    Anyway, now I've got a "baby xv" script working, that re-scales large
    images to fit the screen (something noticeably lacking in "ee" and
    it's competitors in the free/open software world). It's a little
    slow as written, but it least it works:

    #!/usr/bin/perl -w
    # tk_show_jpeg - [email]doomkzsu.stanford.edu[/email]
    # Fri Sep 19 01:10:21 2003

    # Simple image viewer, using Perl/Tk and ImageMagick
    # This is a baby "xv": an image viewer that re-scales to fit the screen.

    use strict;

    use Tk;
    use Tk::JPEG;
    use Image::Magick;
    use MIME::Base64;

    my ($image_file, $mw, $screen_width, $screen_height);
    my ($image_mag, $image_tk, $r, $blob);
    my ($tempfile);
    my ($img_height, $img_width, $new_height, $new_width, $geom);

    $image_file = shift;

    $mw = MainWindow->new;
    $mw->title($image_file);

    # Get screen dimensions
    $screen_width = $mw ->screenwidth(); # 1024
    $screen_height = $mw ->screenheight(); # 768
    $screen_height -= 30; # allow slack for title bars, etc.

    # Bind q key to quit
    $mw->bind("<Key-q>", [sub{$mw->destroy}, Ev('K')]);

    # Read in file into imagemagick

    $image_mag = Image::Magick->new;
    $r = $image_mag->Read("$image_file");
    warn "$r" if "$r";

    # Scale down $image_mag if necessary

    ($img_height, $img_width) = $image_mag->Get('height', 'width');

    $new_height = $img_height; # "New" sizes default to the old
    $new_width = $img_width;

    if ($img_height > $screen_height) {
    $new_height = $screen_height;
    }
    if ($img_width > $screen_width) {
    $new_width = $screen_width;
    }
    $geom = $new_width . 'x' . $new_height;

    $r = $image_mag->Scale(geometry=>$geom);
    warn "$r" if "$r";

    # Transfer image from ImageMagick to Perl/Tk

    $blob = ( $image_mag->ImageToBlob(magick=>'jpg') )[0];
    $image_tk = $mw->Photo('img', -format=>'jpeg', -data=>encode_base64($blob) );

    # (I *gather* that that first argument 'img' is an "imageName"
    # mentioned in some places in the docs, but not explained well.)

    $mw->Label(-image => $image_tk)->pack(-side=>'left');
    $mw->update;
    MainLoop;
    Joseph Brenner Guest

Similar Threads

  1. Image::Magick newbie
    By Ananth Chellappa in forum PERL Modules
    Replies: 0
    Last Post: September 6th, 02:19 AM
  2. Help: Image::Magick::Thumbnail
    By cb in forum PERL Modules
    Replies: 4
    Last Post: February 16th, 10:02 PM
  3. Image::Magick and padding of image
    By Lars Haugseth in forum PERL Modules
    Replies: 0
    Last Post: February 5th, 07:17 PM
  4. Image Magick
    By Kae Verens in forum PHP Development
    Replies: 4
    Last Post: January 9th, 07:06 PM
  5. [PHP] Image Magick
    By David T-G in forum PHP Development
    Replies: 0
    Last Post: August 26th, 10:42 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139