Compare Method

Applies to TestComplete 15.69, last modified on November 13, 2024

Description

The Picture.Compare method compares two images pixel-by-pixel (the first image is represented by the given Picture object, the second is specified by the Picture parameter), and returns True if the images are identical or False otherwise.

To learn more about image comparison, read the How Image Comparison Works topic.

Declaration

PictureObj.Compare(Picture, Transparent, PixelTolerance, Mouse, ColorTolerance, Mask)

PictureObj An expression, variable or parameter that specifies a reference to a Picture object
Picture [in]    Required    A Picture object or an object that has the Picture method which returns a Picture object    
Transparent [in]    Optional    Boolean Default value: False   
PixelTolerance [in]    Optional    Integer Default value: 0   
Mouse [in]    Optional    Boolean Default value: True   
ColorTolerance [in]    Optional    Integer Default value: 0   
Mask [in]    Optional    A Picture object Default value: Empty string   
Result Boolean

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

Picture

The Picture object that represents the image to be compared with the PictureObj image. Alternatively, this can be an object that has the Picture method which returns the desired Picture object.

Transparent

False by default. If it is True, then the top left pixel of the given image will be treated as the “transparent” (background) color in this image. Therefore, any transparent pixels in the first image will be excluded when being compared to the second image.

PixelTolerance

Lets you ignore slight differences between the bitmaps during the search. If the number of different pixels is less than or equal to PixelTolerance, then TestComplete considers the images to be identical. 0 is the default value; it means that no differences between the bitmaps except for those set by the Transparent parameter are ignored.

Note: Using the PixelTolerance parameter with non-zero values may slow down the search process.

Mouse

This parameter is only meaningful if --

  • The Picture parameter specifies an object that has the Picture method that returns a Picture object.

-- and --

  • Picture corresponds to an image that has a mouse pointer on it.

If the Mouse parameter is True, the Compare method includes the mouse pointer in the Picture image. The method will only return True if Picture also contains the mouse pointer image in the same position.

ColorTolerance

Allows ignoring hue differences when comparing bitmaps. The parameter specifies an acceptable color difference under which two pixels of a certain color should be treated as identical. The color difference is represented as an integer value within the range 0…255 that specifies an acceptable difference for each color component (red, green and blue) of the compared pixels. Two pixels are considered identical if the difference between intensities of each of their color components does not exceed the specified value.

When ColorTolerance is 0, which is the default value, compared pixels are considered identical only if they have exactly the same color. When ColorTolerance is 255, pixels of any color are considered identical.

Mask

Specifies which areas of bitmaps should be compared. A mask is another Picture object, whose pixels can be either black or white. If a mask is specified, the routine works in the following way: white pixels on the mask are taken into account during the comparison, whereas black pixels are ignored.

Result Value

True if the compared images are identical and False otherwise.

Remarks

If comparison fails while the images seem to be identical, read the Why Image Comparison Fails and Factors Affecting Image Comparison help topics to determine the cause of the failure.

Example

The following code compares two images, which are represented by the Picture objects.

JavaScript

function Test1()
{
  Pict1 = Utils.Picture;
  Pict2 = Utils.Picture;

  Pict1.LoadFromFile("D:\\Data\\img1.png");
  Pict2.LoadFromFile("D:\\Data\\img2.png");

  // Comparing the images using the Compare method
  if (!Pict1.Compare(Pict2))
  {
    Log.Picture(Pict1);
    Log.Picture(Pict2);
    Log.Error("The compared images are not identical");
  }

  /* Comparing the images using the Difference method
  ResultImage = Pict1.Difference(Pict2);
  if (strictEqual(ResultImage, null))
    Log.Message("The images are identical");
  else
    Log.Picture(ResultImage);*/
}

JScript

function Test1()
{
  Pict1 = Utils.Picture;
  Pict2 = Utils.Picture;

  Pict1.LoadFromFile("D:\\Data\\img1.png");
  Pict2.LoadFromFile("D:\\Data\\img2.png");

  // Comparing the images using the Compare method
  if (!Pict1.Compare(Pict2))
  {
    Log.Picture(Pict1);
    Log.Picture(Pict2);
    Log.Error("The compared images are not identical");
  }

  /* Comparing the images using the Difference method
  ResultImage = Pict1.Difference(Pict2);
  if (ResultImage == null)
    Log.Message("The images are identical");
  else
    Log.Picture(ResultImage);*/
}

Python

def Test1():
  Pict1 = Utils.Picture
  Pict2 = Utils.Picture
  Pict1.LoadFromFile("D:\\Data\\img1.png")
  Pict2.LoadFromFile("D:\\Data\\img2.png")
  # Comparing the images using the Compare method
  if not Pict1.Compare(Pict2):
    Log.Picture(Pict1)
    Log.Picture(Pict2)
    Log.Error("The compared images are not identical")
  # Comparing the images using the Difference method
  # ResultImage = Pict1.Difference(Pict2)
  # if ResultImage == None:
    # Log.Message("The images are identical")
  # else:
    # Log.Picture(ResultImage)

VBScript

Sub Test1
  Set Pict1 = Utils.Picture
  Set Pict2 = Utils.Picture

  Pict1.LoadFromFile("D:\Data\img1.png")
  Pict2.LoadFromFile("D:\Data\img2.png")

  ' Comparing the images using the Compare method
  If Not Pict1.Compare(Pict2) Then
    Log.Picture(Pict1)
    Log.Picture(Pict2)
    Log.Error "The compared images are not identical"
  End If

  ' Comparing the images using the Difference method
   ' Set ResultImage = Pict1.Difference(Pict2)
  ' If ResultImage Is Nothing Then
    ' Log.Message("The images are identical")
  ' Else
    ' Log.Picture(ResultImage)
  ' End If
End Sub

DelphiScript

procedure Test1;
begin
  Pict1 := Utils.Picture;
  Pict2 := Utils.Picture;

  Pict1.LoadFromFile('D:\\Data\\img1.png');
  Pict2.LoadFromFile('D:\\Data\\img2.png');

  // Comparing the images using the Compare method
  if not Pict1.Compare(Pict2) then
  begin
    Log.Picture(Pict1);
    Log.Picture(Pict2);
    Log.Error('The compared images are not identical');
  end;

  { Comparing the images using the Difference method
  ResultImage := Pict1.Difference(Pict2);
  if ResultImage = nil then
    Log.Message('The images are identical')
  else
    Log.Picture(ResultImage);}
end;

C++Script, C#Script

function Test1()
{
  Pict1 = Utils["Picture"];
  Pict2 = Utils["Picture"];

  Pict1["LoadFromFile"]("D:\\Data\\img1.png");
  Pict2["LoadFromFile"]("D:\\Data\\img2.png");

  // Comparing the images using the Compare method
  if (!Pict1["Compare"](Pict2))
  {
    Log["Picture"](Pict1);
    Log["Picture"](Pict2);
    Log["Error"]("The compared images are not identical");
  }

  /* Comparing the images using the Difference method
  ResultImage = Pict1["Difference"](Pict2);
  if (ResultImage == null)
    Log["Message"]("The images are identical");
  else
    Log["Picture"](ResultImage);*/
}

See Also

About Region Checkpoints
How Image Comparison Works
Compare Method
Check Method
Find Method
Difference Method
Pixels Property

Highlight search results