Image Object
The global Image
object provides functions for reading, saving and manipulation of image files. Instances of the Image
class can be obtained by loading an image from a file using the Image Image.load(fileName) or by taking screenshots using either Image object.grabScreenshot(object, [parameterMap]) or grabDesktopScreenshot() test API. It can be used as the search region argument to image search and OCR APIs like ScreenRectangle findImage(imageFile, [parameterMap], [searchRegion]), ScreenRectangle waitForImage(imageFile, [parameterMap], [searchRegion]), String getOcrText([parameterMap], [searchRegion]), ScreenRectangle findOcrText(text, [parameterMap], [searchRegion]), ScreenRectangle waitForOcrText(text, [parameterMap], [searchRegion]), SequenceOfObjects findAllOcrText(text, [parameterMap], [searchRegion]), Boolean test.imagePresent(imageFile, [parameterMap], [searchRegion]) and Boolean test.ocrTextPresent(text, [parameterMap], [searchRegion])
Image Image(QImage)
Image Image(QPixmap)
The constructor for the Image
type. It creates an image object out of the AUT-specific QImage and QPixmap objects.
Image Image(width, height, [color])
The constructor for the Image
type. It creates an image with the specified size. The initial color of the image pixels can be specified using the color
argument. The color may be specified as a 32-bit ARGB value or as a set of values for each of the color channels.
var img = new Image(100, 100, [255, 0, 0]); img.save("C:\\red.png");
my $img = Image->new(100, 100, [255, 0, 0]); $img->save("C:\\red.png");
img = Image(100, 100,[255, 0, 0]) img.save("C:\\red.png")
img = Image.new(100, 100, [255, 0, 0]); img.save("C:\\red.png");
set img [image new 100 100 {255 0 0}] invoke $img save "C:\\red.png"
Image Image.load(fileName)
This static function of the Image
object reads an image from the specified file in the testcase directory by default and returns a reference to the instance. In case the file cannot be found or read an exception is thrown.
See also the findFile(where, filename).
img = Image.load("indicator.png") test.compare(img.width, 32) test.compare(img.height, 32)
var img = Image.load("indicator.png"); test.compare(img.width, 32); test.compare(img.height, 32);
my $img = Image->load("indicator.png"); test->compare($img->width, 32); test->compare($img->height, 32);
img = Image::load("indicator.png"); Test::compare(img.width, 32) Test::compare(img.height, 32)
set img [image load "indicator.png"] test compare [property get $img width] 32 test compare [property get $img height] 32
The properties and functions available in the prototype of image instances can be found below.
Number width
This property denotes the width of the image instance in pixels. The value is read-only. When attempting to set a new value an exception is thrown.
Number height
This property denotes the height of the image instance in pixels. The value is read-only. When attempting to set a new value an exception is thrown.
Image.copy()
Image.copy(x, y, width, height)
This function returns a copy of a part of the image. In case the specified geometry does not fit into the image's boundary [(0, 0)...(width-1, height-1)], an exception is thrown. If the coordinate parameters are not specified, the copy of the entire image is returned.
Image.save(fileName)
This function saves the image instance on disk under the specified file name. In case the path is invalid or not writable, an exception is thrown. If the specified path is relative, the file is saved relative to the interpreter's current working directory, which by default, is the location of the currently executing test case.
The format of the saved file will be based on the file extension. The supported formats vary depending on which platform/edition is used, but will usually include BMP, JPG, PNG, PPM, TIF, XBM and XPM.
var img = grabDesktopScreenshot(); img.save("C:\\screenshot1.png");
my $img = grabDesktopScreenshot(); $img->save("C:\\screenshot1.png");
img = grabDesktopScreenshot() img.save("C:\\screenshot1.png")
img = grabDesktopScreenshot() img.save("C:\\screenshot1.png")
set img [grabDesktopScreenshot] invoke $img save "C:\\screenshot1.png"
Number Image.getPixelRGBA(x, y)
This function returns an array that contains colors of the pixel at the specified position on the red, green, blue and alpha color channel (in that order). In case the pixel does not lie within the boundary of the image (from (0, 0) to (width-1, height-1)) an exception is thrown.
pixel = img.getPixelRGBA(0, 0) red = pixel[0]; green = pixel[1]; blue = pixel[2]; alpha = pixel[3];
var pixel = img.getPixelRGBA(0, 0); var red = pixel[0]; var green = pixel[1]; var blue = pixel[2]; var alpha = pixel[3];
my @pixel = $img->getPixelRGBA(0, 0); my red = $pixel[0]; my green = $pixel[1]; my blue = $pixel[2]; my alpha = $pixel[3];
pixel = img.getPixelRGBA(0, 0); red = pixel[0]; green = pixel[1]; blue = pixel[2]; alpha = pixel[3];
set pix [invoke $img getPixelRGBA 0 0] set red [lindex $pix 0] set green [lindex $pix 1] set blue [lindex $pix 2] set alpha [lindex $pix 3]
The alpha value for fully opaque pixels is 255. Fully transparent pixels have an alpha value 0.
Number Image.getPixel(x, y)
This function returns the 32-bit ARGB color value of the pixel at the specified position. In case the pixel does not lie within the boundary of the image (from (0, 0) to (width-1, height-1)) an exception is thrown. The value can directly be compared against expected values (e.g., 0xff00ff00 for pure green).
pixel = img.getPixel(0, 0) test.compare(pixel, 0xff102030)
var pixel = img.getPixel(0, 0); test.compare(pixel, 0xff102030 );
my $pixel = $img->getPixel(0, 0); test::compare($pixel, 0xff102030);
pixel = img.getPixel(0, 0); Test::compare(pixel, 0xff102030);
set pixel [invoke $img getPixel 0 0] test compare $pixel [expr 0xff102030]
Number Image.setPixel(x, y, rgbColor)
Number Image.setPixel(x, y, Array)
Number Image.setPixel(x, y, red, green, blue)
Number Image.setPixel(x, y, red, green, blue, alpha)
This function changes the color of the pixel at the specified position. The color may be specified as a 32-bit ARGB value, as a set of values for each of the color channels: reg, green, blue and optionally alpha, or an array of such values; for example:
img.setPixel(0, 0, 0xffff0000) img.setPixel(0, 0, 255, 0, 0) img.setPixel(0, 0, 255, 0, 0, 255) img.setPixel(0, 0, [255, 0, 0]) img.setPixel(0, 0, [255, 0, 0, 255])
img.setPixel(0, 0, 0xffff0000); img.setPixel(0, 0, 255, 0, 0); img.setPixel(0, 0, 255, 0, 0, 255); img.setPixel(0, 0, [255, 0, 0]); img.setPixel(0, 0, [255, 0, 0, 255]);
$img->setPixel( 0, 0, 0xffff0000); $img->setPixel( 0, 0, 255, 0, 0); $img->setPixel( 0, 0, 255, 0, 0, 255 ); $img->setPixel( 0, 0, [255, 0, 0]); $img->setPixel( 0, 0, [255, 0, 0, 255]);
img.setPixel(0, 0, 0xffff0000) img.setPixel(0, 0, 255, 0, 0) img.setPixel(0, 0, 255, 0, 0, 255) img.setPixel(0, 0, [255, 0, 0]) img.setPixel(0, 0, [255, 0, 0, 255])
invoke $img setPixel 0 0 [expr 0xffff0000] invoke $img setPixel 0 0 255 0 0 invoke $img setPixel 0 0 255 0 0 255 invoke $img setPixel 0 0 {255 0 0} invoke $img setPixel 0 0 {255 0 0 255}
Boolean Image.equals(otherImage)
This function returns true if this image is equal to the other image. In case the argument does not reference an image instance an exception is thrown.
© 2024 The Qt Company Ltd.
Documentation contributions included herein are the copyrights of
their respective owners.
The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation.
Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners.