Working with images in nut.js

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.

import {loadImage} from "@nut-tree/nut-js";

const image = await loadImage("/Users/foo/Desktop/img.png");
// Use image ...


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

Re-using the previous example we could configure our resourceDirectory to be /Users/foo/Desktop/. Images loaded via imageResource would then be opened relative to the configured resourceDirectory, so you don't have to specify the full path, but only the path relative to the configured resourceDirectory.

import {imageResource, screen} from "@nut-tree/nut-js";

screen.config.resourceDirectory = "/Users/foo/Desktop/"
const image = await imageResource("img.png");
// Use image ...

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.