Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Using Perl and the GIMP to compare images

by sailortailorson (Scribe)
on Feb 03, 2006 at 18:17 UTC ( [id://527759]=perlquestion: print w/replies, xml ) Need Help??

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

O holy monks,

First, let me say that I am looking for an excuse to use the GIMP and Perl together. I think I may have a work-oriented excuse to do so now (allowing me to be remunerated for my self-enlightenment), but have not been able to find anything about my specific application in several searches, including Grokking the Gimp. Here is what I want to do:

I have used commercial testing software that allows one to compare current screenshots to an archived sample, typically with some kind of XOR to show differences between the archived sample and the current screenshot.

I need to do just such a thing now to check progress on a web page, where the progress bar is a flash animation. It contains both text and graphic elements telling percentage complete. I cannot scrape the text, so I would like to compare the text and graphics to a screenshot.

I don't need a very specific answer, but a pointer in the general direction, say, to particular CPAN library or repository, or some webpage would be great. I would take it from there and post what I find out.

  • Comment on Using Perl and the GIMP to compare images

Replies are listed 'Best First'.
Re: Using Perl and the GIMP to compare images
by stonecolddevin (Parson) on Feb 04, 2006 at 07:47 UTC
    well start by searching CPAN...i know for positive there is/are a/many GIMP modules out there...
    meh.

      Right.

      I will search CPAN.

Re: Using Perl and the GIMP to compare images
by shotgunefx (Parson) on Feb 04, 2006 at 08:06 UTC
    I'm not sure if this is much help, but it's an idea.

    If you have a known screen size and you know were the element is, possibly, you could just do a dump to a raw / line oriented format and jump to the appropriate row and scan for the progress bar?



    -Lee
    perl digital dash (in progress)

      Lee

      That probably would work.

      My boss said what he used to do was to just do a bit by bit compare.

      However, I am going to piggyback on to the evolutionary trend evinced by the commercial software test packages and try to do an XORish transform between the current page and an older version of it.

      Also, I read somewhere recently that Flash is an open format. Since the element I am looking for is in Flash, I am going to see I can (find what I need in Perl to) read the text from Flash.

      Thanks for your suggestion

        This might be of help. Didn't find it on CPAN the first time around.
        In the Imager distro, Imager::Filters
        Image Difference
        
        You can create a new image that is the difference between 2 other images.
        
          my $diff = $img->difference(other=>$other_img);
        
        For each pixel in $img that is different to the pixel 
        in $other_img, the pixel from $other_img is given, 
        otherwise the pixel is transparent black.
        
        This can be used for debugging image differences 
        ("Where are they different?"), and for optimizing 
        animated GIFs.
        

        -Lee

        perl digital dash (in progress)
Re: Using Perl and the GIMP to compare images
by Anonymous Monk on Feb 06, 2006 at 14:08 UTC
    I have done something very similar under windows, but used ImageMagick instead of GIMP. ImageMagick has code built in for the comparison part. Here are some code snippets to give you some ideas:
    # http://www.imagemagick.com/ use Image::Magick; use Win32::Screenshot; # to capture screenshots directly into image +magick bjects: sub Capture_Screen { @$image = (); $image = CaptureScreen( ); die "FAILED: Capture-Screen\n" unless ( defined $image ); # my ($w, $h) = $image->Get('columns','rows'); print "Captured: $ +w,$h\n"; $image->Set(matte=>'False'); # $image->Write('captured_scr.bmp'); } sub Capture_Area { $image2 = CaptureRect( 10,730,190,11 ); die "FAILED: Capture-Screen\n" unless ( defined $image2 ); } sub Get_Predefined_Images { # reading predefined images from disk into a hash: for my $nm ( qw( )) { $dsk_img{$nm} = Image::Magick->new(magick=>'bmp'); my $bmp_nm = "IMG_" . $nm . '.bmp'; $x = $dsk_img{$nm}->Read($bmp_nm); warn "$x" if "$x"; } } sub Compare_Area { my $wca_img = CaptureRect( $x,$y,35,13 ); die "FAILED: capture-rectangle\n" unless ( defined $wca_img ); # $cmp_img is image to compare against. $xx = $wca_img->Compare( image => $cmp_img ); warn "Status_Img: $xx" if "$xx"; my $result = $wca_img->Get('mean-error'); # $result is 0 or near zero when images match. return(1) if $result == 0; }

      Thanks,

      This is more or less the kind of thing I was looking for (first, to know that it is do-able, and to avoid looking in the wrong places).
      This part will come a little later in the project, but I will start to gather some resources, starting with your snippets now.

      Thank you Anonymous Monk.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://527759]
Approved by kutsu
Front-paged by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-12-09 05:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (53 votes). Check out past polls.