potrace2.1.2 • Public • Published
Example and demo
|Original image||Potrace output||Posterized output|
(Example image inherited from online demo of the browser version)
npm install potrace
var potrace =fs = ;potrace;
You can also provide a configuration object as a second argument.
var params =background: '#49ffd2'color: 'blue'threshold: 120;potrace;
If you want to run Potrace algorithm multiple times on the same image with different threshold setting and merge results together in a single file -
posterize method does exactly that.
potrace;// or if you know exactly where you want to break it on different levelspotrace;
Advanced usage and configuration
posterize methods return instances of
Posterizer classes respectively to a callback function as third argument.
You can also instantiate these classes directly:
var potrace = ;// Tracingvar trace = ;// You can also pass configuration object to the constructortrace;trace;// Posterizationvar posterizer = ;posterizer;
Callback function provided to
loadImage methods will be executed in context of the
Posterizer instance, so if it doesn't go against your code style - you can just do
Jimp module is used on the back end, so first argument accepted by
loadImage method could be anything Jimp can read: a
Buffer, local path or a url string. Supported formats are: PNG, JPEG or BMP. It also could be a Jimp instance (provided bitmap is not modified)
Potrace class expects following parameters:
- turnPolicy - how to resolve ambiguities in path decomposition. Possible values are exported as constants:
TURNPOLICY_MAJORITY. Refer to this document for more information (page 4)
- turdSize - suppress speckles of up to this size
- alphaMax - corner threshold parameter
- optCurve - curve optimization
- optTolerance - curve optimization tolerance
- threshold - threshold below which color is considered black.
Should be a number in range 0..255 or
THRESHOLD_AUTOin which case threshold will be selected automatically using Algorithm For Multilevel Thresholding
- blackOnWhite - specifies colors by which side from threshold should be turned into vector shape
- color - Fill color. Will be ignored when exporting as <symbol>. (default:
COLOR_AUTO, which means black or white, depending on
- background - Background color. Will be ignored when exporting as <symbol>. By default is not present (
Posterizer class has same methods as
Potrace, in exception of
Configuration object is extended with following properties:
- fillStrategy - determines how fill color for each layer should be selected. Possible values are exported as constants:
FILL_DOMINANT- most frequent color in range (used by default),
FILL_MEAN- arithmetic mean (average),
FILL_MEDIAN- median color,
FILL_SPREAD- ignores color information of the image and just spreads colors equally in range 0..<threshold> (or <threshold>..255 if
blackOnWhiteis set to
- rangeDistribution - how color stops for each layer should be selected. Ignored if
stepsis an array. Possible values are:
RANGES_AUTO- Performs automatic thresholding (using Algorithm For Multilevel Thresholding). Preferable method for already posterized sources, but takes long time to calculate 5 or more thresholds (exponential time complexity)
(used by default)
RANGES_EQUAL- Ignores color information of the image and breaks available color space into equal chunks
- steps - Specifies desired number of layers in resulting image. If a number provided - thresholds for each layer will be automatically calculated according to
rangeDistributionparameter. If an array provided it expected to be an array with precomputed thresholds for each layer (in range 0..255)
STEPS_AUTOwhich will result in
4, depending on
- threshold - Breaks image into foreground and background (and only foreground being broken into desired number of layers). Basically when provided it becomes a threshold for last (least opaque) layer and then
steps - 1intermediate thresholds calculated. If steps is an array of thresholds and every value from the array is lower (or larger if blackOnWhite parameter set to
false) than threshold - threshold will be added to the array, otherwise just ignored.
- all other parameters that Potrace class accepts
- When number of
stepsis greater than 10 - an extra layer could be added to ensure presence of darkest/brightest colors if needed to ensure presence of probably-important-at-this-point details like shadows or line art.
- With big number of layers produced image will be looking brighter overall than original due to math error at the rendering phase because of how layers are composited.
- With default configuration
rangeDistributionsettings all set to auto, resulting in a 4 thresholds/color stops being calculated with Multilevel Thresholding algorithm mentioned above. Calculation of 4 thresholds takes 3-5 seconds on average laptop. You may want to explicitly limit number of
stepsto 3 to moderately improve processing speed.
The GNU General Public License version 2 (GPLv2). Please see License File for more information.