Skip to content

Lens - ImageMagick cheatsheet

This cheatsheet describes how ImageMagick CLI arguments and options map into corresponding Lens arguments and options.

Distortions

Affine

ImageMagick:

shell
magick "<input-image>" -distort Affine "<control-points>" "<output-image>"

Lens:

ts
const { image: outputImage } = await distort(
  inputImage,
  "Affine",
  controlPoints
);

AffineProjection

ImageMagick:

shell
magick "<input-image>" \
-distort AffineProjection "<sx>, <rx>, <ry>, <sy>, <tx>, <ty>" \
"<output-image>"

Lens:

ts
const { image: outputImage } = await distort(
  inputImage,
  "AffineProjection",
  [sx, rx, ry, sy, tx, ty]
);

Arc

ImageMagick:

shell
magick "<input-image>" \
-distort Arc "<arc-angle>, <rotate-angle>, <outer-radius>, <inner-radius>" \
"<output-image>"

Lens:

ts
const { image: outputImage } = await distort(
  inputImage,
  "Arc",
  [arcAngle, rotateAngle, outerRadius, innerRadius]
);

Perspective

ImageMagick:

shell
magick "<input-image>" -distort Perspective "<control-points>" "<output-image>"

Lens:

ts
const { image: outputImage } = await distort(
  inputImage,
  "Perspective",
  controlPoints
);

PerspectiveProjection

ImageMagick:

shell
magick "<input-image>" \
-distort PerspectiveProjection "<sx>, <ry>, <tx>, <rx>, <sy>, <ty>, <px>, <py>" \
"<output-image>"

Lens:

ts
const { image: outputImage } = await distort(
  inputImage,
  "PerpsectiveProjection",
  [sx, ry, tx, rx, sy, ty, px, py]
);

Resampling

EWA

EWA resampling is used by default in ImageMagick and Lens.

Interpolated Color Lookup

ImageMagick:

shell
magick "<input-image>" \
-filter point \
-distort ...

Lens:

ts
distort(image, distortion, args, {
  preferredResampler: "point",
});

Supersampling

ImageMagick:

shell
magick "<input-image>" \
-set option:distort:scale <factor> \
-distort ...

Lens:

ts
distort(image, distortion, args, {
  outputScaling: factor,
});

Resample filter

TIP

See list of supported filters.

ImageMagick:

shell
magick "<input-image>" \
-filter <filter-name> \
-distort ...

Lens:

ts
distort(image, distortion, args, {
  filter: filterName,
});

Virtual Pixel Method

TIP

In following example "transparent" virtual pixel method is used for clarity. You can use any of supported methods. See list of supported virtual pixel methods.

ImageMagick:

shell
magick "<input-image>" \
-virtual-pixel transparent \
-distort ...

Lens:

ts
import { distort, VirtualPixelMethod } from "@alxcube/lens";
// ...
distort(image, distortion, args, {
  virtualPixelMethod: VirtualPixelMethod.TRANSPARENT,
});

Interpolation Method

TIP

In following example "integer" virtual pixel method is used for clarity. You can use any of supported methods. See list of supported interpolation methods.

ImageMagick:

shell
magick "<input-image>" \
-interpolate integer \
-distort ...

Lens:

ts
import { distort, InterpolationMethod } from "@alxcube/lens";
// ...
distort(image, distortion, args, {
  interpolationMethod: InterpolationMethod.INTEGER,
});

Options

Best-fit output viewport

ImageMagick: (using +distort instead -distort)

shell
magick "<input-image>" +distort ...

Lens:

ts
distort(image, distortion, args, {
  viewport: "bestFit",
});
// or
distort(image, distortion, args, {
  viewport: true,
});

Custom output viewport

ImageMagick:

shell
magick "<input-image>" \
-define distort:viewport=<width>x<height>+<offset-x>+<offset-y> \
-distort ...

Lens:

ts
distort(image, distortion, args, {
  viewport: { width: width, height: height, x: offsetX, y: offsetY },
});
// or
distort(image, distortion, args, {
  viewport: {
    x1: offsetX,
    y1: offsetY,
    x2: offsetX + width - 1,
    y2: offsetY + height - 1,
  },
});

Repage

Set custom viewport

ImageMagick:

shell
magick "<input-image>" \
+distort <distortion> "<args>" \
-repage <width>x<height>+<offset-x>+<offset-y> \
"<output-image>"

Lens:

ts
distort(inputImage, distortion, args, {
  repage: { width: width, height: height, x: offsetX, y: offsetY },
  viewport: true,
});

Set custom offset

ImageMagick:

shell
magick "<input-image>" \
+distort <distortion> "<args>" \
-repage +<offset-x>+<offset-y> \
"<output-image>"

Lens:

ts
distort(inputImage, distortion, args, {
  repage: [offsetX, offsetY],
  viewport: true,
});

Reset offset

ImageMagick:

shell
magick "<input-image>" \
+distort <distortion> "<args>" \
+repage \
"<output-image>"

Lens:

ts
distort(inputImage, distortion, args, {
  repage: true,
  viewport: true,
});

Image Background Color

ImageMagick:

shell
magick "<input-image>" \
-background <color> \
-distort ...

Lens:

INFO

Lens supports color options in following formats:

  • HEX ("#ffffff", "#fff")
  • HEXA ("#ffffffff")
  • RGB ("rgb(255, 255, 255)", "rgb(100%, 100%, 100%)")
  • RGBA ("rgba(255, 255, 255, 0.5)", "rgba(100%, 100%, 100%, 1)")
  • Array: [255, 255, 255, 255]
  • Special string constant for transparent color: "transparent"
ts
distort(inputImage, distortion, args, {
  imageBackgroundColor: color,
});

Matte Color

ImageMagick:

shell
magick "<input-image>" \
-mattecolor <color> \
-distort ...

Lens:

INFO

Lens supports color options in following formats:

  • HEX ("#ffffff", "#fff")
  • HEXA ("#ffffffff")
  • RGB ("rgb(255, 255, 255)", "rgb(100%, 100%, 100%)")
  • RGBA ("rgba(255, 255, 255, 0.5)", "rgba(100%, 100%, 100%, 1)")
  • Array: [255, 255, 255, 255]
  • Special string constant for transparent color: "transparent"
ts
distort(inputImage, distortion, args, {
  matteColor: color,
});