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 | Red, green, and blue values of the target pixel. |
| c, m, y, k | Cyan, magenta, yellow, and black values for CMYK target pixels. |
| a, o | Alpha and opacity values of the target pixel. |
| 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). |
| Constant | Value / Description |
|---|---|
| E, e | Euler's constant (e ≈ 2.7182818) |
| Epsilon, epsilon | Smallest difference between distinct values (1.0 × 10⁻¹²) |
| QuantumRange, quantumrange | Maximum channel scalar value dependent on build depth (e.g. 65535) |
| QuantumScale, quantumscale | Reciprocal of QuantumRange (1.0 / QuantumRange) |
| PHI, phi | Golden Ratio (φ ≈ 1.6180339) |
| PI, Pi, pi | Archimedes' constant (π ≈ 3.1415926) |
Supported native built-in mathematical functions:
rgb(), rgba(), hsl(), hsla(), cmyk()
channel(); This takes up to 5 expressions, and returns whichever is appropriate for the channel being processed, in order of RGBKA or CMYKA, e.g., channel(r, g+b, g-b).
for(init, condition, step), while(cond, stmt), do(cond, stmt), if(cond, then, else)
Operator Precedence
- 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): =, +=, -=, *=, /=, %=
Control Structures & Conditionals
expr1 if condition is non-zero (true); otherwise returns expr2.then_statement if condition holds true, or the optional else_statement if false.statement as long as condition evaluates to non-zero (true).statement at least once, then repeatedly executes it as long as condition evaluates to non-zero (true).Syntax & Semantics Rules
- Symbols and built-in identifiers are case insensitive.
- Ternary conditionals (
x ? y : z) can be nested within another ternary. - 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 permitted; e.g.,
1/2xreturns a syntax exception, instead use1/(2*x)explicitly.
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.