Forum Discussion

mrezahoseini's avatar
mrezahoseini
Contributor
9 years ago

How to compare two images

Hello

 

I've got two images, the location of one of them is on my hard disk and other one has been mapped on my the name mapping of my TC, how can I compare these two images with each other with scripting ?

 

I looked for the solution in several forums, without any result.

 

please help me ASAP

 

Thanks

  • osaid_ahmad's avatar
    osaid_ahmad
    9 years ago

    You can try Picture.Compare (https://support.smartbear.com/viewarticle/72669/) as suggested by somebody else also.

     

    Cosider the following VB Script taken from the above link: 
     

      Set imageOnDisk = Utils.Picture 'to obtain empty picture object
      Set actualPicOnScreen = (Aliases.page.obj).Picture    'capture yourMappedObj's image
     

     imageOnDisk.LoadFromFile("D:\Data\img1.png")
      
      ' Comparing the images using the Compare method
      If Not imageOnDisk.Compare(actualPicOnScreen ) Then
        Log.Picture imageOnDisk, "Expected Image"
        Log.Picture actualPicOnScreen, "Actual Image"
        Log.Error "The compared images are not identical"
      End If

12 Replies

    • chik's avatar
      chik
      Occasional Contributor

      Why do you want to compare 2 images ?

      Ï have a bad experience with comparing image because if they differ a few pixels you are screwed.

      • baxatob's avatar
        baxatob
        Community Hero

        You can play with "Pixel tolerance" parameter to make image recognition more flexible.

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    chik's question is a good one. Comparing images as part of an automated test, while useful in a limited fashion, is something that should be evaluated. If you are checking if a label has the correct text or if the color of a button is correct or such things, it's probably better to do object comparisons.  Check out the support article here for how to use that function.

  • chik's avatar
    chik
    Occasional Contributor
    I always avoid comparing images until you have a very good reason to do that. I still haven't heard from mrezahozeini what is actually his/her problem.
    • baxatob's avatar
      baxatob
      Community Hero

      Sometimes image comparison is very useful feature. In my current project we have an application (iOS), where user interacts with embedded Google Map. While he selects different items from the menu, different locations are presented on the map. I have a test to validate, that map location was correctly changed for each menu item. I have a template-image for each location - actually it's a small piece of map. Test contains a checkpoint to find a region on the map that is similar to the template. Honestly it is not a pure image comparison however the same algorithm works in this case. Pixel tolerance = 75% - I found this value by experimental way and it provides an excellent reliability and speed of the test.

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        Oh, certainly, there's a use for it... I've used it before myself. But it's dependant upon what you hope to achieve. In your case, you are actually comparing an image to an image... for application components like buttons, labels, etc, property comparisons are usually the better bet.