Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Image

A loaded image

This class is the core API of Web Images.

API Example:

import {Image} from "web-images";
import {basename} from "path";
import * as fs from "fs";

async function load_and_save_fast(path: string): Promise<null> {
    // Assume `path` has a file extension set to “jpeg”.
    const output_path = `./output/${basename(path, "jpeg")}.png`;
    return Image
        .open(path)
        .then((x: Image) => x.thumbnail({
            width: 500,
            height: 500
        }))
        .then((x: Image) => x.save(output_path))
        .then((_) => {
            console.log("done");
            return null;
        });
}

async function load_and_save_quality(path: string): Promise<null> {
    // Assume `path` has a file extension set to “jpeg”.
    const output_path = `./output/${basename(path, "jpeg")}.png`;
    return Image
        .open(path)
        .then((x: Image) => x.resize({
            width: 500,
            height: 500
        }))
        .then((x: Image) => x.save(output_path))
        .then((_) => {
            console.log("done");
            return null;
        });
}

async function load_only(path: string): Promise<Image> {
    return Image
        .open(path)
        .then((x: Image) => x.resize({
            width: 500,
            height: 500,
            resize_exact: false,
            filter_type: "Lanczos3"
        }));
}

Hierarchy

  • Image

Index

Constructors

constructor

  • Treat this as private unless you’re using the lower level sys module. For construction prefer the Image.open and related static methods.

    Parameters

    Returns Image

Properties

Private handle

handle: Image

Methods

adaptive_threshold

  • adaptive_threshold(block_radius: Number): Promise<Image>
  • Applies an adaptive threshold to an image.

    This algorithm compares each pixel's brightness with the average brightness of the pixels in the (2 * block_radius + 1) square block centered on it. If the pixel if at least as bright as the threshold then it will have a value of 255 in the output image, otherwise 0.

    Parameters

    • block_radius: Number

    Returns Promise<Image>

adjust_contrast

  • adjust_contrast(contrast: Number): Promise<Image>
  • Adjust the contrast of this image. contrast is the amount to adjust the contrast by. Negative values decrease the contrast and positive values increase the contrast.

    Parameters

    • contrast: Number

    Returns Promise<Image>

blur

  • blur(sigma: Number): Promise<Image>
  • Performs a Gaussian blur on this image. sigma is a measure of how much to blur by.

    Parameters

    • sigma: Number

    Returns Promise<Image>

box_filter

  • box_filter(x_radius: Number, y_radius: Number): Promise<Image>
  • Convolves an 8bpp grayscale image with a kernel of width (2 * x_radius + 1) and height (2 * y_radius + 1) whose entries are equal and sum to one. i.e. each output pixel is the unweighted mean of a rectangular region surrounding its corresponding input pixel.

    We handle locations where the kernel would extend past the image's boundary by treating the image as if its boundary pixels were repeated indefinitely.

    Parameters

    • x_radius: Number
    • y_radius: Number

    Returns Promise<Image>

brighten

  • brighten(value: Number): Promise<Image>
  • Brighten the pixels of this image. value is the amount to brighten each pixel by. Negative values decrease the brightness and positive values increase it.

    Parameters

    • value: Number

    Returns Promise<Image>

canny

  • canny(low_threshold: Number, high_threshold: Number): Promise<Image>
  • Runs the canny edge detection algorithm.

    Returns a binary image where edge pixels have a value of 255 and non-edge pixels a value of 0.

    Parameters

    • low_threshold: Number

      Low threshold for the hysteresis procedure. Edges with a strength higher than the low threshold will appear in the output image, if there are strong edges nearby.

    • high_threshold: Number

      High threshold for the hysteresis procedure. Edges with a strength higher than the high threshold will always appear as edges in the output image.

    Returns Promise<Image>

color

connected_components

  • connected_components(conn: "Four" | "Eight", background: Number): Promise<GrayImageU32>
  • Returns an image of the same size as the input, where each pixel is labelled by the connected foreground component it belongs to, or 0 if it's in the background. Input pixels are treated as belonging to the background if and only if they are equal to the provided background pixel.

    Panics if the image contains 232 or more pixels. If this limitation causes you problems then open an issue and we can rewrite this function to support larger images.

    Four: A pixel is connected to its N, S, E and W neighbors. Eight: A pixel is connected to all of its neighbors.

    Parameters

    • conn: "Four" | "Eight"
    • background: Number

    Returns Promise<GrayImageU32>

crop

  • crop(cx: Number, cy: Number, width: Number, height: Number): Promise<Image>
  • Return a cut out of this image delimited by the bounding rectangle.

    Parameters

    • cx: Number
    • cy: Number
    • width: Number
    • height: Number

    Returns Promise<Image>

dimensions

distance_transform

  • distance_transform(norm: "L1" | "LInf"): Promise<Image>
  • Returns an image showing the distance of each pixel from a foreground pixel in the original image.

    Parameters

    • norm: "L1" | "LInf"

    Returns Promise<Image>

equalize_histogram

  • equalize_histogram(): Promise<Image>

