Options
All
  • Public
  • Public/Protected
  • All
Menu

Module imageutils

Defines the ImageUtils object, which helps you work with images. Include this library by adding useLibrary("imageutils") to your script.

Example of use:

useLibrary( "imageutils" );
let src = ImageUtils.get("icons/application/256.png");
let tinted = ImageUtils.tint(src, 0.25, 1.8, 1.4);
ImageUtils.view(tinted);

Index

Type aliases

Variables

Type aliases

BufferedImage

The BufferedImage Java class.

Variables

Const ImageUtils

ImageUtils: object

An object that defines a number of image-related utility functions.

Type declaration

  • FORMAT_BMP: "bmp"

    A format constant indicating the BMP (bmp) image format.

  • FORMAT_GIF: "gif"

    A format constant indicating the GIF89a (gif) image format.

  • FORMAT_JPEG: "jpg"

    A format constant indicating the JPEG (jpg) image format.

  • FORMAT_JPEG2000: "jp2"

    A format constant indicating the JPEG2000 (jp2) image format.

  • FORMAT_PNG: "png"

    A format constant indicating the PNG (png) image format.

  • STITCH_HORIZONTAL: 1

    Indicates that images should be stitched from left to right.

  • STITCH_VERTICAL: 2

    Indicates that images should be stitched from top to bottom.

  • copy: function
    • Returns a new copy of the specified image. If you are going to draw on an image that you obtained from resources, it is important to work with a copy so that you don't alter the shared version stored in the image cache.

      Parameters

      Returns BufferedImage

      a new copy of the source image

  • create: function
    • create(width: number, height: number, transparent?: boolean): BufferedImage
    • Returns a new image with the specified dimensions. If transparent is true, the image will have an alpha channel and pixels can be transparent or translucent. Otherwise, all pixels in the image are opaque.

      Parameters

      • width: number

        the image width, in pixels; must be greater than zero

      • height: number

        the image height, in pixels; must be greater than zero

      • Optional transparent: boolean

        if true, the image includes an alpha channel (default is false)

      Returns BufferedImage

      an image with the requested parameters

  • createForResolution: function
    • createForResolution(pixelsPerInch: number, widthInPoints: number, heightInPoints: number, transparent: boolean): BufferedImage
    • Returns a new image with pixel dimensions calculated from a measurement in points and a specified resolution in pixels per inch. One point is defined to be 1/72 of an inch. Thus, at a resolution of 300 ppi, a width of 36 points would translate to a pixel width of 150 pixels. If transparent is true, the image will have an alpha channel and pixels can be transparent or translucent. Otherwise, all pixels in the image are opaque. The calculated image size must be at least one pixel wide and high.

      Parameters

      • pixelsPerInch: number

        the image resolution, in pixels per inch

      • widthInPoints: number

        the image width, in points

      • heightInPoints: number

        the image height, in points

      • transparent: boolean

        if true, the image includes an alpha channel (default is false)

      Returns BufferedImage

      an image with the requested parameters

  • createIcon: function
    • Returns a new icon that displays an existing image. The icon will be resized to fit within size×size pixels.

      Parameters

      • image: BufferedImage

        the image used to create the icon, which is not changed

      • Optional size: number

        the target size for the icon (default is 16)

      Returns JavaObject<"javax.swing.ImageIcon">

      an icon that displays the image at the requested size

  • crop: function
    • Creates an image by cropping a source image. The resulting image will consist of the subimage of the source image that starts at pixel (x, y) in the source image and is width by height pixels in size. Either width or height (or both) may undefined or less than 1, in which case the crop will extend to the right (or bottom) edge of the image, respectively.

      Parameters

      • image: BufferedImage

        the source image, which is not changed

      • x: number

        the x-coordinate of the upper-left corner of the region to retain in the destination

      • y: number

        the y-coordinate of the upper-left corner of the region to retain in the destination

      • Optional width: number

        the width of the region to retain in the destination, or -1 to use the image edge

      • Optional height: number

        the height of the region to retain in the destination, or -1 to use the image edge

      Returns BufferedImage

      a new image containing the cropped region of the source image

  • desaturate: function
    • Returns a copy of the image converted to greyscale.

      Parameters

      Returns BufferedImage

      the desaturated image

  • fit: function
    • Ensures that an image is as large as it can be and still fit within the given dimensions without changing the aspect ratio. If the image already just fits it is returned unchanged. Otherwise a new, resized image is returned.

      Parameters

      • image: BufferedImage

        the image to fit within the space, which is not changed

      • newMaxWidth: number

        the maximum width of the image

      • newMaxHeight: number

        the maximum height of the image

      • Optional fast: boolean

        an optional hint used when the image needs to be resized; if true, lower quality but faster resampling is performed (default is false, use higher quality)

      Returns BufferedImage

      the original image, or a resized copy sized to just fit within the specified dimensions

  • get: function
    • get(resourcePath: string, cacheResult?: boolean, quietly?: boolean): BufferedImage | null
    • Returns an image loaded from an image file stored in the plug-in resources folder. If the image cannot be loaded (for example, if no image file exists at the given resource path), returns null.

      see

      Image resources

      Parameters

      • resourcePath: string

        a path string relative to the resources folder

      • Optional cacheResult: boolean

        if true, the image may be cached to speed future requests for the same resource (default is true)

      • Optional quietly: boolean

        if true, no error is displayed if loading the image fails (default is false, display an error message)

      Returns BufferedImage | null

      the image, or null

  • getIcon: function
    • Returns an icon loaded from an image file stored in the plug-in resources folder. If the image cannot be loaded (for example, if no image file exists at the given resource path), returns null.

      Parameters

      • resourcePath: string

        a path string relative to the resources folder

      • Optional unthemed: boolean

        if true, then the current theme will not be allowed to modify the icon image to fit the theme (default is false, icon can be themed)

      Returns JavaObject<"javax.swing.ImageIcon"> | null

      the icon, or null

  • invert: function
    • Returns a copy of the image with all of the pixels inverted. The result is similar to a photographic negative.

      Parameters

      Returns BufferedImage

      the inverted image

  • mirror: function
    • Returns a new image that mirrors and/or flips the the source image.

      Parameters

      • image: BufferedImage

        the source image, which is not changed

      • Optional flipHoriz: boolean

        if true, mirrors the image horizontally (default is true)

      • Optional flipVert: boolean

        if true, mirrors the image vertically (default is false)

      Returns BufferedImage

      the rotated image

  • pad: function
    • Returns a padded copy of the image that adds margins of the given sizes to the outside of the image. A margin may be negative, in which case rows or columns will be removed from that edge.

      Parameters

      • image: BufferedImage

        the source image, which is not changed

      • top: number

        the number of pixels to add to the top edge

      • left: number

        the number of pixels to add to the left edge

      • bottom: number

        the number of pixels to add to the bottom edge

      • right: number

        the number of pixels to add to the right edge

      Returns BufferedImage

      a padded copy of the image

    • Returns a padded copy of the image that adds a margin on each side of the image. The margin may be negative, in which case rows or columns will be removed.

      Parameters

      • image: BufferedImage

        the source image, which is not changed

      • top: number
      • left: number
      • bottom: number
      • right: number

      Returns BufferedImage

      a padded copy of the image

  • read: function
    • Returns an image decoded from an image file stored on the user's device.

      throws

      if reading the image fails

      Parameters

      Returns BufferedImage

      the decoded image

  • resize: function
    • Creates a copy of an image that is resampled to a new size.

      Parameters

      • image: BufferedImage

        the image to create a resized copy of

      • newWidth: number

        the width of the new image, in pixels (must be positive)

      • newHeight: number

        the height of the new image, in pixels (must be positive)

      • Optional fast: boolean

        an optional hint; if true, lower quality but faster resampling is performed (default is false, use higher quality)

      Returns BufferedImage

      the resized copy of the source image

  • save: function
    • Prompts the user to choose a file, then saves the specified image to the selected file. A default file may be suggested as the starting point for file selecton; if none is selected then a default location is selected based on the last-saved file.

      Parameters

      Returns JavaObject<"java.io.File"> | null

      the file that the user selected, or null if the save was cancelled

  • stitch: function
    • Returns a new image that combines two source images by "stitching" them together along an edge. If stitchEdge is ImageUtils.STITCH_HORIZONTAL, then the left edge of image2 will be stitched to the right edge of image1. If stitchEdge is ImageUtils.STITCH_VERTICAL, then the top edge of image2 will be stitched to the bottom edge of image1. If either source image has transparency, then the returned image will also; otherwise it is opaque.

      Parameters

      • image1: BufferedImage

        the first image to be stitched, which is not changed

      • image2: BufferedImage

        the second image to be stitched, which is not changed

      • stitchEdge: 1 | 2

        the edge to joint the images along, STITCH_HORIZONTAL or STITCH_VERTICAL

      Returns BufferedImage

      a new image combining the source images at their edges

  • tint: function
    • Returns a tinted copy of an image. The value of h is a relative number of degrees. The hue of each pixel in the source image will be shifted by h × 360 degrees around the HSB colour wheel: a value of 0 (or 1) leaves the hue unchanged, while a value of 0.5 shifts the hue by 180 degrees to its complementary colour. The value of s is a saturation factor; each pixel's saturation will be multiplied by this value. A value of 0 will convert the image to greyscale. A value of 1 will leave the saturation unchanged. The value of b is a brightness factor. Each pixel's brightness will be multiplied by this value. A value of 0 will set each pixel's brightness to 0, resulting in a black image. A value of 1 will leave the brightness unchanged.

      Parameters

      • image: BufferedImage

        the source image, which is not changed

      • h: number

        the hue shift to apply expressed as a number of rotations around the colour wheel

      • s: number

        the saturation factor to apply

      • b: number

        the brightness factor to apply

      Returns BufferedImage

      a tinted copy of the image

    • Returns a tinted copy of an image. The elements of the hsb array specify h, a hue shift (in rotations), s, a saturation adjustment factor, and b, a brightness adjustment factor, respectively. The hue of each pixel in the source image will be shifted by h × 360 degrees around the HSB colour wheel: a value of 0 (or 1) leaves the hue unchanged, while a value of 0.5 shifts the hue by 180 degrees to its complementary colour. The saturation level of each pixel will be multiplied by s. A value of 0 will convert the image to greyscale. A value of 1 will leave the saturation The brightness of each pixel will be multiplied by b. A value of 0 will set each pixel's brightness to 0, resulting in a black image. A value of 1 will leave the brightness unchanged.

      Parameters

      • image: BufferedImage

        the source image, which is not changed

      • hsb: [number, number, number]

      Returns any

      a tinted copy of the image

  • trim: function
    • Returns a trimmed copy of the source image that excludes fully transparent (alpha=0) rows and columns around the image's edges. If there are no such rows or columns, returns the original image. If the entire image is fully transparent, returns a 1x1 transparent image.

      Parameters

      Returns BufferedImage

      a trimmed copy of the image

  • view: function
    • Displays an image in an image viewer window.

      Parameters

      • image: BufferedImage

        the image to view, which is not changed

      • Optional title: string

        an optional title for the viewer

      • Optional modal: boolean

        if true, the viewer blocks the application until closed (default is false)

      • Optional parentWindow: JavaObject<"java.awt.Window">

        an optional parent window (default is the application window)

      Returns void

  • write: function
    • write(image: BufferedImage, filePath: string | JavaObject<"java.io.File">, format?: "png" | "jpg" | "jp2" | "bmp" | "gif", quality?: number, progressive?: boolean, ppi?: number): void
    • Writes an image to a file.

      Parameters

      • image: BufferedImage

        the image to write, which is not changed

      • filePath: string | JavaObject<"java.io.File">

        the file to write to

      • Optional format: "png" | "jpg" | "jp2" | "bmp" | "gif"

        the image format (default is "png")

      • Optional quality: number

        a value between 0 and 1 inclusive that describes the desired tradeoff between smaller size and better image quality (use -1 for default quality; 1 for maximum quality)

      • Optional progressive: boolean

        if true and supported by the format, requests that the file be organized so that it can be displayed progressively as it downloads (default is false)

      • Optional ppi: number

        if defined and supported by the encoder, the image's metadata will indicate that this value is the the resolution of the image in pixels per inch

      Returns void