The FX special effects image operator applies mathematical expressions to each pixel channel of an image. The FX expression language provides a flexible mechanism for fine-grained pixel-level transformations, analysis, and custom procedural generation.
Create procedural canvases, mathematical gradients, colormaps, and fractal sets from equations.
Translate, flip, mirror, rotate, scale, shear, and warp images using custom coordinate functions.
Move color channels between images, convolve pixel neighborhoods, and merge multiple inputs seamlessly.
u). Other images in the sequence serve as reference data unless +fx is used to process every image individually.
Examples & Scripting Workflows
Convert a black canvas to navy blue by assigning a fixed fractional value to the blue channel:
Apply an exponential contrast curve formula to adjust image luminance response:
Assign intermediate variable calculations (Xi, Yj) to construct mathematical gradients:
Inject custom statistical Gaussian noise into an image pipeline:
myn=sqrt(-2*ln(rone))*cos(2*Pi*rtwo); myntwo=sqrt(-2*ln(rtwo))* \
cos(2*Pi*rone); pnoise=sqrt(p)*myn*sqrt(iso)* \
channel(4.28,3.86,6.68)/255; max(0,p+pnoise)' noisy.png
Leverage iterative for() loops to map complex polynomial fractal calculations into color lookup tables (CLUT):
Xi=2.4*i/w-1.2; \
Yj=2.4*j/h-1.2; \
for (pixel=0.0, (hypot(Xi,Yj) < 2.0) && (pixel < 1.0), \
delta=Xi^2-Yj^2; \
Yj=2.0*Xi*Yj+0.2; \
Xi=delta+0.4; \
pixel+=0.00390625 \
); \
pixel == 1.0 ? 0.0 : pixel" \
\( -size 1x1 xc:white xc:red xc:orange xc:yellow xc:green1 xc:cyan xc:blue \
xc:blueviolet xc:white -reverse +append -filter Cubic -resize 1024x1! \) \
-clut -rotate -90 julia-set.png
Use the debug() statement inside FX loops to calculate math output directly to standard output:
for (prime=2, prime < 30, composite=0; \
for (nn=2, nn < (prime/2+1), if ((prime % nn) == 0, composite++, ); nn++); \
if (composite <= 0, debug(prime), ); prime++)" null:
For more script examples, visit the official ImageMagick Usage Guide.
The Anatomy of an FX Expression
| Symbol | Description |
|---|---|
| u, v | First (u) and second (v) images in the active sequence list. |
| s, t, n | Current image reference (s), image sequence index (t), total image count (n). |
| i, j, p | Current column offset (i), row offset (j), target pixel channel accessor (p). |
| w, h, z | Current image width (w), height (h), channel depth (z). |
| r, g, b, a, o | Red, green, blue, alpha, and opacity values of the target pixel. |
| c, m, y, k | Cyan, magenta, yellow, and black values for CMYK target pixels. |
| intensity, luma | Calculated pixel color intensity or luma weighted scalar. |
| hue, saturation, lightness | Pixel color channels extracted from HSL space representation. |
| page.x, page.y, page.width | Canvas page offsets and dimensions. |
| mean, standard_deviation, kurtosis | Image or individual channel statistics (e.g. mean.r, s.kurtosis). |
Supported native built-in mathematical functions:
rgb(), rgba(), hsl(), hsla(), cmyk()
for(init, condition, step), while(cond, stmt), do(cond, stmt)
- Precedence 1 (Power): ^
- Precedence 2 (Unary/Multiplicative): -, *, /, %
- Precedence 3 (Additive/Shift): +, -, <<, >>
- Precedence 4 (Relational): <, <=, >, >=, ==, !=
- Precedence 5 (Bitwise/Logical): &, |, ~, &&, ||, !
- Precedence 6 (Conditional): ?: (Ternary operator)
- Precedence 7 (Assignment): =, +=, -=, *=, /=, %=
Syntax & Semantics Rules
- Symbols and built-in identifiers are case insensitive.
- Only one ternary conditional (
x ? y : z) is permitted per individual expression statement. - Single-character variable names are strictly reserved for built-ins. Use multi-character names for custom variables (e.g.
Xi,iso). - Unary operators take a lower priority than binary operators. For instance,
-3^2evaluates as-(3^2) = -9. Always use parentheses ((-3)^2 = 9) to explicitly indicate order. - Implicit multiplication is not performed over fractions;
1/2xis interpreted as(1/2)x. Write1/(2x)explicitly when required.
Source Images & Pixel Addressing
Image Sequence Indexes
Use index qualifiers on image references to query other images in the current sequence stack. Zero is the first image; negative indexes count backward from the end.
- u[0] First image
- u[2] Third image
- u[-1] Last image in list
- u[t] Current image (s)
Pixel & Neighborhood Access
All color values are normalized in the range 0.0 to 1.0. Use relative indexing (brackets) or absolute coordinates (braces) with p:
Format Escapes & String Evaluations
Instead of modifying pixels directly on an image canvas, FX formulas can be embedded into string escapes for metadata queries or dynamic settings:
%[fx:] Value Escape
Evaluated once per image in sequence. Returns numerical computation result directly into format attributes.
%[pixel:] Color Escape
Evaluates formula across color channels and automatically outputs a standardized CSS color or hex string.