filter3x3

  • filter3x3(kernel: sys.Kernel3x3): Promise<Image>
  • Filters this image with the specified 3x3 kernel.

    Parameters

    • kernel: sys.Kernel3x3

    Returns Promise<Image>

fliph

  • fliph(): Promise<Image>

flipv

  • flipv(): Promise<Image>

gaussian_blur_f32

  • gaussian_blur_f32(sigma: Number): Promise<Image>
  • Blurs an image using a Gaussian of standard deviation sigma. The kernel used has type f32 and all intermediate calculations are performed at this type.

    Parameters

    • sigma: Number

    Returns Promise<Image>

gaussian_noise

  • gaussian_noise(mean: Number, stddev: Number, seed: Number): Promise<Image>
  • Adds independent additive Gaussian noise to all channels of an image, with the given mean and standard deviation.

    Parameters

    • mean: Number
    • stddev: Number
    • seed: Number

    Returns Promise<Image>

grayscale

  • grayscale(): Promise<Image>
  • Return a grayscale version of this image.

    Returns Promise<Image>

horizontal_filter

  • horizontal_filter(kernel: Array<Number>): Promise<Image>
  • Returns horizontal correlations between an image and a 1d kernel. Pads by continuity. Intermediate calculations are performed at type K.

    Parameters

    • kernel: Array<Number>

    Returns Promise<Image>

huerotate

  • huerotate(value: Number): Promise<Image>
  • Hue rotate the supplied image. value is the degrees to rotate each pixel by. 0 and 360 do nothing, the rest rotates by the given degree value. just like the css webkit filter hue-rotate(180)

    Parameters

    • value: Number

    Returns Promise<Image>

invert

  • invert(): Promise<Image>

map_luma

  • map_luma(f: function): Promise<Image>
  • Transforms the input image.

    Parameters

    • f: function
        • (x: number, y: number, px: number): number
        • Parameters

          • x: number
          • y: number
          • px: number

          Returns number

    Returns Promise<Image>

map_rgba

  • map_rgba(f: function): Promise<Image>
  • Transforms the input image.

    Parameters

    • f: function
        • (x: number, y: number, px: Array<number>): Array<number>
        • Parameters

          • x: number
          • y: number
          • px: Array<number>

          Returns Array<number>

    Returns Promise<Image>

match_histogram

  • Adjusts contrast of an 8bpp grayscale image so that its histogram is as close as possible to that of the target image.

    Parameters

    Returns Promise<Image>

median_filter

  • median_filter(x_radius: Number, y_radius: Number): Promise<Image>
  • Applies a median filter of given dimensions to an image. Each output pixel is the median of the pixels in a (2 * x_radius + 1) * (2 * y_radius + 1) kernel of pixels in the input image.

    Pads by continuity. Performs O(max(x_radius, y_radius)) operations per pixel.

    Parameters

    • x_radius: Number
    • y_radius: Number

    Returns Promise<Image>

morph_close

  • morph_close(norm: "L1" | "LInf", k: Number): Promise<Image>
  • Dilation followed by erosion.

    See the erode and dilate documentation for definitions of dilation and erosion.

    Parameters

    • norm: "L1" | "LInf"
    • k: Number

    Returns Promise<Image>

morph_dilate

  • morph_dilate(norm: "L1" | "LInf", k: Number): Promise<Image>
  • Sets all pixels within distance k of a foreground pixel to white.

    A pixel is treated as belonging to the foreground if it has non-zero intensity.

    Parameters

    • norm: "L1" | "LInf"
    • k: Number

    Returns Promise<Image>

morph_erode

  • morph_erode(norm: "L1" | "LInf", k: Number): Promise<Image>
  • Sets all pixels within distance k of a background pixel to black.

    A pixel is treated as belonging to the foreground if it has non-zero intensity.

    Parameters

    • norm: "L1" | "LInf"
    • k: Number

    Returns Promise<Image>

morph_open

  • morph_open(norm: "L1" | "LInf", k: Number): Promise<Image>
  • Erosion followed by dilation.

    See the erode and dilate documentation for definitions of dilation and erosion.

    Parameters

    • norm: "L1" | "LInf"
    • k: Number

    Returns Promise<Image>

otsu_level

  • otsu_level(): Promise<Image>

reduce_luma

  • reduce_luma<T>(initial_value: T, f: function): Promise<T>
  • Sometimes called “fold”, useful for e.g. summing specific pixel values.

    Type parameters

    • T

    Parameters

    • initial_value: T
    • f: function
        • (accumulator: T, x: number, y: number, px: number): T
        • Parameters

          • accumulator: T
          • x: number
          • y: number
          • px: number

          Returns T

    Returns Promise<T>

reduce_rgba

  • reduce_rgba<T>(initial_value: T, f: function): Promise<T>
  • Sometimes called “fold”, useful for e.g. summing specific pixel values.

    Type parameters

    • T

    Parameters

    • initial_value: T
    • f: function
        • (accumulator: T, x: number, y: number, px: Array<number>): T
        • Parameters

          • accumulator: T
          • x: number
          • y: number
          • px: Array<number>

          Returns T

    Returns Promise<T>

