<metaname="description"content="Use ImageMagick® to create, edit, compose, and convert bitmap images. Resize an image, crop it, change its shades and colors, add captions, and more."/>
<h1class="text-center">FX Special Effects Image Operator</h1>
<pclass="text-center"><ahref="fx.html#fx">The FX Special Effects Image Operator</a> • <ahref="fx.html#anatomy">The Anatomy of an FX Expression</a></p>
<aclass="anchor"id="fx"></a>
<pclass="lead magick-description">Use the FX special effects image operator to apply a mathematical expression to an image or image channels. Use FX to:</p>
<p>The expression can include variable assignments. Assignments, in most cases, reduce the complexity of an expression and permit some operations that might not be possible any other way. For example, lets create a radial gradient:</p>
for (nn=2, nn < (prime/2+1), if ((prime % nn) == 0, composite++, ); nn++); \
if (composite <= 0, debug(prime), ); prime++)" null:</code></pre>
<p>See <ahref="https://legacy.imagemagick.org/Usage/transform/index.html#fx">Using FX, The Special Effects Image Operator</a> for more examples.</p>
<p>The next section discusses the FX expression language.</p>
<h2><aclass="anchor"id="anatomy"></a>The Anatomy of an FX Expression</h2>
<h5>The FX Expression Language</h5>
<p>The formal FX expression language is defined here:</p>
<dlclass="row">
<dtclass="col-md-4"> numbers:</dt>
<ddclass="col-md-8"> integer, floating point, scientific notation (+/- required, e.g. 3.81469e-06), International System number postfixes (.e.g KB, Mib, GB, etc.)</dd>
<ddclass="col-md-8">an image offers array storage (e.g. p[-1,-1].r) bounded by its width and height. An image sequence represents multiple arrays (e.g. u.p[0,0].r, v.p[0,0].r). Storage is limited to Quantum values, e.g. [0..65535] for Q16 builds and floating point for HDRI-enabled builds.</dd>
<dtclass="col-md-4"><var>x</var><code>*</code><var>y</var></dt><ddclass="col-md-8"> multiplication (the asterisk <code>*</code> is optional, for example, <code>2u</code> or <code>2(x+y)</code> are acceptable)</dd>
<dtclass="col-md-4"><var>x</var><code><<</code><var>y</var></dt><ddclass="col-md-8"> left shift</dd>
<dtclass="col-md-4"><var>x</var><code>>></code><var>y</var></dt><ddclass="col-md-8"> right shift</dd>
<dtclass="col-md-4"><var>x</var><code><</code><var>y</var></dt><ddclass="col-md-8"> boolean relation, return value 1.0 if <var>x</var><<var>y</var>, otherwise 0.0</dd>
<dtclass="col-md-4"><var>x</var><code><=</code><var>y</var></dt><ddclass="col-md-8"> boolean relation, return value 1.0 if <var>x</var><= <var>y</var>, otherwise 0.0</dd>
<dtclass="col-md-4"><var>x</var><code>></code><var>y</var></dt><ddclass="col-md-8"> boolean relation, return value 1.0 if <var>x</var>><var>y</var>, otherwise 0.0</dd>
<dtclass="col-md-4"><var>x</var><code>>=</code><var>y</var></dt><ddclass="col-md-8"> boolean relation, return value 1.0 if <var>x</var>>= <var>y</var>, otherwise 0.0</dd>
<dtclass="col-md-4"><var>x</var><code>==</code><var>y</var></dt><ddclass="col-md-8"> boolean relation, return value 1.0 if <var>x </var>==<var> y</var>, otherwise 0.0</dd>
<dtclass="col-md-4"><var>x</var><code>!=</code><var>y</var></dt><ddclass="col-md-8"> boolean relation, return value 1.0 if <var>x </var>!=<var> y</var>, otherwise 0.0</dd>
<dtclass="col-md-4"><var>x</var><code>&&</code><var>y</var></dt><ddclass="col-md-8"> logical AND connective, return value 1.0 if <var>x</var>> 0 and <var>y</var>> 0, otherwise 0.0</dd>
<dtclass="col-md-4"><var>x</var><code>||</code><var>y</var></dt><ddclass="col-md-8"> logical OR connective (inclusive), return value 1.0 if <var>x</var>> 0 or <var>y</var>> 0 (or both), otherwise 0.0</dd>
<dtclass="col-md-4"><code>~</code><var>x</var></dt><ddclass="col-md-8"> logical NOT operator, return value 1.0 if <var>not</var><var>x</var>> 0, otherwise 0.0</dd>
<dtclass="col-md-4"><var>x</var><code>?</code><var>y</var><code>:</code><var>z</var></dt><ddclass="col-md-8">ternary conditional expression, return value <var>y</var> if <var>x</var> != 0, otherwise <var>z</var>; only one ternary conditional permitted per statement</dd>
<dtclass="col-md-4"><var>x</var><code>=</code><var>y</var></dt><ddclass="col-md-8">assignment; single character variables are reserved, instead use 2 or more characters, letter combinations only (e.g. Xi not X1)</dd>
<dtclass="col-md-4"><code>intensity</code></dt><ddclass="col-md-8"> pixel intensity whose value respects the <ahref="command-line-options.html#intensity">-intensity</a> option.</dd>
<dtclass="col-md-4"><code>airy(</code><var>x</var><code>)</code></dt><ddclass="col-md-8"> Airy function (max=1, min=0); airy(<var>x</var>)=[jinc(<var>x</var>)]<sup>2</sup>=[2*j1(<var>pi*x</var>)/(<var>pi*x</var>)]<sup>2</sup></dd>
<dtclass="col-md-4"><code>alt(</code><var>x</var><code>)</code></dt><ddclass="col-md-8"> sign alternation function (return 1.0 if int(<var>x</var>) is even, -1.0 if int(<var>x</var>) is odd)</dd>
<dtclass="col-md-4"><code>asin(</code><var>x</var><code>)</code></dt><ddclass="col-md-8"> arc sine function</dd>
<dtclass="col-md-4"><code>asinh(</code><var>x</var><code>)</code></dt><ddclass="col-md-8"> inverse hyperbolic sine function</dd>
<dtclass="col-md-4"><code>atan2(</code><var>y</var>,<var>x</var><code>)</code></dt><ddclass="col-md-8"> arc tangent function of two variables</dd>
<dtclass="col-md-4"><code>ceil(</code><var>x</var><code>)</code></dt><ddclass="col-md-8">smallest integral value not less than argument</dd>
<dtclass="col-md-4"><code>channel(</code><var>r</var>,<var>g</var>,<var>b</var>,<var>a</var><code>)</code></dt><ddclass="col-md-8">select numeric argument based on current channel</dd>
<dtclass="col-md-4"><code>channel(</code><var>c</var>,<var>m</var>,<var>y</var>,<var>k</var>,<var>a</var><code>)</code></dt><ddclass="col-md-8">select numeric argument based on current channel</dd>
<dtclass="col-md-4"><code>debug(</code><var>x</var><code>)</code></dt><ddclass="col-md-8"> print <var>x</var> (useful for debugging your expression)</dd>
<dtclass="col-md-4"><code>while(</code><var>expression</var>, <var>condition test</var><code>)</code></dt><ddclass="col-md-8"> iterate while the condition is not equal to 0</dd>
<dtclass="col-md-4"><code>drc(</code><var>x</var>,<var>y</var><code>)</code></dt><ddclass="col-md-8"> dynamic range compression (knee curve); drc(<var>x</var>,<var>y</var>)=(<var>x</var>)/(<var>y</var>*(<var>x</var>-1)+1); -1<<var>y</var><1 </dd>
<dtclass="col-md-4"><code>exp(</code><var>x</var><code>)</code></dt><ddclass="col-md-8"> natural exponential function (<var>e<sup>x</sup></var>)</dd>
<dtclass="col-md-4"><code>floor(</code><var>x</var><code>)</code></dt><ddclass="col-md-8"> largest integral value not greater than argument</dd>
<dtclass="col-md-4"><code>for(</code><var>initialization</var>, <var>condition test</var>, <var>expression</var><code>)</code></dt><ddclass="col-md-8"> iterate while the condition is not equal to 0</dd>
<dtclass="col-md-4"><code>gcd(</code><var>x</var>,<var>y</var><code>)</code></dt><ddclass="col-md-8"> greatest common denominator</dd>
<dtclass="col-md-4"><code>hypot(</code><var>x</var>,<var>y</var><code>)</code></dt><ddclass="col-md-8"> the square root of x<sup>2</sup>+y<sup>2</sup></dd>
<dtclass="col-md-4"><code>if(</code><var>condition test</var>, <var>nonzero-expression</var>, <var>zero-expression</var><code>)</code></dt><ddclass="col-md-8"> interpret expression depending on condition</dd>
<dtclass="col-md-4"><code>int(</code><var>x</var><code>)</code></dt><ddclass="col-md-8"> greatest integer function (return greatest integer less than or equal to <var>x</var>)</dd>
<dtclass="col-md-4"><code>isnan(</code><var>x</var><code>)</code></dt><ddclass="col-md-8">return 1.0 if <var>x</var> is NAN, 0.0 otherwise</dd>
<dtclass="col-md-4"><code>j0(</code><var>x</var><code>)</code></dt><ddclass="col-md-8"> Bessel functions of <var>x</var> of the first kind of order 0</dd>
<dtclass="col-md-4"><code>j1(</code><var>x</var><code>)</code></dt><ddclass="col-md-8"> Bessel functions of <var>x</var> of the first kind of order 1</dd>
<dtclass="col-md-4"><code>jinc(</code><var>x</var><code>)</code></dt><ddclass="col-md-8"> jinc function (max=1, min=-0.1323); jinc(<var>x</var>)=2*j1(pi*<var>x</var>)/(pi*<var>*x</var>)</dd>
<dtclass="col-md-4"><code>not(</code><var>x</var><code>)</code></dt><ddclass="col-md-8"> return 1.0 if <var>x</var> is zero, 0.0 otherwise</dd>
<dtclass="col-md-4"><code>pow(</code><var>x</var>,<var>y</var><code>)</code></dt><ddclass="col-md-8"> power function (<var>x<sup>y</sup></var>)</dd>
<dtclass="col-md-4"><code>rand()</code></dt><ddclass="col-md-8"> value uniformly distributed over the interval [0.0, 1.0) with a 2 to the 128th-1 period</dd>
<dtclass="col-md-4"><code>round()</code></dt><ddclass="col-md-8"> round to integral value, regardless of rounding direction</dd>
<dtclass="col-md-4"><code>sign(</code><var>x</var><code>)</code></dt><ddclass="col-md-8"> return -1.0 if <var>x</var> is less than 0.0 otherwise 1.0</dd>
<dtclass="col-md-4"><code>sin(</code><var>x</var><code>)</code></dt><ddclass="col-md-8"> sine function</dd>
<dtclass="col-md-4"><code>sinc(</code><var>x</var><code>)</code></dt><ddclass="col-md-8"> sinc function (max=1, min=-0.21); sinc(<var>x</var>)=sin(<var>pi*x</var>)/(<var>pi*x</var>)</dd>
<dtclass="col-md-4"><code>trunc(</code><var>x</var><code>)</code></dt><ddclass="col-md-8"> round to integer, towards zero</dd>
<dtclass="col-md-4"><code>while(</code><var>condition test</var>, <var>expression</var><code>)</code></dt><ddclass="col-md-8"> iterate while the condition is not equal to 0</dd>
<p>The expression semantics include these rules:</p>
<ul>
<li>symbols are case insensitive</li>
<li>only one ternary conditional (e.g. x ? y : z) per statement</li>
<li>statements are assignments or the final expression to return</li>
<li>an assignment starts a statement, it is not an operator</li>
<li>single character variables are reserved. Assignments to reserved built-ins do not throw an exception and have no effect; e.g. <code>r=3.0; r</code> returns the pixel red color value, not 3.0</li>
<li>unary operators have a lower priority than binary operators, that is, the unary minus (negation) has lower precedence than exponentiation, so -3^2 is interpreted as -(3^2) = -9. Use parentheses to clarify your intent (e.g. (-3)^2 = 9).</li>
<li>care must be exercised when using the slash ('/') symbol. The string of characters <var>1/2x</var> is interpreted as (1/2)x. The contrary interpretation should be written explicitly as 1/(2x). Again, the use of parentheses helps clarify the meaning and should be used whenever there is any chance of misinterpretation.</li>
</ul>
<br/>
<h5>Source Images</h5>
<p>The symbols <code>u</code> and <code>v</code> refer to the first and second images, respectively, in the current image sequence. Refer to a particular image in a sequence by appending its index to any image reference (usually <code>u</code>), with a zero index for the beginning of the sequence. A negative index counts from the end. For example, <code>u[0]</code> is the first image in the sequence, <code>u[2]</code> is the third, <code>u[-1]</code> is the last image, and <code>u[t]</code> is the current image. The current image can also be referenced by <code>s</code>. If the sequence number exceeds the length of the sequence, the count is wrapped. Thus in a 3-image sequence, <code>u[-1]</code>, <code>u[2]</code>, and <code>u[5]</code> all refer to the same (third) image.</p>
<p>As an example, we form an image by averaging the first image and third images (the second (index 1) image is ignored and just junked):</p>
<p>By default, the image to which <code>p</code>, <code>r</code>, <code>g</code>, <code>b</code>, <code>a</code>, etc., are applied is the current image <code>s</code> in the image list. This is equivalent to <code>u</code> except when used in an escape sequence <code>%[fx:...]</code>. </p>
<p>It is important to note the special role played by the first image. This is the only image in the image sequence that is modified, other images are used only for their data. As an illustrative example, consider the following, and note that the setting <ahref="command-line-options.html#channel">-channel red</a> instructs <ahref="command-line-options.html#fx">-fx</a> to modify only the red channel; nothing in the green or blue channels will change. It is instructive to ponder why the result is not symmetric.</p>
<p>All color values are normalized to the range of 0.0 to 1.0. The alpha channel ranges from 0.0 (fully transparent) to 1.0 (fully opaque).</p>
<p>The pixels are processed one at a time, but a different pixel of an image can be specified using a pixel index represented by <code>p</code>. For example,</p>
<preclass="highlight"><code>p[-1].g green value of pixel to the immediate left of the current pixel
p[-1,-1].r red value of the pixel diagonally left and up from current pixel
</code></pre>
<p>To specify an absolute position, use braces, rather than brackets.</p>
<preclass="highlight"><code>p{0,0}.r red value of the pixel in the upper left corner of the image
p{12,34}.b blue pixel value at column number 12, row 34 of the image
</code></pre>
<p>Integer values of the position retrieve the color of the pixel referenced, while non-integer position values return a blended color according to the current <ahref="command-line-options.html#interpolate">-interpolate</a> setting.</p>
<p>A position outside the boundary of the image retrieves a value dictated by the <ahref="command-line-options.html#virtual-pixel">-virtual-pixel</a> option setting.</p>
<h5>Apply an Expression to Select Image Channels</h5>
<p>Use the <ahref="command-line-options.html#channel">-channel</a> setting to specify the output channel of the result. If no output channel is given, the result is set over all channels except the opacity channel. For example, to replace the red channel of <code>alpha.png</code> with the average of the green channels from the images <code>alpha.png</code> and <code>beta.png</code>, use:</p>
<preclass="highlight"><code>convert alpha.png beta.png -channel red -fx "(u.g+v.g)/2" gamma.png
</code></pre>
<h5>Results</h5>
<p>The <ahref="command-line-options.html#fx">-fx</a> operator evaluates the given expression for each channel (set by <ahref="command-line-options.html#channel">-channel</a>) of each pixel in the first image (<code>u</code>) in the sequence. The computed values are temporarily stored in a copy (clone) of that first image until all the pixels have been processed, after which this single new image replaces the list of images in the current image sequence. As such, in the previous example the updated version of <code>alpha.png</code> replaces both of the original images, <code>alpha.png</code> and <code>beta.png</code>, before being saved as <code>gamma.png</code>.</p>
<p>The current image <code>s</code> is set to the first image in the sequence (<code>u</code>), and <code>t</code> to its index, 0. The symbols <code>i</code> and <code>j</code> reference the current pixel being processed.</p>
<p>For use with <ahref="command-line-options.html#format_identify_">-format</a>, the value-escape <code>%[fx:]</code> is evaluated just once for each image in the current image sequence. As each image in the sequence is being evaluated, <code>s</code> and <code>t</code> successively refer to the current image and its index, while <code>i</code> and <code>j</code> are set to zero, and the current channel set to red (<ahref="command-line-options.html#channel">-channel</a> is ignored). An example:</p>
-format 'Red channel of NW corner of image #%[fx:t] is %[fx:s]\n' info:
Red channel of NW corner of image #0 is 0.464883
Red channel of NW corner of image #1 is 0.184582
</code></pre>
<p>Here we use the image indexes to <var>rotate</var> each image differently, and use <code>-set</code> with the image index to set a different <var>pause delay</var> on the first image in the animation:</p>
<p>The color-escape <code>%[pixel:]</code> or <code>%[hex:]</code> is evaluated once per image and per color channel in that image (<ahref="command-line-options.html#channel">-channel</a> is ignored), The values generated are then converted into a color string (a named color or hex color value). The symbols <code>i</code> and <code>j</code> are set to zero, and <code>s</code> and <code>t</code> refer to each successively current image and index.</p>
</div>
</div>
</main><!-- /.container -->
<footerclass="magick-footer">
<divclass="container">
<p><ahref="security-policy.html">Security</a> •
<ahref="news.html">News</a>
<ahref="fx.html#"><imgclass="d-inline"id="wand"alt="And Now a Touch of Magick"width="16"height="16"src="../images/wand.ico"/></a>