Pencil sketch¶
| Fact | Value |
|---|---|
| Also called | Sketch effect, Color dodge sketch |
| Status | Implemented and tested |
| Family | Artistic and geometric effects |
| Engine | Browser |
| Type | Nonlinear, spatial, color dodge with a blurred negative |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | floor(3 * sigma + 0.5) |
| Code | web/src/core/filters/artistic/painterly.ts, pencilSketchFilter |
| Operator code | web/src/core/filters/artistic/painterly.ts, sketchPlane |
Overview¶
The pencil sketch turns the image into a light drawing. Flat areas turn white, and edges and dark details stay as gray lines. It blends the luma with its own blurred negative by color dodge, a blend mode of the W3C Compositing and Blending spec.
How it works¶
- Compute the luma and invert it.
- Blur the inverted image with a Gaussian.
- Divide the luma by one minus the blurred negative, capped at white.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(Y\): The luma 0.2126 R + 0.7152 G + 0.0722 B
- \(N, B\): The negative and the blurred negative
- \(S\): The sketch
Y is the luma, N its negative and B the negative blurred with a Gaussian cut at 3 sigma. S is the color dodge of the W3C Compositing and Blending spec, with Y as the backdrop and B as the source. In flat areas B is close to N, so Y / (1 - B / 255) is close to 1 and the area turns white. Near an edge the blur mixes in the other side, so the dodge stays below 1 and leaves a gray line. The recipe of dodging with a blurred negative is PixelNexus's own choice, though photo editors use the same idea. With Keep colors on, the color differences C - Y of the image are added back to S.
Where the image is flat, the blurred negative equals the negative, 1 - Y in the range 0 to 1. Then the dodge Y / (1 - B) equals Y / Y = 1, white. Near an edge, the blur mixes in the other side. On the dark side the negative is lowered by the bright neighbors, so the ratio stays below 1 and a gray line appears.
Kernel or operator¶
The Inspector shows the steps of the effect as formulas with the current settings.
Algorithm¶
- Compute the luma and its negative.
- Blur the negative with a Gaussian.
- Blend the luma with the blurred negative by color dodge.
- With Keep colors on, add the color differences back.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Blur sigma |
8 | 1 to 40 px | The sigma of the Gaussian blur of the negative. |
Keep colors colored |
off | On or off | Keep the color differences of the image and replace only its luma by the sketch. |
Border mode border |
Reflect 101 (default) | Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap | How pixels outside the image are filled when the window reaches past the edge. |
Implementation¶
pencilSketchFilter in web/src/core/filters/artistic/painterly.ts uses sketchPlane and colorDodge.
Worked example¶
| x | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 17 |
|---|---|---|---|---|---|---|---|---|
| Input | 80 | 80 | 80 | 80 | 200 | 200 | 200 | 200 |
| Sketch, blur 3 | 243 | 196 | 175 | 155 | 255 | 255 | 255 | 255 |
The middle row of a 24 by 3 image that steps from 80 to 200 between x = 11 and x = 12. Far from the step, the bright side is white and the dark side comes close to it, 243 at x = 6. Just left of the step, the blurred negative takes in the bright side, so the dodge stays well below white and draws the line on the dark side.
Complexity¶
- Time: \(O(N r)\)
- Memory: \(O(N)\)
N is the number of pixels and r the radius of the Gaussian. The blur runs as two 1D passes.
Artifacts and limitations¶
Limitations
- Dark areas stay dark, since the dodge cannot lighten black.
- Noise turns into fine strokes.
Artifacts
- Faint halos along strong edges at large blur.
Tests¶
The reference writes the blur and the dodge again with NumPy and SciPy, and compares every output value on the test images of the fixtures. Unit tests check the dodge at the special cases of the spec.
web/src/core/filters/filters.test.tsweb/src/core/filters/run.test.tsweb/src/core/filters/artistic/artistic.test.ts
References¶
- W3C. Compositing and Blending Level 1, section 10.1.7, the color-dodge blend mode. W3C Candidate Recommendation Draft, 21 March 2024. https://www.w3.org/TR/compositing-1/