resize

  • Resize this image using the specified filter algorithm. Returns a new image.

    Parameters

    Returns Promise<Image>

rotate180

  • rotate180(): Promise<Image>
  • Rotate this image 180 degrees clockwise.

    Returns Promise<Image>

rotate270

  • rotate270(): Promise<Image>
  • Rotate this image 270 degrees clockwise.

    Returns Promise<Image>

rotate90

  • rotate90(): Promise<Image>
  • Rotate this image 90 degrees clockwise.

    Returns Promise<Image>

salt_and_pepper_noise

  • salt_and_pepper_noise(rate: Number, seed: Number): Promise<Image>
  • Converts pixels to black or white at the given rate (between 0.0 and 1.0). Black and white occur with equal probability.

    Parameters

    • rate: Number
    • seed: Number

    Returns Promise<Image>

save

  • save(path: String): Promise<null>
  • Saves the supplied buffer to a file at the path specified. The image format is derived from the file extension.

    Parameters

    • path: String

    Returns Promise<null>

save_with_format

  • save_with_format(path: String, format: data.ImageFormat): Promise<null>
  • Saves the supplied buffer to a file at the path specified in the specified format.

    Parameters

    • path: String
    • format: data.ImageFormat

    Returns Promise<null>

separable_filter

  • separable_filter(h_kernel: Array<Number>, v_kernel: Array<Number>): Promise<Image>
  • Returns 2d correlation of view with the outer product of the 1d kernels h_kernel and v_kernel.

    Parameters

    • h_kernel: Array<Number>
    • v_kernel: Array<Number>

    Returns Promise<Image>

separable_filter_equal

  • separable_filter_equal(kernel: Array<Number>): Promise<Image>
  • Returns 2d correlation of an image with the outer product of the 1d kernel filter with itself.

    Parameters

    • kernel: Array<Number>

    Returns Promise<Image>

sharpen3x3

  • sharpen3x3(): Promise<Image>
  • Sharpens a grayscale image (to converts to grayscale) by applying a 3x3 approximation to the Laplacian.

    Returns Promise<Image>

sharpen_gaussian

  • sharpen_gaussian(sigma: Number, amount: Number): Promise<Image>
  • Sharpens a grayscale image using a Gaussian as a low-pass filter.

    Parameters

    • sigma: Number

      is the standard deviation of the Gaussian filter used

    • amount: Number

      controls the level of sharpening. output = input + amount * edges.

    Returns Promise<Image>

shrink_width

  • shrink_width(target_width: Number): Promise<Image>

stretch_contrast

  • stretch_contrast(lower: Number, upper: Number): Promise<Image>
  • Linearly stretches the contrast in an image, sending lower to 0 and upper to 2558. Is it common to choose upper and lower values using image percentiles - see percentile.

    Parameters

    • lower: Number
    • upper: Number

    Returns Promise<Image>

threshold

  • threshold(thresh: Number): Promise<Image>
  • Returns a binarized image from an input 8bpp grayscale image obtained by applying the given threshold. Pixels with intensity equal to the threshold are assigned to the background.

    Parameters

    • thresh: Number

    Returns Promise<Image>

thumbnail

  • Scale this image down to fit within a specific size. Returns a new image. This method uses a fast integer algorithm where each source pixel contributes to exactly one target pixel. May give aliasing artifacts if new size is close to old size.

    Parameters

    Returns Promise<Image>

unsharpen

  • unsharpen(sigma: Number, threshold: Number): Promise<Image>
  • Performs an unsharpen mask on this image. sigma is the amount to blur the image by. threshold is a control of how much to sharpen

    Parameters

    • sigma: Number
    • threshold: Number

    Returns Promise<Image>

vertical_filter

  • vertical_filter(kernel: Array<Number>): Promise<Image>
  • Returns horizontal correlations between an image and a 1d kernel. Pads by continuity.

    Parameters

    • kernel: Array<Number>

    Returns Promise<Image>

Static create

  • create(width: Number, height: Number, pixel_type: "rgba" | "rgb" | "luma"): Promise<Image>
  • Parameters

    • width: Number
    • height: Number
    • pixel_type: "rgba" | "rgb" | "luma"

    Returns Promise<Image>

Static open

  • open(path: string): Promise<Image>
  • Open the image located at the path specified. The image's format is determined from the path's file extension.

    let image: Promise<Image> = Image
         .open("test.jpeg")
         .then(x => {
             console.log("loaded image");
             return x;
         });

    Parameters

    • path: string

    Returns Promise<Image>

Static open_with_format

  • open_with_format(path: string, format: data.ImageFormat): Promise<Image>
  • let image: Promise<Image> = Image
         .open("test.jpeg")
         .then(x => {
             console.log("loaded image");
             return x;
         });

    Parameters

    • path: string
    • format: data.ImageFormat

    Returns Promise<Image>

Generated using TypeDoc