blob: 8e1c465eb5a058d30545b631fbb2345e5ea81a74 [file] [log] [blame]
Cristy594d2772015-11-15 10:47:42 -05001
2
3
4
cristy29dc8652015-05-12 16:26:21 +00005<!DOCTYPE html>
cristyd2432012015-05-04 23:15:15 +00006<html lang="en">
cristyad41c0c2012-10-31 00:49:28 +00007<head>
Cristy912a9222016-06-17 15:37:28 -04008 <meta charset="utf-8" />
Cristy3b0b58d2016-11-06 07:40:23 -05009 <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
10 <meta http-equiv="x-ua-compatible" content="ie=edge" />
Cristy6549abb2016-11-10 08:25:55 -050011 <title>The Fx Special Effects Image Operator @ ImageMagick</title>
Cristy594d2772015-11-15 10:47:42 -050012 <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
13 <meta name="application-name" content="ImageMagick"/>
14 <meta name="description" content="ImageMagick® is a software suite to create, edit, compose, or convert bitmap images. It can read and write images in a variety of formats (over 200) including PNG, JPEG, JPEG-2000, GIF, WebP, Postscript, PDF, and SVG. Use ImageMagick to resize, flip, mirror, rotate, distort, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves."/>
Cristy364a72f2016-11-04 22:38:41 -040015 <meta name="application-url" content="https://www.imagemagick.org"/>
Cristy594d2772015-11-15 10:47:42 -050016 <meta name="generator" content="PHP"/>
17 <meta name="keywords" content="the, fx, special, effects, image, operator, ImageMagick, PerlMagick, image processing, image, photo, software, Magick++, OpenMP, convert"/>
18 <meta name="rating" content="GENERAL"/>
19 <meta name="robots" content="INDEX, FOLLOW"/>
20 <meta name="generator" content="ImageMagick Studio LLC"/>
21 <meta name="author" content="ImageMagick Studio LLC"/>
22 <meta name="revisit-after" content="2 DAYS"/>
23 <meta name="resource-type" content="document"/>
Cristyfe36f992016-12-29 20:24:13 -050024 <meta name="copyright" content="Copyright (c) 1999-2017 ImageMagick Studio LLC"/>
Cristy594d2772015-11-15 10:47:42 -050025 <meta name="distribution" content="Global"/>
26 <meta name="magick-serial" content="P131-S030410-R485315270133-P82224-A6668-G1245-1"/>
Cristy912a9222016-06-17 15:37:28 -040027 <meta name="google-site-verification" content="_bMOCDpkx9ZAzBwb2kF3PRHbfUUdFj2uO8Jd1AXArz4" />
Cristyfe36f992016-12-29 20:24:13 -050028 <link rel="canonical" href="https://www.imagemagick.org/script/fx.html"/>
Cristy594d2772015-11-15 10:47:42 -050029 <link rel="icon" href="../images/wand.png"/>
30 <link rel="shortcut icon" href="../images/wand.ico"/>
Cristybc232422016-11-22 07:46:18 -050031 <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Roboto:900,400,400italic,700,700italic,300,300italic|Open+Sans:300italic,400italic,700italic,300,400,600,700" />
Cristyce4a3552015-12-14 13:53:35 -050032 <link rel="stylesheet" href="css/magick.css"/>
cristyad41c0c2012-10-31 00:49:28 +000033</head>
cristyd2432012015-05-04 23:15:15 +000034<body>
cristyad41c0c2012-10-31 00:49:28 +000035<div class="main">
cristyd2432012015-05-04 23:15:15 +000036<div class="magick-masthead">
37 <div class="container">
Cristy6549abb2016-11-10 08:25:55 -050038 <script async="async" src="http://localhost/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle"
Cristy594d2772015-11-15 10:47:42 -050039 style="display:block"
40 data-ad-client="ca-pub-3129977114552745"
41 data-ad-slot="6345125851"
42 data-ad-format="auto"></ins>
cristy29f2c972015-07-28 13:37:46 +000043 <script>
Cristy542697a2015-10-12 20:10:32 -040044 (adsbygoogle = window.adsbygoogle || []).push({});
cristyd2432012015-05-04 23:15:15 +000045 </script>
cristyd2432012015-05-04 23:15:15 +000046 <nav class="magick-nav">
cristy44443b92015-05-05 00:06:07 +000047 <a class="magick-nav-item " href="../index.html">Home</a>
Cristy9017f3a2017-01-21 10:51:11 -050048 <a class="magick-nav-item " href="download.html">Download</a>
cristyd2432012015-05-04 23:15:15 +000049 <a class="magick-nav-item " href="command-line-tools.html">Tools</a>
Cristye93be682015-11-26 09:07:51 -050050 <a class="magick-nav-item " href="command-line-processing.html">Command-line</a>
cristyea2793e2015-05-31 16:52:26 +000051 <a class="magick-nav-item " href="resources.html">Resources</a>
Cristy9017f3a2017-01-21 10:51:11 -050052 <a class="magick-nav-item " href="develop.html">Develop</a>
Cristy6549abb2016-11-10 08:25:55 -050053 <a class="magick-nav-item " href="https://www.imagemagick.org/script/search.php">Search</a>
Cristy6fe3b072017-06-08 20:59:09 -040054 <a class="magick-nav-item float-right" href="https://www.imagemagick.org/discourse-server/">Community</a>
cristyd2432012015-05-04 23:15:15 +000055 </nav>
56 </div>
57</div>
58<div class="container">
59<div class="magick-header">
60<p class="text-center"><a href="fx.html#fx">The Fx Special Effects Image Operator</a><a href="fx.html#anatomy">The Anatomy of an Fx Expression</a></p>
cristy3ed852e2009-09-05 21:47:34 +000061
cristydcca3fa2010-12-29 21:50:07 +000062<a id="fx"></a>
cristy3ed852e2009-09-05 21:47:34 +000063
cristyd2432012015-05-04 23:15:15 +000064<p class="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>
cristy3ed852e2009-09-05 21:47:34 +000065
66<ul>
67 <li>create canvases, gradients, mathematical colormaps</li>
68 <li>move color values between images and channels</li>
69 <li>translate, flip, mirror, rotate, scale, shear and generally distort images</li>
70 <li>merge or composite multiple images together</li>
71 <li>convolve or merge neighboring pixels together</li>
72 <li>generate image metrics or 'fingerprints'</li>
73</ul>
74
75<p>The expression can be simple:</p>
76
cristyd2432012015-05-04 23:15:15 +000077<pre>
78convert -size 64x64 canvas:black -channel blue -fx "1/2" fx_navy.png
79</pre>
80
cristy3ed852e2009-09-05 21:47:34 +000081<p>Here, we convert a black to a navy blue image:</p>
82
cristyd2432012015-05-04 23:15:15 +000083<ul>
Cristy594d2772015-11-15 10:47:42 -050084 <a href="../images/black.png"><img src="../images/black.png" width="64" height="64" alt="black" /></a>
85 <img style="margin-top:22px; margin-bottom:22px;" src="../images/right.gif" width="20" height="20" alt="==>" />
86 <a href="../images/navy.png"><img src="../images/navy.png" width="64" height="64" alt="navy" /></a>
cristyd2432012015-05-04 23:15:15 +000087</ul>
cristy3ed852e2009-09-05 21:47:34 +000088
89<p>Or the expression can be complex:</p>
90
cristyd2432012015-05-04 23:15:15 +000091<pre>
92convert rose.jpg \
93 -fx "(1.0/(1.0+exp(10.0*(0.5-u)))-0.006693)*1.0092503" \
94 rose-sigmoidal.png'
95</pre>
96
cristy3ed852e2009-09-05 21:47:34 +000097<p>This expression results in a high contrast version of the image:</p>
98
cristyd2432012015-05-04 23:15:15 +000099<ul>
Cristy594d2772015-11-15 10:47:42 -0500100 <a href="../images/rose.jpg"><img src="../images/rose.jpg" width="70" height="46" alt="rose" /></a>
101 <img style="margin-top:13px; margin-bottom:13px;" src="../images/right.gif" width="20" height="20" alt="==>" />
102 <a href="../images/rose-sigmoidal.png"><img src="../images/rose-sigmoidal.png" width="70" height="46" alt="rose-sigmoidal" /></a>
cristyd2432012015-05-04 23:15:15 +0000103</ul>
cristy3ed852e2009-09-05 21:47:34 +0000104
105<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>
106
cristyd2432012015-05-04 23:15:15 +0000107<pre>
108convert -size 70x70 canvas: \
cristya1890f02015-05-15 23:32:44 +0000109 -fx "Xi=i-w/2; Yj=j-h/2; 1.2*(0.5-hypot(Xi,Yj)/70.0)+0.5"
110 radial-gradient.png'
cristyd2432012015-05-04 23:15:15 +0000111</pre>
112
cristy3ed852e2009-09-05 21:47:34 +0000113<p>The command above returns this image:</p>
114
cristyd2432012015-05-04 23:15:15 +0000115<ul>
Cristy594d2772015-11-15 10:47:42 -0500116 <a href="../images/radial-gradient.png"><img src="../images/radial-gradient.png" width="70" height="70" alt="radial-gradient" /></a>
cristyd2432012015-05-04 23:15:15 +0000117</ul>
cristye9a32c32011-04-07 01:11:05 +0000118
119<p>This FX expression adds random noise to an image:</p>
120
cristyd2432012015-05-04 23:15:15 +0000121<pre>
122convert photo.jpg -fx \'iso=32; rone=rand(); rtwo=rand(); \
123 myn=sqrt(-2*ln(rone))*cos(2*Pi*rtwo); myntwo=sqrt(-2*ln(rtwo))* \
124 cos(2*Pi*rone); pnoise=sqrt(p)*myn*sqrt(iso)* \
125 channel(4.28,3.86,6.68,0)/255; max(0,p+pnoise)\' noisy.png
126</pre>
127
Cristy364a72f2016-11-04 22:38:41 -0400128<p>See <a href="https://www.imagemagick.org/Usage/transform/index.html#fx">Using FX, The Special Effects Image Operator</a> for more examples.</p>
cristy3ed852e2009-09-05 21:47:34 +0000129
130<p>The next section discusses the Fx expression language.</p>
131
cristyd2432012015-05-04 23:15:15 +0000132<h2 class="magick-header"><a id="anatomy"></a>The Anatomy of an Fx Expression</h2>
cristy3ed852e2009-09-05 21:47:34 +0000133
134<h3>The Fx Expression Language</h3>
cristy3ed852e2009-09-05 21:47:34 +0000135
136<p>The formal Fx expression language is defined here:</p>
137
cristyd2432012015-05-04 23:15:15 +0000138<dl class="dl-horizontal">
139 <dt> numbers:</dt>
Cristy542697a2015-10-12 20:10:32 -0400140 <dd> integer, floating point, scientific notation (+/- required, e.g. 3.81469e-06), International System number postfixes (.e.g KB, Mib, GB, etc.)</dd>
cristyd2432012015-05-04 23:15:15 +0000141 <dt> constants: </dt>
cristyeed6cb62012-01-28 19:38:34 +0000142 <dd> E (Euler's number), Epsilon, QuantumRange, QuantumScale, Opaque, Phi (golden ratio), Pi, Transparent</dd>
cristyd2432012015-05-04 23:15:15 +0000143 <dt> Fx operators (in order of precedence): </dt>
cristyd92fed52010-10-25 13:48:19 +0000144 <dd> ^ (power), unary -, *, /, % (modulo), +, -,
cristy3ed852e2009-09-05 21:47:34 +0000145 &lt;&lt;, &gt;&gt;, &lt;, &lt;=, &gt;, &gt;=, ==, !=,
146 &amp; (bitwise AND), | (bitwise OR),
147 &amp;&amp; (logical AND), || (logical OR),
148 ~ (logical NOT), ?: (ternary conditional)</dd>
cristyd2432012015-05-04 23:15:15 +0000149 <dt> math functions: </dt>
Cristy1f1f3a02016-06-01 07:25:42 -0400150 <dd> abs(), acos(), acosh(), airy(), alt(), asin(), asinh(), atan(), atanh(), atan2(), ceil(), clamp(), cos(), cosh(), debug(), drc(), erf(), exp(), floor(), gauss(), gcd(), hypot(), int(), isnan(), j0(), j1(), jinc(), ln(), log(), logtwo(), max(), min(), mod(), not(), pow(), rand(), round(), sign(), sin(), sinc(), sinh(), sqrt(), squish(), tan(), tanh(), trunc()</dd>
cristyd2432012015-05-04 23:15:15 +0000151 <dt> channel functions: </dt>
cristy3ed852e2009-09-05 21:47:34 +0000152 <dd> channel(r,g,b,a), channel(c,m,y,k,a)</dd>
cristyd2432012015-05-04 23:15:15 +0000153 <dt> color names:</dt>
cristy3ed852e2009-09-05 21:47:34 +0000154 <dd> red, cyan, black, etc.</dd>
cristyd2432012015-05-04 23:15:15 +0000155 <dt> color functions:</dt>
cristy751980d2012-06-03 23:18:35 +0000156 <dd> srgb(), srgba(), rgb(), rgba(), cmyk(), cmyka(), hsl(), hsla(), etc.</dd>
cristyd2432012015-05-04 23:15:15 +0000157 <dt> color hex values:</dt>
cristy751980d2012-06-03 23:18:35 +0000158 <dd> #ccc, #cbfed0, #b9e1cc00, etc.</dd>
cristyd2432012015-05-04 23:15:15 +0000159 <dt> symbols:</dt><dd><dl>
Cristy594d2772015-11-15 10:47:42 -0500160 <li><code>u</code>=> first image in list</li>
161 <li><code>v</code>=> second image in list</li>
162 <li><code>s</code>=> current image in list (for %[fx:] otherwise = u)</li>
163 <li><code>t</code>=> index of current image (s) in list</li>
164 <li><code>n</code>=> number of images in list</li>
cristy3ed852e2009-09-05 21:47:34 +0000165
Cristy594d2772015-11-15 10:47:42 -0500166 <li><code>i</code>=> column offset</li>
167 <li><code>j</code>=> row offset</li>
168 <li><code>p</code>=> pixel to use (absolute or relative to current pixel)</li>
cristy3ed852e2009-09-05 21:47:34 +0000169
Cristy594d2772015-11-15 10:47:42 -0500170 <li><code>w</code>=> width of this image</li>
171 <li><code>h</code>=> height of this image</li>
172 <li><code>z</code>=> channel depth</li>
cristy3ed852e2009-09-05 21:47:34 +0000173
Cristy594d2772015-11-15 10:47:42 -0500174 <li><code>r</code>=> red value (from RGBA), of a specific or current pixel</li>
175 <li><code>g</code>=> green ''</li>
176 <li><code>b</code>=> blue ''</li>
177 <li><code>a</code>=> alpha ''</li>
178 <li><code>o</code>=> opacity ''</li>
cristy3ed852e2009-09-05 21:47:34 +0000179
Cristy594d2772015-11-15 10:47:42 -0500180 <li><code>c</code>=> cyan value of CMYK color of pixel</li>
181 <li><code>y</code>=> yellow ''</li>
182 <li><code>m</code>=> magenta ''</li>
183 <li><code>k</code>=> black ''</li>
cristy3ed852e2009-09-05 21:47:34 +0000184
Cristy594d2772015-11-15 10:47:42 -0500185 <li><code>intensity</code>=> pixel intensity</li>
cristy3ed852e2009-09-05 21:47:34 +0000186
Cristy594d2772015-11-15 10:47:42 -0500187 <li><code>hue</code>=> pixel hue</li>
188 <li><code>saturation</code>=> pixel saturation</li>
189 <li><code>lightness</code>=> pixel lightness</li>
190 <li><code>luma</code>=> pixel luma</li>
cristy3ed852e2009-09-05 21:47:34 +0000191
Cristy594d2772015-11-15 10:47:42 -0500192 <li><code>page.width</code>=> page width</li>
193 <li><code>page.height</code>=> page height</li>
194 <li><code>page.x</code>=> page x offset</li>
195 <li><code>page.y</code>=> page y offset</li>
cristy3ed852e2009-09-05 21:47:34 +0000196
Cristy594d2772015-11-15 10:47:42 -0500197 <li><code>resolution.x</code>=> x resolution</li>
198 <li><code>resolution.y</code>=> y resolution</li>
cristy3ed852e2009-09-05 21:47:34 +0000199
Cristy594d2772015-11-15 10:47:42 -0500200 <li><code>depth</code>=> image depth</li>
201 <li><code>minima</code>=> image minima</li>
202 <li><code>maxima</code>=> image maxima</li>
203 <li><code>mean</code>=> image mean</li>
204 <li><code>standard_deviation</code>=> image standard deviation</li>
205 <li><code>kurtosis</code>=> image kurtosis</li>
206 <li><code>skewness</code>=> image skewness (add a channel specifier to compute a statistic for that channel, e.g. depth.r)</li></dl></dd>
cristyd2432012015-05-04 23:15:15 +0000207 <dt> iterators:</dt>
cristyeed6cb62012-01-28 19:38:34 +0000208 <dd> while()</dd>
Cristy594d2772015-11-15 10:47:42 -0500209</dl>
cristy3ed852e2009-09-05 21:47:34 +0000210
cristy3ed852e2009-09-05 21:47:34 +0000211
212<h3>The Fx Expression</h3>
cristy3ed852e2009-09-05 21:47:34 +0000213
214<p>An Fx expression may include any combination of the following:</p>
cristyd2432012015-05-04 23:15:15 +0000215<dl class="dl-horizontal">
216<dt> <var>x</var> <code>^</code> <var>y</var></dt><dd> exponentiation (<var>x<sup>y</sup></var>)</dd>
217<dt> <code>(</code> ... <code>)</code></dt><dd> grouping</dd>
218<dt> <var>x</var> <code>*</code> <var>y</var></dt><dd> multiplication (the asterisk <code>*</code> is optional, for example, <code>2u</code> or <code>2(x+y)</code> are acceptable)</dd>
219<dt> <var>x</var> <code>/</code> <var>y</var></dt><dd> division</dd>
220<dt> <var>x</var> <code>%</code> <var>y</var></dt><dd> modulo</dd>
221<dt> <var>x</var> <code>+</code> <var>y</var></dt><dd> addition</dd>
222<dt> <var>x</var> <code>-</code> <var>y</var></dt><dd> subtraction</dd>
223<dt> <var>x</var> <code>&lt;&lt;</code> <var>y</var></dt><dd> left shift</dd>
224<dt> <var>x</var> <code>&gt;&gt;</code> <var>y</var></dt><dd> right shift</dd>
225<dt> <var>x</var> <code>&lt;</code> <var>y</var></dt><dd> boolean relation, return value 1.0 if <var>x</var> &lt; <var>y</var>, otherwise 0.0</dd>
226<dt> <var>x</var> <code>&lt;=</code> <var>y</var></dt><dd> boolean relation, return value 1.0 if <var>x</var> &lt;= <var>y</var>, otherwise 0.0</dd>
227<dt> <var>x</var> <code>&gt;</code> <var>y</var></dt><dd> boolean relation, return value 1.0 if <var>x</var> &gt; <var>y</var>, otherwise 0.0</dd>
228<dt> <var>x</var> <code>&gt;=</code> <var>y</var></dt><dd> boolean relation, return value 1.0 if <var>x</var> &gt;= <var>y</var>, otherwise 0.0</dd>
229<dt> <var>x</var> <code>==</code> <var>y</var></dt><dd> boolean relation, return value 1.0 if <var>x </var>==<var> y</var>, otherwise 0.0</dd>
230<dt> <var>x</var> <code>!=</code> <var>y</var></dt><dd> boolean relation, return value 1.0 if <var>x </var>!=<var> y</var>, otherwise 0.0</dd>
231<dt> <var>x</var> <code>&amp;</code> <var>y</var></dt><dd> binary AND</dd>
232<dt> <var>x</var> <code>|</code> <var>y</var></dt><dd> binary OR</dd>
233<dt> <var>x</var> <code>&amp;&amp;</code> <var>y</var></dt><dd> logical AND connective, return value 1.0 if <var>x</var> &gt; 0 and <var>y</var> &gt; 0, otherwise 0.0</dd>
234<dt> <var>x</var> <code>||</code> <var>y</var></dt><dd> logical OR connective (inclusive), return value 1.0 if <var>x</var> &gt; 0 or <var>y</var> &gt; 0 (or both), otherwise 0.0</dd>
235<dt> <code>~</code><var>x</var></dt><dd> logical NOT operator, return value 1.0 if <var>not</var> <var>x</var> &gt; 0, otherwise 0.0</dd>
236<dt> <code>+</code><var>x</var></dt><dd> unary plus, return 1.0*value</dd>
237<dt> <code>-</code><var>x</var></dt><dd> unary minus, return -1.0*value</dd>
238<dt> <var>x</var> <code>?</code> <var>y</var> </dt><dd><var>z</var>: 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>
239<dt> <var>x</var> <code>=</code> <var>y</var></dt><dd>assignment; assignment variables are restricted to letter combinations only (e.g. Xi not X1)</dd>
240<dt> <var>x</var> <code>;</code> <var>y</var></dt><dd>statement separator </dd>
241<dt> <code>phi</code></dt><dd> constant (1.618034...)</dd>
Cristyfe36f992016-12-29 20:24:13 -0500242<dt> <code>pi</code></dt><dd> constant (3.14159265359...)</dd>
cristyd2432012015-05-04 23:15:15 +0000243<dt> <code>e</code></dt><dd> constant (2.71828...)</dd>
244<dt> <code>QuantumRange</code></dt><dd> constant maximum pixel value (255 for Q8, 65535 for Q16)</dd>
245<dt> <code>QuantumScale</code></dt><dd> constant 1.0/<code>QuantumRange</code></dd>
246<dt> <code>intensity</code></dt><dd> pixel intensity; equivalent to 0.299*red+0.587*green+0.114*blue</dd>
247<dt> <code>hue</code></dt><dd> pixel hue</dd>
248<dt> <code>saturation</code></dt><dd> pixel saturation</dd>
249<dt> <code>lightness</code></dt><dd> pixel lightness; equivalent to 0.5*max(red,green,blue) + 0.5*min(red,green,blue)</dd>
250<dt> <code>luminance</code></dt><dd> pixel luminance; equivalent to 0.212656*red + 0.715158*green + 0.072186*blue</dd>
251<dt> <code>red, green, blue</code>, etc.</dt><dd> color names</dd>
252<dt> <code>#ccc, #cbfed0, #b9e1cc00</code>, etc.</dt><dd> color hex values</dd>
253<dt> <code>rgb(), rgba(), cmyk(), cmyka(), hsl(), hsla()</code></dt><dd> color functions</dd>
254<dt> <code>s, t, u, v, n, i, j, w, h, z, r, g, b, a, o, c, y, m, k</code></dt><dd> symbols</dd>
255<dt> <code>abs(</code><var>x</var><code>)</code></dt><dd> absolute value function</dd>
256<dt> <code>acos(</code><var>x</var><code>)</code></dt><dd> arc cosine function</dd>
257<dt> <code>acosh(</code><var>x</var><code>)</code></dt><dd> inverse hyperbolic cosine function</dd>
258<dt> <code>airy(</code><var>x</var><code>)</code></dt><dd> 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>
259<dt> <code>alt(</code><var>x</var><code>)</code></dt><dd> sign alternation function (return 1.0 if int(<var>x</var>) is even, -1.0 if int(<var>x</var>) is odd)</dd>
260<dt> <code>asin(</code><var>x</var><code>)</code></dt><dd> arc sine function</dd>
261<dt> <code>asinh(</code><var>x</var><code>)</code></dt><dd> inverse hyperbolic sine function</dd>
262<dt> <code>atan(</code><var>x</var><code>)</code></dt><dd> arc tangent function</dd>
263<dt> <code>atanh(</code><var>x</var><code>)</code></dt><dd> inverse hyperbolic tangent function</dd>
cristy29f2c972015-07-28 13:37:46 +0000264<dt> <code>atan2(</code><var>x</var>,<var>y</var><code>)</code></dt><dd> arc tangent function of two variables</dd>
cristyd2432012015-05-04 23:15:15 +0000265<dt> <code>ceil(</code><var>x</var><code>)</code></dt><dd>smallest integral value not less than argument</dd>
266<dt> <code>channel(</code><var>r</var>,<var>g</var>,<var>b</var>,<var>a</var><code>)</code></dt><dd>select numeric argument based on current channel</dd>
267<dt> <code>channel(</code><var>c</var>,<var>m</var>,<var>y</var>,<var>k</var>,<var>a</var><code>)</code></dt><dd>select numeric argument based on current channel</dd>
268<dt> <code>clamp(</code><var>x</var><code>)</code></dt><dd> clamp value</dd>
269<dt> <code>cos(</code><var>x</var><code>)</code></dt><dd> cosine function</dd>
270<dt> <code>cosh(</code><var>x</var><code>)</code></dt><dd> hyperbolic cosine function</dd>
271<dt> <code>debug(</code><var>x</var><code>)</code></dt><dd> print <var>x</var> (useful for debugging your expression)</dd>
272<dt> <code>drc(</code><var>x</var>,<var>y</var><code>)</code></dt><dd> dynamic range compression (knee curve); drc(<var>x</var>,<var>y</var>)=(<var>x</var>)/(<var>y</var>*(<var>x</var>-1)+1); -1&lt;<var>y</var>&lt;1 </dd>
Cristy1f1f3a02016-06-01 07:25:42 -0400273<dt> <code>erf(</code><var>x</var><code>)</code></dt><dd> error function</dd>
cristyd2432012015-05-04 23:15:15 +0000274<dt> <code>exp(</code><var>x</var><code>)</code></dt><dd> natural exponential function (<var>e<sup>x</sup></var>)</dd>
275<dt> <code>floor(</code><var>x</var><code>)</code></dt><dd> largest integral value not greater than argument</dd>
276<dt> <code>gauss(</code><var>x</var><code>)</code></dt><dd> gaussian function; gauss(<var>x</var>)=exp(<var>-x*x/2</var>)/sqrt(2*pi)</dd>
277<dt> <code>gcd(</code><var>x</var>,<var>y</var><code>)</code></dt><dd> greatest common denominator</dd>
278<dt> <code>hypot(</code><var>x</var>,<var>y</var><code>)</code></dt><dd> the square root of x<sup>2</sup>+y<sup>2</sup></dd>
279<dt> <code>int(</code><var>x</var><code>)</code></dt><dd> greatest integer function (return greatest integer less than or equal to <var>x</var>)</dd>
280<dt> <code>isnan(</code><var>x</var><code>)</code></dt><dd>return 1.0 if <var>x</var> is NAN, 0.0 otherwise</dd>
281<dt> <code>j0(</code><var>x</var><code>)</code></dt><dd> Bessel functions of <var>x</var> of the first kind of order 0</dd>
282<dt> <code>j1(</code><var>x</var><code>)</code></dt><dd> Bessel functions of <var>x</var> of the first kind of order 1</dd>
283<dt> <code>jinc(</code><var>x</var><code>)</code></dt><dd> jinc function (max=1, min=-0.1323); jinc(<var>x</var>)=2*j1(pi*<var>x</var>)/(pi*<var>*x</var>)</dd>
284<dt> <code>ln(</code><var>x</var><code>)</code></dt><dd> natural logarithm function</dd>
285<dt> <code>log(</code><var>x</var><code>)</code></dt><dd> logarithm base 10</dd>
286<dt> <code>logtwo(</code><var>x</var><code>)</code></dt><dd> logarithm base 2</dd>
287<dt> <code>ln(</code><var>x</var><code>)</code></dt><dd> natural logarithm</dd>
288<dt> <code>max(</code><var>x</var>, <var>y</var><code>)</code></dt><dd> maximum of <var>x</var> and <var>y</var></dd>
289<dt> <code>min(</code><var>x</var>, <var>y</var><code>)</code></dt><dd> minimum of <var>x</var> and <var>y</var></dd>
290<dt> <code>mod(</code><var>x</var>, <var>y</var><code>)</code></dt><dd> floating-point remainder function</dd>
291<dt> <code>not(</code><var>x</var><code>)</code></dt><dd> return 1.0 if <var>x</var> is zero, 0.0 otherwise</dd>
292<dt> <code>pow(</code><var>x</var>,<var>y</var><code>)</code></dt><dd> power function (<var>x<sup>y</sup></var>)</dd>
293<dt> <code>rand()</code></dt><dd> value uniformly distributed over the interval [0.0, 1.0) with a 2 to the 128th-1 period</dd>
294<dt> <code>round()</code></dt><dd> round to integral value, regardless of rounding direction</dd>
295<dt> <code>sign(</code><var>x</var><code>)</code></dt><dd> return -1.0 if <var>x</var> is less than 0.0 otherwise 1.0</dd>
296<dt> <code>sin(</code><var>x</var><code>)</code></dt><dd> sine function</dd>
297<dt> <code>sinc(</code><var>x</var><code>)</code></dt><dd> sinc function (max=1, min=-0.21); sinc(<var>x</var>)=sin(<var>pi*x</var>)/(<var>pi*x</var>)</dd>
298<dt> <code>squish(</code><var>x</var><code>)</code></dt><dd> squish function; squish(<var>x</var>)=1.0/(1.0+exp(<var>-x</var>))</dd>
299<dt> <code>sinh(</code><var>x</var><code>)</code></dt><dd> hyperbolic sine function</dd>
300<dt> <code>sqrt(</code><var>x</var><code>)</code></dt><dd> square root function</dd>
301<dt> <code>tan(</code><var>x</var><code>)</code></dt><dd> tangent function</dd>
302<dt> <code>tanh(</code><var>x</var><code>)</code></dt><dd> hyperbolic tangent function</dd>
303<dt> <code>trunc(</code><var>x</var><code>)</code></dt><dd> round to integer, towards zero</dd>
Cristy542697a2015-10-12 20:10:32 -0400304<dt> <code>while(</code><var>condition</var>,<var>expression</var><code>)</code></dt><dd> iterate while the condition is not equal to 0</dd>
cristyd2432012015-05-04 23:15:15 +0000305</dl>
Cristy594d2772015-11-15 10:47:42 -0500306<br/>
cristy3ed852e2009-09-05 21:47:34 +0000307<p>The expression semantics include these rules:</p>
308
309<ul>
310<li>symbols are case insensitive</li>
311<li>only one ternary conditional (e.g. x ? y : z) per statement</li>
312<li>statements are assignments or the final expression to return</li>
313<li>an assignment starts a statement, it is not an operator</li>
cristy8ee7f242013-06-20 16:08:44 +0000314<li>assignments to 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>
cristyb7b531e2010-10-24 01:41:05 +0000315<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>
cristyd2432012015-05-04 23:15:15 +0000316<li>Similarly, 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>
cristy3ed852e2009-09-05 21:47:34 +0000317</ul>
Cristy594d2772015-11-15 10:47:42 -0500318<br/>
cristy3ed852e2009-09-05 21:47:34 +0000319
cristyce66ab12010-10-21 23:36:32 +0000320
cristy3ed852e2009-09-05 21:47:34 +0000321<h3>Source Images</h3>
cristy3ed852e2009-09-05 21:47:34 +0000322
cristy8ee7f242013-06-20 16:08:44 +0000323<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>
cristy3ed852e2009-09-05 21:47:34 +0000324
cristy8ee7f242013-06-20 16:08:44 +0000325<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>
cristy3ed852e2009-09-05 21:47:34 +0000326
cristyd2432012015-05-04 23:15:15 +0000327<pre>
328convert image1.jpg image2.jpg image3.jpg -fx "(u+u[2])/2.0" image.jpg
329</pre>
330
Cristy542697a2015-10-12 20:10:32 -0400331<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>
cristy3ed852e2009-09-05 21:47:34 +0000332
cristy751980d2012-06-03 23:18:35 +0000333<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 <a href="command-line-options.html#channel">-channel red</a> instructs <a href="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>
cristy3ed852e2009-09-05 21:47:34 +0000334
cristyd2432012015-05-04 23:15:15 +0000335<pre>
336convert -channel red logo: -flop logo: -resize "20%" -fx "(u+v)/2" image.jpg
337</pre>
cristy3ed852e2009-09-05 21:47:34 +0000338
cristyd2432012015-05-04 23:15:15 +0000339<ul>
Cristy594d2772015-11-15 10:47:42 -0500340 <a href="../images/logo-sm-flop.png"><img src="../images/logo-sm-flop.png" width="128" height="96" alt="logo-sm-flop.png" /></a>
341 <a href="../images/logo-sm.png"><img src="../images/logo-sm.png" width="128" height="96" alt="logo-sm.png" /></a>
342<img style="margin-top:38px; margin-bottom:38px;" src="../images/right.gif" width="20" height="20" alt="==>" />
343 <a href="../images/logo-sm-fx.png"><img src="../images/logo-sm-fx.png" width="128" height="96" alt="logo-sm-fx.png" /></a>
cristyd2432012015-05-04 23:15:15 +0000344</ul>
cristy3ed852e2009-09-05 21:47:34 +0000345
Cristy594d2772015-11-15 10:47:42 -0500346<br/>
cristy3ed852e2009-09-05 21:47:34 +0000347<h3>Accessing Pixels</h3>
cristy3ed852e2009-09-05 21:47:34 +0000348
349<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>
350
cristy8ee7f242013-06-20 16:08:44 +0000351<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>
cristy3ed852e2009-09-05 21:47:34 +0000352
cristyd2432012015-05-04 23:15:15 +0000353<pre>
cristya1890f02015-05-15 23:32:44 +0000354p[-1].g green value of pixel to the immediate left of the current pixel
355p[-1,-1].r red value of the pixel diagonally left and up from current pixel
cristy3ed852e2009-09-05 21:47:34 +0000356</pre>
357
358<p>To specify an absolute position, use braces, rather than brackets.</p>
359
cristyd2432012015-05-04 23:15:15 +0000360<pre>
cristya1890f02015-05-15 23:32:44 +0000361p{0,0}.r red value of the pixel in the upper left corner of the image
362p{12,34}.b blue pixel value at column number 12, row 34 of the image
cristy3ed852e2009-09-05 21:47:34 +0000363</pre>
364
cristy751980d2012-06-03 23:18:35 +0000365<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 <a href="command-line-options.html#interpolate">-interpolate</a> setting.</p>
cristy3ed852e2009-09-05 21:47:34 +0000366
cristy751980d2012-06-03 23:18:35 +0000367<p>A position outside the boundary of the image retrieves a value dictated by the <a href="command-line-options.html#virtual-pixel">-virtual-pixel</a> option setting.</p>
cristy3ed852e2009-09-05 21:47:34 +0000368
cristy3ed852e2009-09-05 21:47:34 +0000369<h3>Apply an Expression to Select Image Channels</h3>
cristy3ed852e2009-09-05 21:47:34 +0000370
cristy8ee7f242013-06-20 16:08:44 +0000371<p>Use the <a href="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>
cristy3ed852e2009-09-05 21:47:34 +0000372
cristyd2432012015-05-04 23:15:15 +0000373<pre>
374convert alpha.png beta.png -channel red -fx "(u.g+v.g)/2" gamma.png
375</pre>
376
cristy3ed852e2009-09-05 21:47:34 +0000377
378<h3>Results</h3>
cristy3ed852e2009-09-05 21:47:34 +0000379
cristy8ee7f242013-06-20 16:08:44 +0000380<p>The <a href="command-line-options.html#fx">-fx</a> operator evaluates the given expression for each channel (set by <a href="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>
cristy3ed852e2009-09-05 21:47:34 +0000381
cristy8ee7f242013-06-20 16:08:44 +0000382<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>
cristy3ed852e2009-09-05 21:47:34 +0000383
384
cristyd2432012015-05-04 23:15:15 +0000385<p>For use with <a href="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 (<a href="command-line-options.html#channel">-channel</a> is ignored). An example:</p>
cristy3ed852e2009-09-05 21:47:34 +0000386
cristyd2432012015-05-04 23:15:15 +0000387<pre>
388convert canvas:'rgb(25%,50%,75%)' rose: -colorspace rgb \
389 -format 'Red channel of NW corner of image #%[fx:t] is %[fx:s]' info:
390Red channel of NW corner of image #0 is 0.453758
391Red channel of NW corner of image #1 is 0.184588
392</pre>
cristy3cd7cc92011-04-01 01:00:28 +0000393
cristyd2432012015-05-04 23:15:15 +0000394<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>
395
396<pre>
397convert rose: -duplicate 29 -virtual-pixel Gray -distort SRT '%[fx:360.0*t/n]' \
398 -set delay '%[fx:t == 0 ? 240 : 10]' -loop 0 rose.gif"
399</pre>
400
Cristyd07f4fe2017-06-02 20:36:40 -0400401<p>The color-escape <code>%[pixel:]</code> or <code>%[hex:]</code> is evaluated once per image and per color channel in that image (<a href="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>
cristy3ed852e2009-09-05 21:47:34 +0000402
403</div>
cristyd2432012015-05-04 23:15:15 +0000404 <footer class="magick-footer">
cristy29f2c972015-07-28 13:37:46 +0000405 <p><a href="support.html">Donate</a>
406 <a href="sitemap.html">Sitemap</a>
cristyd2432012015-05-04 23:15:15 +0000407 <a href="links.html">Related</a>
Cristye0779712016-07-30 17:25:22 -0400408 <a href="security-policy.html">Security</a>
cristy29f2c972015-07-28 13:37:46 +0000409 <a href="architecture.html">Architecture</a>
cristyd2432012015-05-04 23:15:15 +0000410</p>
411 <p><a href="fx.html#">Back to top</a>
cristyea2793e2015-05-31 16:52:26 +0000412 <a href="http://pgp.mit.edu:11371/pks/lookup?op=get&amp;search=0x89AB63D48277377A">Public Key</a>
Cristy6549abb2016-11-10 08:25:55 -0500413 <a href="https://www.imagemagick.org/script/contact.php">Contact Us</a></p>
Cristyfe36f992016-12-29 20:24:13 -0500414 <p><small>© 1999-2017 ImageMagick Studio LLC</small></p>
cristyd2432012015-05-04 23:15:15 +0000415 </footer>
416</div><!-- /.container -->
cristyad41c0c2012-10-31 00:49:28 +0000417
Cristyb7f455a2017-03-03 19:30:34 -0500418 <script src="https://localhost/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
Cristy1f1f3a02016-06-01 07:25:42 -0400419 <script src="../js/magick.html"></script>
cristyad41c0c2012-10-31 00:49:28 +0000420</div>
cristyad41c0c2012-10-31 00:49:28 +0000421</body>
cristy5a367e42015-05-05 12:39:18 +0000422</html>
Cristy5890de62017-06-10 13:25:17 -0400423<!-- Magick Cache 10th June 2017 13:15 -->