Options
All
  • Public
  • Public/Protected
  • All
Menu

Module image

image module provides image reading, grayscale, cropping, scaling, template matching, etc. functions. The main APIs are functions of this page and the Image class.

The functions of screen capture have been moved to the media_projection module. Also, the functions of opencv are directly available in the @autojs/opencv module.

Index

Type aliases

ImageFormat

ImageFormat: "jpg" | "jpeg" | "png" | "webp"

Region

Region: Rect | number[]

Variables

Const MAX_LEVEL_AUTO

MAX_LEVEL_AUTO: -1 = -1

Functions

decodeImage

  • decodeImage(str: string, encoding?: BufferEncoding): Promise<Image>
  • Decode the string to image object, and return the image object asynchronously. If the string cannot be decoded, throw an exception.

    Parameters

    • str: string

      the string, based on encoding parameter, default is base64

    • encoding: BufferEncoding = 'base64'

      the encoding format, default is base64

    Returns Promise<Image>

decodeImageFromBuffer

  • decodeImageFromBuffer(buffer: Buffer): Promise<Image>
  • Decode the image from Buffer, and return the decoded image object asynchronously. If the decoding failed, throw an exception.

    Parameters

    • buffer: Buffer

    Returns Promise<Image>

decodeImageFromBufferSync

  • decodeImageFromBufferSync(buffer: Buffer): Image
  • Decode the image from Buffer, and return the decoded image object synchronously. If the decoding failed, throw an exception.

    Parameters

    • buffer: Buffer

    Returns Image

decodeImageSync

  • decodeImageSync(str: string, encoding?: BufferEncoding): Image
  • Decode the string to image object, and return the image object synchronously. If the string cannot be decoded, throw an exception.

    Parameters

    • str: string

      the string, based on encoding parameter, default is base64

    • encoding: BufferEncoding = 'base64'

      the encoding format, default is base64

    Returns Image

detectsColor

detectsMultiColors

encodeImage

  • encodeImage(img: Image, encoding?: BufferEncoding, format?: ImageFormat, quality?: number): Promise<string>
  • Encode the image to string, and return the encoded string asynchronously.

    Parameters

    • img: Image
    • encoding: BufferEncoding = 'base64'

      the encoding format, default is base64

    • format: ImageFormat = 'png'

      the image compress format, default is png. The available values are: png, jpg, jpeg, webp.

    • quality: number = 100

      the image quality, range 0-100. The default value is 100.

    Returns Promise<string>

encodeImageSync

  • encodeImageSync(img: Image, encoding?: BufferEncoding, format?: ImageFormat, quality?: number): string
  • Encode the image to string, and return the encoded string synchronously.

    Parameters

    • img: Image
    • encoding: BufferEncoding = 'base64'

      the encoding format, default is base64

    • format: ImageFormat = 'png'

      the image compress format, default is png. The available values are: png, jpg, jpeg, webp.

    • quality: number = 100

      the image quality, range 0-100. The default value is 100.

    Returns string

encodeImageToBuffer

  • encodeImageToBuffer(img: Image, format?: ImageFormat, quality?: number): Promise<Buffer>
  • Encode the image to Buffer, and return the encoded Buffer object asynchronously.

    Parameters

    • img: Image
    • format: ImageFormat = 'png'

      the image compress format, default is png. The available values are: png, jpg, jpeg, webp.

    • quality: number = 100

      the image quality, range 0-100. The default value is 100.

    Returns Promise<Buffer>

encodeImageToBufferSync

  • encodeImageToBufferSync(img: Image, format?: ImageFormat, quality?: number): Buffer
  • Encode the image to Buffer, and return the encoded Buffer object synchronously.

    Parameters

    • img: Image
    • format: ImageFormat = 'png'

      the image compress format, default is png. The available values are: png, jpg, jpeg, webp.

    • quality: number = 100

      the image quality, range 0-100. The default value is 100.

    Returns Buffer

findColor

findColorSync

findImage

findImageInRegion

  • findImageInRegion(src: Image, template: Image, x: number, y: number, width?: number, height?: number, threshold?: number): Promise<Point2 | null>
  • Parameters

    • src: Image
    • template: Image
    • x: number
    • y: number
    • Optional width: number
    • Optional height: number
    • Optional threshold: number

    Returns Promise<Point2 | null>

findImageInRegionSync

  • findImageInRegionSync(src: Image, template: Image, x: number, y: number, width?: number, height?: number, threshold?: number): Point2 | null
  • Parameters

    • src: Image
    • template: Image
    • x: number
    • y: number
    • Optional width: number
    • Optional height: number
    • Optional threshold: number

    Returns Point2 | null

findImageSync

findMultiColors

findMultiColorsSync

loadImage

  • loadImage(url: string): Promise<Image>
  • Load the file at the specified url, and return the image object asynchronously. If the url cannot be accessed or the image cannot be decoded, then throw an exception.

    Parameters

    • url: string

      the url address, must start with https or http

    Returns Promise<Image>

matchFeatures

matchTemplate

matchTemplateSync

readImage

  • readImage(file: string): Promise<Image>
  • Read the file at the specified path, and return the image object asynchronously. If the file does not exist or cannot be decoded, then throw an exception.

    Parameters

    • file: string

      the file path, support relative path

    Returns Promise<Image>

readImageSync

  • readImageSync(file: string): Image
  • Read the file at the specified path, and return the image object synchronously. If the file does not exist or cannot be decoded, then throw an exception.

    Parameters

    • file: string

      the file path, support relative path

    Returns Image

writeImage

  • writeImage(img: Image, file: string, quality?: number): Promise<void>
  • Write the image asynchronously to the specified path.

    example
    "nodejs";
    
    const {loadImage, writeImage} = require("image");
    async function main() {
       const img = await loadImage("https://picsum.photos/200/300");
       await writeImage(img, "./output-200x300.png");  
    }
    main();
    

    Parameters

    • img: Image
    • file: string

      the file path, support relative path. The file path must end with one of the following extensions: .jpg, .jpeg, .png, .webp.

    • quality: number = 100

      the image quality, range 0-100. The default value is 100.

    Returns Promise<void>

writeImageSync

  • writeImageSync(img: Image, file: string, quality?: number): void
  • Write the image synchronously to the specified path.

    Parameters

    • img: Image
    • file: string

      the file path, support relative path. The file path must end with one of the following extensions: .jpg, .jpeg, .png, .webp.

    • quality: number = 100

      the image quality, range 0-100. The default value is 100.

    Returns void