Image Type

Whenever you want to work with images in bot scripts, you need the built-in Image type. Using the Image type, images can be loaded into the script engine, manipulated, and used in methods.

Table of contents

Loading an image

Usually, you want to load an image from an image file provided with your script file. Images can be stored in any common format, but we strongly recommend using .png files (a lossless image file format).

The image file path provided should be relative to the script file. Absolute file paths are supported too, but never use them in scripts you want to share with someone else.

Creating an image

Images can also be created by providing their Size and optionally a Color. The image will be null (Image.isNull(); will return true) if no color has been provided.

Creating images using this method can be useful, e.g. to compare such images to other images taken using the APIs’ takeScreenshot methods.

Image methods


Image.isNull();

A null image has all parameters set to zero and no allocated data.

Image.hasAlphaChannel();

Returns true if the image has a format that respects the alpha channel; otherwise returns false.

Image.getWidth();

Returns the image’s width.

Image.getHeight();

Returns the image’s height.

Image.getSize();

Returns the image’s Size.

Image.load(filepath);

Loads image data from filepath. Most common image file extensions are supported, but .png files are strongly recommended.

Image.save(filepath);

Saves the image to filepath.

Image.fill(color);

Fills the entire image with the given color.

Image.getPixelColor(position);

Returns the color of the pixel at the given position as a Color object.

Image.setPixelColor(position, color);

Alters the color of the pixel at the given position.

Image.copy(sub_area);

Returns a sub-area of the image as a new Image.

Image.mirrored(horizontally, vertically);

Returns a mirror of the image as a new Image object.

Image.grayed();

Returns the image in grayscale format as a new Image object.

Image.createMaskFromColor(color);

Creates and returns a mask for this image based on the given color value. The returned mask will be a new Image. Pixels having the provided color will be black on the mask image, and every other pixel white.

Image.createMaskFromAlpha();

Builds and returns a mask Image from the alpha channel of this image.

Image.isolateColorRange(min_color, max_color, keep_color);

Turns all pixels black that are not in the range of min_color and max_color. Pixels in range keep their color if keep_color is true or become white if false. The method calculates the range to isolate using the HSV color space. Thus, it’s good practice to instantiate min_color and max_color as HSV colors.

Image.countDifferentPixels(other_image);

Returns the number of pixels that differ between two images of the same size.

Image.pixelEqualityTo(other_image);

Calculates the percentage of pixels that have the same color and position in both images. (Internally, this method uses the Image.countDifferentPixels method).