An embroidery machine sews areas of solid color, and every color in a design is one thread. A photo, or a logo saved as JPEG, contains thousands of slightly different shades. Before such an image can be turned into embroidery, the ACE service reduces it to a small number of flat color areas. The settings for this step go in the ACEParaOptimize object of a request with "Client": "ACE".
What the optimization does
The server processes the image in this order:
- Rasterization (SVG and AI only): the vector file is converted to a PNG. With
RasterizeNoAntiAliasit is drawn with hard edges. - Crop to content (optional,
EnableCropToContent): the empty margin around the motif is cut off. - Size: the server reads the pixel size and resolution (96 DPI if the PNG stores no resolution). With
EnableResampleit then calculates the target size. SVG/AI may be drawn again at a higher resolution for large target sizes. - Background detection (optional,
EnableBackgroundReplacement): the background becomes transparent, so it does not use up any of the target colors. - Pre-smoothing (
PreSmoothStrength): the source is smoothed before the colors are chosen. This applies to photo-like images only. Flat artwork is not smoothed. - Color reduction to at most
TargetColorCountcolors. Flat artwork keeps its original colors. Photo-like images get averaged colors. - Merge similar colors (optional,
MergeSimilarColors, off by default). - Noise filter (
NoiseStrength): isolated specks and grain take the color of their surroundings. - Thread table (optional,
ThreadTable): every color is replaced by the closest thread of the chosen thread series. - Preserve white highlights (optional,
PreserveWhiteHighlights): very light pixels are set to one white color. It takes one of the target colors only if the result would otherwise contain no white. - Output: a PNG with the reduced colors. The pixels are not resampled. The stored resolution gives the PNG the requested size, or the source resolution if
EnableResampleis off.
Details, value tables and pitfalls: Color reduction (steps 1 and 5 to 10), Scaling (size and resolution) and Background detection (step 4).
Two ways the optimization runs
- On its own, with
"RequestType": "OptimizePng". The response contains the optimized PNG and its key figures. No embroidery data is created. Use this request as a preview to tune the settings. - Automatically before every ACE Create request (
CreateTC,CreatePNG,CreateStatPNG,CreateACEPng,CreateACEVector). The server runs the same optimization with theACEParaOptimizeof that request and creates the embroidery from the optimized PNG. To get the colors of your preview, send the sameACEParaOptimizeas in the OptimizePng request.- It runs for PNG input (
PictureType"PNG") and for SVG/AI input. SVG/AI input is replaced by the optimized PNG. JPG, BMP and DXF are passed on without this optimization, so convert JPG and BMP files to PNG first. - It also runs if you leave out
ACEParaOptimize. The API defaults are then used. - After a successful optimization the server sets
ACEParaBitmapOptimize.Toleranceto 0,RemoveAreato 0 andMaxColorsto 24, so the embroidery step does not reduce the colors a second time. Any values you send in these three fields are replaced. ThreadTablematches the colors to threads. In a Create request the server also copies the name to an emptyACEParaBitmapToPunch.UseThreadCones, so the thread numbers are written into the embroidery file; see Color reduction.- Create reports no optimization errors. If the optimization fails, for example because of an unknown
ThreadTableor because background detection removed every pixel, the original image is used. None of theACEParaOptimizesettings are applied, and the color reduction of the embroidery step (ACEParaBitmapOptimize) works with the values you sent. SVG/AI input cannot be used without the optimization, so the Create request fails with HTTP 503 andCommunication with GiS ASPWebService failed!. Test new settings with OptimizePng first. SkipColorReduction: true(PNG only): the PNG is passed on unchanged. It is only cropped ifEnableCropToContentis also true. The color reduction of the embroidery step is switched off in the same way. Use this only for PNGs that already contain only their final thread colors (at most 24). For SVG/AI input it is ignored.
- It runs for PNG input (
The OptimizePng request
- Send exactly one source:
PictureBase64withPictureType: "PNG", orVectorBase64withVectorType: "SVG"or"AI". Type values are not case-sensitive. - Send plain Base64, without a data-URL prefix such as
data:image/png;base64,. ACEParaOptimizeis optional. OptimizePng does not needACEParaBitmapOptimize,ACEParaBitmapToVector,ACEParaBitmapToPunchorACEParaVector, and ignores them if you send them. The one exception isACEParaBitmapToPunch.UseThreadCones: ifThreadTableis empty, that series is used asThreadTable.ServerVersionis not evaluated and can be left out.
Errors return Success: false with HTTP status 400. RequestData then contains the message:
| RequestData | Cause |
|---|---|
No licence token found. | WebApiLicense is missing or empty. |
Invalid licence token. | The license is not valid for this service. |
No PictureBase64 or VectorBase64 data provided! | No source was sent. |
Only image or vector data may be specified! | PictureBase64 and VectorBase64 were both sent. |
Invalid PictureType JPG. PNG is supported. | PictureType is not "PNG". JPG and BMP are rejected. |
Invalid VectorType DXF. AI, SVG is supported. | VectorType is not "SVG" or "AI". |
Invalid Base64 data! | The data is not valid Base64, or it has a data-URL prefix. |
OptimizePng failed: Garntabelle nicht gefunden: <name> | The ThreadTable is not installed on the server. Use a series name returned by GetThreadConeSeries. |
OptimizePng failed: PNG enthaelt nur transparente Pixel. | No opaque pixel is left. The image is fully transparent, or background detection removed everything. |
OptimizePng failed: <message> | Other processing errors, for example an image that cannot be decoded. |
Response
The response is a JobResponseType. On success:
RequestData: the optimized PNG as Base64.Info: a JSON string. Parse it a second time to read its fields.
| Info field | Content |
|---|---|
pixelWidth, pixelHeight | Pixel size of the returned PNG, after cropping. For SVG/AI this is the size after rasterization. |
widthMm, heightMm | Size of the result in mm, rounded to 2 decimals. With EnableResample this is the requested size, reduced in proportion if EnableCropToContent cut off a margin. Without it, it is the size given by the image resolution. |
colorCount | Number of different colors among the visible (non-transparent) pixels of the result. |
mappedYarnColors | Only with a ThreadTable: the number of different threads the source colors map to before reduction. It is for information only; the number of colors in the result is colorCount. Without a thread table it is null. |
backgroundAutoDisabled | true if background detection had to be skipped because no opaque pixel was found along the image border. Rarely true, because Auto mode already skips images with a mostly transparent border. |
{
"pixelWidth": 1200,
"pixelHeight": 900,
"widthMm": 100.0,
"heightMm": 75.0,
"colorCount": 12,
"mappedYarnColors": null,
"backgroundAutoDisabled": false
}
- The PNG is transparent where the background was removed and where the source was already transparent. Pixels below 50 % opacity count as transparent, and all other pixels become fully opaque.
- The PNG has at most
TargetColorCountcolors, and flat artwork often has fewer. The exception isTargetColorCount1 withPreserveWhiteHighlights, which can give 2 colors. - Check
Successas well as the HTTP status; see Crop only below.
Crop only
With both SkipColorReduction: true and EnableCropToContent: true, OptimizePng only cuts off the empty margin:
- The response contains the cropped PNG with no color reduction, no scaling and no background removal. The pixels inside the cropped area are unchanged, and so is the stored resolution.
- SVG/AI input is rasterized first (with
RasterizeNoAntiAlias, if set), so the response is always a PNG. Send that PNG asPictureBase64in your next requests. - Margin is detected by transparency if the image border is mostly transparent (less than a quarter of the outermost pixels opaque). Otherwise it is detected by the border color within
BackgroundTolerance. WithEnableBackgroundReplacement: true, anything background detection would remove also counts as margin. See Background detection. Infohas the same fields, withcolorCount0,mappedYarnColors0 andbackgroundAutoDisabledfalse. Two more fields are added:cropped(false if there was nothing to crop; the PNG is then returned uncropped) andrasterized(true for SVG/AI input).- If the crop fails, for example because the image cannot be decoded,
Successis false andRequestDatastarts withCrop failed:. The HTTP status is not set to an error in this case. - Crop-only requests use no license quota, but a valid
WebApiLicenseis still required.
Why crop in a separate request: if EnableCropToContent is set in a normal optimization or Create request, TargetWidthMm and TargetHeightMm still describe the uncropped image. To give the motif itself a size, first send a crop-only request. Then send the cropped PNG with the target size.
SkipColorReduction: true without EnableCropToContent is ignored by OptimizePng: the full optimization runs and is billed.
Billing
- Each successful OptimizePng request uses license quota. How many units it uses depends on your license type; see ZSK Web API license.
- Failed requests and crop-only requests use no quota.
- The optimization inside a Create request is not billed on its own. Only the Create request is billed.
Rules for ACEParaOptimize values
- Field names are case-sensitive. A misspelled field is silently ignored and its default is used. This also applies to the object name: a misspelled
ACEParaOptimizemeans all defaults. - A field you leave out uses its default. If the object is left out or
null, all defaults are used. - Whole numbers:
TargetColorCount,NoiseStrength,PreSmoothStrength,BackgroundTolerance,SourceR/G/BandMergeSimilarDeltaE. A decimal value such as7.5in these fields makes the whole request fail. Decimals are accepted inScalePercent,TargetWidthMmandTargetHeightMm(for example 85.5). - No
nullor empty string in number and true/false fields. In any of them, for exampleScalePercentorEnableResample, it makes the whole request fail, and no JobResponseType is returned. To use a default, leave the field out. Only the text fieldsThreadTableandBackgroundDetectionModeacceptnull. - The values
"OptimizePng","ACE",PictureType,VectorTypeandBackgroundDetectionModeare not case-sensitive in OptimizePng. For Create requests, sendPictureTypein upper case ("PNG"); see Color reduction. - The server sets
KmeansAttemptsitself and ignores any value you send.
| Field | Values outside the range |
|---|---|
TargetColorCount | Moved to the nearest limit of 1 to 24: 30 becomes 24, 0 becomes 1. |
NoiseStrength, PreSmoothStrength | Moved to the nearest limit of 0 to 10. |
MergeSimilarDeltaE | Moved to 1 to 20 when MergeSimilarColors is true. |
BackgroundTolerance | Below 0 counts as 0. There is no upper limit; values above 150 are used as sent. The ACE client offers 0 to 150. |
SourceR, SourceG, SourceB | Values outside 0 to 255 make the request fail. |
ScalePercent | 0 or less counts as 100. |
TargetWidthMm, TargetHeightMm | 0 or less means not set. |
BackgroundDetectionMode | "Manual" (any case) selects Manual. Any other value counts as "Auto". |
ThreadTable | null, "" or omitted means no thread table. An unknown name is an error in OptimizePng; in Create the optimization is silently skipped. |
API defaults and the ACE client
The API defaults suit clean logos. The default preset of the ACE client is tuned for photos:
| Field | API default (field omitted) | ACE client default preset |
|---|---|---|
TargetColorCount | 8 | 12 |
NoiseStrength | 2 | 5 |
PreSmoothStrength | 0 | 5 |
EnableBackgroundReplacement | false | true |
BackgroundDetectionMode | "Auto" | "Auto" |
BackgroundTolerance | 30 | 30 |
ThreadTable | none | none |
For the same result as in the ACE client, or for photos, send these values explicitly.
Recommended starting values
| Image type | TargetColorCount | NoiseStrength | PreSmoothStrength |
|---|---|---|---|
| Clean logo: real vector graphic (SVG, AI) or flat PNG | number of logo colors | 2 | 0 |
| Logo from a JPEG, scan or screenshot | number of logo colors | 3 – 5 | 1 – 3 |
| Photo or complex illustration | 12 – 16 | 5 | 5 |
| Very noisy image, AI-generated image, or SVG/AI that only contains an embedded photo | 12 – 16 | 8 | 6 |
- Counting logo colors: a transparent or removed background does not count. A background that stays in the design counts as one color. With
PreserveWhiteHighlights: true, add 1 for a logo with white parts. - For a logo or graphic on a plain background, set
EnableBackgroundReplacement: true. For photos without a real background, set it to false. - For flat SVG/AI logos that get extra colors along their edges, also set
RasterizeNoAntiAlias: true. - Some SVG and AI files contain only an embedded photo. Treat them like a noisy photo; the server does not detect this.
Start with these values, check the result with OptimizePng, and then change one field at a time:
| What you see | What to change |
|---|---|
| Too many similar colors or color changes | Lower TargetColorCount |
| Important colors or shading are missing | Raise TargetColorCount |
| A logo gets extra colors along its edges | Set TargetColorCount to the number of logo colors |
| Grainy, speckled areas | Raise NoiseStrength |
| Thin lines, small dots or small text disappear | Lower NoiseStrength. Only 0 keeps lines 1 pixel wide; 5 or lower keeps 2-pixel lines; 8 or lower keeps 3-pixel lines. |
| Blotchy, uneven areas in a photo | Raise PreSmoothStrength |
| Outlines look rounded or small details merge in a photo | Lower PreSmoothStrength |
| A dark outline color appears along the edge of the motif | Lower PreSmoothStrength to 3 or less |
Related sections
- Color reduction: target colors, noise filter, pre-smoothing, thread table, white highlights, hard vector edges
- Scaling: embroidery size, resolution, PNG versus SVG/AI
- Background detection: Auto and Manual mode, tolerance, interaction with the embroidery parameters
- ACEParaOptimize: field reference
- ACEParameter, ExampleRequest: the embroidery parameters of Create requests