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


I am starting on writing a program that will allow me to edit images files (gif, jpeg, tiff etc.) I already have Perl, Perl/Tk and the Perl API to ImageMagick. I have been trying to find examples of ImageMagick and Tk Perl scripts. I haven't found any. If anybody would know where I could find such examples it would be the monks!

Please let me know if I am on the right track with Perl/Tk/ImageMagick or if I am swimming up stream.


Replies are listed 'Best First'.
Re: Tk and ImageMagick
by lin0 (Curate) on Feb 20, 2007 at 17:49 UTC
Re: Tk and ImageMagick
by jdtoronto (Prior) on Feb 20, 2007 at 17:52 UTC
Re: Tk and ImageMagick
by Anonymous Monk on Feb 20, 2007 at 18:21 UTC
    Thanks for your responses. It does look as though PIL might be what I'd use if I had a choice ... Tk support is a big plus ... but I am sort of stuck with Perl/Tk. I was hoping to use the functions in ImageMagick (crop, annotate text, blur, add noise, scale, rotate, circlur crop). I guess I would have to write the image out to disk everytime a manipuation to it was made ... and then read it back in a display it via Tk's photoimage method. :/ Again thanks for your responses so far!
      You can use Image::Magick and Tk together without any problem. The thing to remember, is what each module does best, and how they interact. Tk is basically just a display engine. The Tk::Photo module has some very crude image manipulation methods, but IM has some great ones. So you use IM to do your manipulations, and Tk to do your input and display. The way to connect the two, is through in-memory files. Tk::Photo does this with the -data option, and IM uses the 'blob'. One other thing to remember, is that the Tk -data option wants it as base64 encoded strings. So you will need to have some subs to convert base64 back and forth. You really have to watch out for Tk leaking memory through left over Photo objects, so you need to reuse Photo objects. Here is a simple example that views all jpg, png, and gif's in a dir, with IM manipulation.
      #!/usr/bin/perl -w use strict; use Tk; use Tk::JPEG; use Tk::PNG; 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 *.png *.gif>; print "@fl\n"; my $main = new MainWindow; 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 }); 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. Cogito ergo sum a bum
Re: Tk and ImageMagick
by Anonymous Monk on Feb 20, 2007 at 17:33 UTC
    Yes you ARE "swimming up stream". Try PIL instead. Your life will be some much easier.