Data Types

Image


nut.js enables you to search for images on your screen.

To represent image data, nut.js provides the Image datatype.

Image Properties


An Image holds all the data required to perform on-screen search.

That is:

  • the image width
  • the image height
  • a buffer storing raw image data
  • the amount of color channels
  • an id

ColorMode


Additionally, it also stores information about the order of color channels.
By default, nut.js images are stored using the BGR color mode.
This mode was chosen to align with a well known computer vision library.

However, since not every image processing library expects image data in BGR ordering, nut.js images provide methods to convert between BGR and RGB mode.

1const bgrImage = new Image(...);
2
3const rgbImage = bgrImage.toRGB();
1const rgbImage = new Image(..., ColorMode.RGB);
2
3const bgrImage = rgbImage.toBGR();

Image loading


nut.js provides helpers for image loading.

loadImage receives a full path to an image, loads it and returns an Image in BGR color mode, representing this particular file.
The image id will automatically be set to the path it was loaded from.

imageResources


Often times, images are loaded to be used as inputs to Screen#find or similar.
For such use cases, nut.js provides the imageResources helper function to load images relative to a configured resource directory.

Remote Images


Especially when collaborating with others, keeping all image resources on disk can become cumbersome.
Remote image loading allows you to store and manage all your image assets in a central place.

fetchFromUrl allows you to pass in a URL to an image located on a remote host that will be fetched and returned as nut.js Image.

Image saving


Similar to image loading, nut.js provides helpers for image saving as well.

saveImage receives an ImageWriterParameters object that consists of the following:

  • image: The Image to store
  • path: The path where to store the Image

nut.js will take care of possibly required color mode conversions, so you, as a user, do not have to care about it.

Region


When searching for images on your screen, nut.js uses the Region datatype to represent a 2D area on your screen.

A Region holds a left, top, width and height property to represent its size and location, as well as an area() method that calculates the area occupied by a particular Region.

Sample


1const r = new Region(10, 100, 400, 300);
2
3console.log(r);
4
5const regionArea = r.area();

Point


When navigating your screen, nut.js relies on the Point datatype to represent points in 2D space.

A Point is a simple data structure holding an x and y coordinate.

Sample


1const p = new Point(10, 100);
2
3console.log(p);

© 2022