Convert image bitmaps into SVG by tracing outlines. Typescript version of imagetracerjs. Use imagetracerts-nodejs or imagetracerts-browser for platform-specific file input and output.
Trace images into SVG.
Typescript reimplementation of imagetracer-js.
Provides ESM and CJS packages.
The core package only works with ImageData, use @image-tracer-ts/browser or @image-tracer-ts/nodejs to trace different image formats (like PNG, JPEG, etc) from different sources (files, buffers, URLs, etc).
---
```
import { ImageTracer, Options } from '@image-tracer/core';
const options: Partial
const tracer = new ImageTracer(options)
const svgString = tracer.traceImage(imageData, drawer)
---
Tracing happens in four steps, each comes with several configuration options. Pass them in a configuration object, or as command line parameter, prefixed with -- (i.e. --blurRadius 5).
#### Step 1: Image preprocessing
Optional steps to adjust image before processing.
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| blurRadius | number (between 0 and 5) | 0 (off) | Number of pixels (in each direction) to calculate the blurred pixel value from. |20
| blurDelta | number (0 - 4*255) | | Maximum allowed difference between original pixel and blurred pixel when summing up RGBA values. If a blurred pixel exceeds delta, the original pixel is used instead. |false
| sharpen | boolean | | Use sharpen filter |20
| sharpenThreshold | number (0 - 4*255) | | Maximum allowed difference between original pixel and sharpened pixel when summing up RGBA values. If a sharpened pixel exceeds threshold, the original pixel is used instead. |
#### Step 2: Building a color index
Create a base palette and apply clustering to the pixels in the image to build color layer masks.
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| colorSamplingMode | generate \| sample \| scan \| palette | scan | Select how internal palette will be generated: Array<{r:number, g:number, b:number, a?:number}>
|generate: Generate colors along the spectrum independent of image colors. Builds a grayscale palette if less than 8 colors are requested. sample: Randomly access image for colors. scan: Step through the image along a raster. palette: Manually supply colors.
| palette | | null | Array of colors to use with colorSamplingMode=palette |16
| numberOfColors | number | | Number of colors to be generated. |3
| colorClusteringCycles | number | | Number of color clustering cycles. |off
| colorDistanceBuffering | \| on \| reasonable | reasonable | Buffers color distances during clustering. Very efficient if palette has more than 30 colors. |0
| minColorQuota | number (between 0 and 1) | (off) | Threshold for color pruning during color clustering. If ratio between pixels of a color and all pixels is below the given number, the color will be replaced by a random color. |
#### Step 3: Tracing
Create vector data from pixels.
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| minShapeOutline | number | 0 (off)| Discard traced areas with an outline of less than the given number of points. |off
| interpolation | \| interpolate | interpolate | Sets interpolation mode. |true
| enhanceRightAngles | boolean | | Do not interpolate right angles. |1
| lineErrorMargin | number | | Line tracer error margin. Gives the squared maximum distance a point can be off a line trajectory to be still put on it. |1
| curveErrorMargin | number | | Curve tracer error margin. Gives the squared maximum distance a point can be off a curved trajectory to be put on it. |
#### Step 4: Drawing
Create SVG data from vectors.
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| fillStyle | fill \| stroke \| stroke+fill | stroke+fill | Select how color segments are colored. |1
| strokeWidth | number | | Stroke width written to SVG path. |1
| scale | number | | Multiply all coordinates by this number. |1
| decimalPlaces | number | | Number of decimal places in svg values. |true
| viewBox | boolean | | If enabled, the viewBox attribute will be set on the SVG tag element. |off
| trim | \| ratio \| all | off | Removes empty border from the image. |false
| lineFilter | boolean | | Do not draw lines (areas with less than 3 points). |false
| desc | boolean | | Write a desc attribute to SVG edges with debug output |0
| segmentEndpointRadius | number | (off) | Enables control output, draws white dots with given radius at segment borders. |0
| curveControlPointRadius | number | (off) | Enables control output, draws curve control points as cyan dots with given radius.
#### Misc
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| verbose | boolean | false | Write status data to console during trace. |.svg
| out | string | input file name + | Set output file name (in ImageTracerNodejs) |
| preset | see preset names below | null | Use preset from command line |
Preset configurations can be imported from the Options object:
``
import { Options } from '@image-tracer/core';
ImageTracerBrowser.fromUrl--preset
From command line, the parameter can be used along with a preset name:default
- posterized1
- posterized2 posterized3curvy
- sharp
- detailed
- smoothed
- grayscale
- fixedpalette
- randomsampling1
- randomsampling2artistic1
- artistic2 artistic3 artistic4
!Collection of example images for each preset
---
You can provide a custom ImageDrawer to change how output is created from traced data:
```
const drawer = new CustomizedImageDrawer()
const svgString = new ImageTracer(options).traceImage(imageData, drawer)