fx.html revision ba1758d71797bef0cbb262ab2ae7f5c7589d4f4a
1
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
3  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4<html version="-//W3C//DTD XHTML 1.1//EN"
5      xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
6      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7      xsi:schemaLocation="http://www.w3.org/1999/xhtml
8                          http://www.w3.org/MarkUp/SCHEMA/xhtml11.xsd">
9<head>
10  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
11  <meta name="verify-v1" content="g222frIIxcQTrvDR3NBRUSKP3AnMNoqxOkIniCEkV7U="/>
12  <title>ImageMagick: The Fx Special Effects Image Operator</title>
13  <meta http-equiv="Content-Language" content="en-US"/>
14  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
15  <meta http-equiv="Reply-to" content="magick-users@imagemagick.org"/>
16  <meta name="Application-name" content="ImageMagick"/>
17  <meta name="Description" content="Use ImageMagick to convert, edit, or compose bitmap images in a variety of formats.  In addition resize, rotate, shear, distort and transform images."/>
18  <meta name="Application-url" content="http://www.imagemagick.org"/>
19  <meta name="Generator" content="PHP"/>
20  <meta name="Keywords" content="the, fx, special, effects, image, operator, ImageMagick, ImageMagic, MagickCore, MagickWand, PerlMagick, Magick++, RMagick, PythonMagick, JMagick, TclMagick, Image, Magick, Magic, Wand, ImageMagickObject, Swiss, Army, Knife, Image, Processing"/>
21  <meta name="Rating" content="GENERAL"/>
22  <meta name="Robots" content="INDEX, FOLLOW"/>
23  <meta name="Generator" content="ImageMagick Studio LLC"/>
24  <meta name="Author" content="ImageMagick Studio LLC"/>
25  <meta name="Revisit-after" content="2 DAYS"/>
26  <meta name="Resource-type" content="document"/>
27  <meta name="Copyright" content="Copyright (c) 1999-2011 ImageMagick Studio LLC"/>
28  <meta name="Distribution" content="Global"/>
29  <link rel="icon" href="/images/wand.png"/>
30  <link rel="shortcut icon" href="/images/wand.ico"  type="images/x-icon"/>
31  <link rel="meta" type="application/rdf+xml" title="ICI" href="http://imagemagick.org/ici.rdf"/>
32  <link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" />
33  <style type="text/css" media="all">
34    @import url("/www/magick.css");
35  </style>
36  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
37  <script type="text/javascript" src="/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
38  <link rel="stylesheet" type="text/css" href="/fancybox/jquery.fancybox-1.3.4.css" media="screen" />
39  <script type="text/javascript">
40    $(document).ready(function() {
41      $("a[href$=.jpg],a[href$=.png],a[href$=.gif]").fancybox({
42        'transitionIn'  : 'elastic',
43        'transitionOut' : 'elastic',
44        'overlayShow'   : false,
45        'opacity'       : true
46      });
47    });
48  </script>
49</head>
50
51<body id="www-imagemagick-org">
52<div class="titlebar">
53<div style="margin: 17px auto; float: left;">
54  <script type="text/javascript">
55  <!--
56    google_ad_client = "pub-3129977114552745";
57    google_ad_slot = "5439289906";
58    google_ad_width = 728;
59    google_ad_height = 90;
60  //-->
61  </script>
62  <script type="text/javascript"
63    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
64  </script>
65</div>
66<a href="http://www.imagemagick.org/discourse-server/">
67  <img src="/images/logo.jpg"
68  alt="ImageMagick Logo"
69  style="width: 123px; height: 118px; border: 0px; float: right;" /></a>
70<a href="/index.html">
71  <img src="/images/sprite.jpg"
72  alt="ImageMagick Sprite"
73  style="width: 114px; height: 118px; border: 0px; float: right;" /></a>
74</div>
75
76<div class="westbar">
77
78<div class="menu">
79  <a title="About ImageMagick" href="/index.html">About ImageMagick</a>
80</div>
81<div class="sep"></div>
82<div class="menu">
83  <a title="Binary Releases" href="/www/binary-releases.html">Binary Releases</a>
84</div>
85<div class="sub">
86    <a title="Binary Release: Unix" href="/www/binary-releases.html#unix">Unix</a>
87</div>
88<div class="sub">
89    <a title="Binary Release: MacOS X" href="/www/binary-releases.html#macosx">Mac OS X</a>
90</div>
91<div class="sub">
92    <a title="Binary Release: iPhone" href="/www/binary-releases.html#iPhone">iPhone</a>
93</div>
94<div class="sub">
95    <a title="Binary Release: Windows" href="/www/binary-releases.html#windows">Windows</a>
96</div>
97<div class="sep"></div>
98<div class="menu">
99  <a title="Command-line Tools" href="/www/command-line-tools.html">Command-line Tools</a>
100</div>
101<div class="sub">
102    <a title="Command-line Tools: Processing" href="/www/command-line-processing.html">Processing</a>
103</div>
104<div class="sub">
105    <a title="Command-line Tools: Options" href="/www/command-line-options.html">Options</a>
106</div>
107<div class="sub">
108    <a title="Command-line Tools: Usage" href="http://www.imagemagick.org/Usage/">Usage</a>
109</div>
110<div class="menu">
111  <a title="Program Interfaces" href="/www/api.html">Program Interfaces</a>
112</div>
113<div class="sub">
114    <a title="Program Interface: MagickWand" href="/www/magick-wand.html">MagickWand</a>
115</div>
116<div class="sub">
117    <a title="Program Interface: MagickCore" href="/www/magick-core.html">MagickCore</a>
118</div>
119<div class="sub">
120    <a title="Program Interface: PerlMagick" href="/www/perl-magick.html">PerlMagick</a>
121</div>
122<div class="sub">
123    <a title="Program Interface: Magick++" href="/www/magick++.html">Magick++</a>
124</div>
125<div class="sep"></div>
126<div  class="menu">
127   <a title="Install from Source" href="/www/install-source.html">Install from Source</a>
128</div>
129<div class="sub">
130    <a title="Install from Source: Unix" href="/www/install-source.html#unix">Unix</a>
131</div>
132<div class="sub">
133    <a title="Install from Source: Windows" href="/www/install-source.html#windows">Windows</a>
134 </div>
135<div class="menu">
136  <a title="Resources" href="/www/resources.html">Resources</a>
137</div>
138<div class="menu">
139  <a title="Architecture" href="/www/architecture.html">Architecture</a>
140</div>
141<div class="menu">
142  <a title="Download" href="/www/download.html">Download</a>
143</div>
144<div class="sep"></div>
145<div class="menu">
146  <a title="Search" href="/www/search.html">Search</a>
147</div>
148<div class="sep"></div>
149<div class="menu">
150  <a title="Site Map" href="/www/sitemap.html">Site Map</a>
151</div>
152<div  class="sub">
153  <a title="Site Map: Links" href="/www/links.html">Links</a>
154</div>
155<div class="sep"></div>
156<div  class="menu">
157  <a rel="follow" title="Sponsors" href="/www/sponsors.html">Sponsors:</a>
158
159<a href="http://www.networkredux.com">
160  <img src="/images/networkredux.png" alt="[sponsor]"
161  style="margin-top: 4px; margin-left: 4px; border: 0px; float: left;" /></a>
162<div class="sponsbox">
163<div  class="sponsor">
164  <a rel="follow" title="Sponsor: Web Hosting Deals" href="http://www.webhostingdeals.org">Web Hosting Deals</a><!-- 201111010270 chee.hoa darin -->
165</div>
166<div  class="sponsor">
167  <a rel="follow" title="Sponsor: Web Hosting Reviews" href="http://www.webhostingsecretrevealed.com">Web Hosting Secret Revealed</a><!-- 201204010540 iamchee jerry -->
168</div>
169<div  class="sponsor">
170  <a rel="follow" title="Sponsor: Druckerei" href="http://print24.com/de/">Druckerei</a><!-- 201110010720 -->
171</div>
172<div  class="sponsor">
173  <a rel="follow" title="Sponsor: Web Hosting Break" href="http://www.webhostingbreak.com">Web Hosting Break</a><!-- 201109010090 ian@ian-m... -->
174</div>
175<div  class="sponsor">
176  <a rel="follow" title="Sponsor: Druckerei" href="http://www.allesdruck.de">Druckerei</a><!-- 201303011500 r.leo -->
177</div>
178<div  class="sponsor">
179  <a rel="follow" title="Sponsor: Website Hosting" href="http://www.hostreviewgeeks.com">Website Hosting</a><!-- 201110010090 alexanian media -->
180</div>
181<div  class="sponsor">
182  <a rel="follow" title="Sponsor: Hotel München" href="http://www.messehotel-erb-muenchen.de">Hotel München</a><!-- 201111010450 cerb -->
183</div>
184<div  class="sponsor">
185  <a rel="follow" title="Sponsor: Best Web Hosting" href="http://webhostinggeeks.com">Best Web Hosting</a><!-- 201110010720 -->
186</div>
187<div  class="sponsor">
188  <a rel="follow" title="Sponsor: Flyer drucken" href="http://www.online-druck.biz">Flyer drucken</a><!-- 201109010900 Floeter-->
189</div>
190<div  class="sponsor">
191  <a rel="follow" title="Sponsor: Web Hosting Reviews" href="http://www.webhostingmasters.com">Web Hosting Reviews</a><!-- 201107011500 affliatelabel -->
192</div>
193<div  class="sponsor">
194  <a rel="follow" title="Sponsor: Web Hosting Ratings" href="http://webhostingrating.com">Web Hosting Ratings</a><!-- 201110010720 -->
195</div>
196<div  class="sponsor">
197   <a rel="follow" title="Sponsor: Free Catalogs" href="http://www.who-sells-it.com/">Free Catalogs</a><!-- 20120801000600 -->
198</div>
199</div>
200</div>
201</div>
202
203<div class="eastbar">
204
205</div>
206
207<div class="main">
208
209<h1>Apply Special Effects to an Image with an Fx Expression</h1>
210<p class="navigation-index">[<a href="#fx">The Fx Special Effects Image Operator</a> &bull; <a href="#anatomy">The Anatomy of an Fx Expression</a>]</p>
211
212<a id="fx"></a>
213<div class="doc-section">
214
215<p>Use the Fx special effects image operator to apply a mathematical expression to an image or image channels.  Use Fx to:</p>
216
217<ul>
218  <li>create canvases, gradients, mathematical colormaps</li>
219  <li>move color values between images and channels</li>
220  <li>translate, flip, mirror, rotate, scale, shear and generally distort images</li>
221  <li>merge or composite multiple images together</li>
222  <li>convolve or merge neighboring pixels together</li>
223  <li>generate image metrics or 'fingerprints'</li>
224</ul>
225
226<p>The expression can be simple:</p>
227
228<p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert -size 64x64 canvas:black -channel blue -fx "1/2" fx_navy.png</span></p>
229<p>Here, we convert a black to a navy blue image:</p>
230
231<p class="image">
232  <a href="/images/black.png"><img src="/images/black.png" width="64" height="64" alt="black" /></a>
233  <img style="margin-top:22px; margin-bottom:22px;" src="/images/right.gif" width="20" height="20" alt="==>" />
234  <a href="/images/navy.png"><img src="/images/navy.png" width="64" height="64" alt="navy" /></a>
235</p>
236
237<p>Or the expression can be complex:</p>
238
239<p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert rose.jpg \  <br/>
240          -fx "(1.0/(1.0+exp(10.0*(0.5-u)))-0.006693)*1.0092503" \ <br/>
241          rose-sigmoidal.png</span></p>
242<p>This expression results in a high contrast version of the image:</p>
243
244<p class="image">
245  <a href="/images/rose.jpg"><img src="/images/rose.jpg" width="70" height="46" alt="rose" /></a>
246  <img style="margin-top:13px; margin-bottom:13px;" src="/images/right.gif" width="20" height="20" alt="==>" />
247  <a href="/images/rose-sigmoidal.png"><img src="/images/rose-sigmoidal.png" width="70" height="46" alt="rose-sigmoidal" /></a>
248</p>
249
250<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>
251
252<p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert -size 70x70 canvas: \  <br/>
253          -fx "Xi=i-w/2; Yj=j-h/2; 1.2*(0.5-hypot(Xi,Yj)/70.0)+0.5" \ <br/>
254          radial-gradient.png</span></p>
255<p>The command above returns this image:</p>
256
257<p class="image">
258  <a href="/images/radial-gradient.png"><img src="/images/radial-gradient.png" width="70" height="70" alt="radial-gradient" /></a>
259</p>
260
261<p>This FX expression adds random noise to an image:</p>
262
263<p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert photo.jpg -fx 'iso=32; rone=rand(); rtwo=rand(); \<br />
264     myn=sqrt(-2*ln(rone))*cos(2*Pi*rtwo); myntwo=sqrt(-2*ln(rtwo))* \<br />
265     cos(2*Pi*rone); pnoise=sqrt(p)*myn*sqrt(iso)* \ <br />
266     channel(4.28,3.86,6.68,0)/255; max(0,p+pnoise)' noisy.png</span></p>
267<p>See <a href="http://www.imagemagick.org/Usage/transform/index.html#fx">Using FX, The Special Effects Image Operator</a> for more examples.</p>
268
269<p>The next section discusses the Fx expression language.</p>
270
271</div>
272
273<h2><a id="anatomy"></a>The Anatomy of an Fx Expression</h2>
274<div class="doc-section">
275
276<h3>The Fx Expression Language</h3>
277<div class="doc-section">
278
279<p>The formal Fx expression language is defined here:</p>
280
281<dl class="doc">
282  <dt class="doc"> numbers:</dt>
283  	<dd> integer, floating point, or scientfic notation (+/- required, e.g. 3.81469e-06)</dd>
284  <dt class="doc"> constants: </dt>
285    <dd> e, Epsilon, QuantumRange, QuantumScale, Opaque, Pi, Transparent</dd>
286  <dt class="doc"> Fx operators (in order of precedence): </dt>
287     <dd> ^ (power), unary -, *, /, % (modulo), +, -,
288     &lt;&lt;, &gt;&gt;, &lt;, &lt;=, &gt;, &gt;=, ==, !=,
289     &amp; (bitwise AND),   | (bitwise OR),
290     &amp;&amp; (logical AND),  || (logical OR),
291     ~ (logical NOT),  ?: (ternary conditional)</dd>
292  <dt class="doc"> math functions: </dt>
293     <dd> abs(), acos(), airy(), alt(), asin(), atan(), atan2(), ceil(), cos(), cosh(), debug(), exp(), floor(),
294     hypot(), int(), j0(), j1(), jinc(), ln(), log(), logtwo(), max(), min(), mod(), pow(), rand(), round(),
295     sign(), sin(), sinc(), sinh(), sqrt(), tan(), tanh(), trunc()</dd>
296  <dt class="doc"> channel functions: </dt>
297    <dd> channel(r,g,b,a), channel(c,m,y,k,a)</dd>
298  <dt class="doc"> color names:</dt>
299    <dd> red, cyan, black, etc.</dd>
300  <dt class="doc"> color functions:</dt>
301    <dd> rgb(), rgba(), cmyk(), cmyka(), hsl(), hsla()</dd>
302  <dt class="doc"> color hex values:</dt>
303    <dd> #ccc, #cbfed0, #b9e1cc00</dd>
304  <dt class="doc"> symbols:</dt>
305     <dd style="white-space:pre">
306     s = current image in sequence
307     t = scene number of current image in sequence
308     u = first image in sequence
309     v = second image in sequence
310     n = number of images in sequence
311
312     i = column offset
313     j = row offset
314     p = pixel to use (absolute or relative to current pixel)
315
316     w = width of this image
317     h = height of this image
318     z = channel depth
319
320     r = red value (from RGBA), of a specific or current pixel
321     g = green    ''
322     b = blue     ''
323     a = alpha    ''
324     o = opacity  ''
325
326     c = cyan value of CMYK color of pixel
327     y = yellow   ''
328     m = magenta  ''
329     k = black    ''
330
331     intensity = pixel intensity
332
333     hue = pixel hue
334     saturation = pixel saturation
335     lightness = pixel lightness
336     luminance = pixel luminance
337
338     page.width = page width
339     page.height = page height
340     page.x = page x offset
341     page.y = page y offset
342
343     resolution.x = x resolution
344     resolution.y = y resolution
345
346     depth = image depth
347     minima = image minima
348     maxima = image maxima
349     mean = image mean
350     standard_deviation = image standard deviation
351     kurtosis = image kurtosis
352     skewness = image skewness
353       (add a channel specifier to compute a statistic for that channel, e.g. depth.r)
354     </dd>
355</dl>
356
357</div>
358
359<h3>The Fx Expression</h3>
360<div class="doc-section">
361
362<p>An Fx expression may include any combination of the following:</p>
363<ul>
364<li> <em>x</em> <kbd>^</kbd> <em>y</em>:  exponentiation (<em>x<sup>y</sup></em>)</li>
365<li> <kbd>(</kbd> ... <kbd>)</kbd>:  grouping</li>
366<li> <em>x</em> <kbd>*</kbd> <em>y</em>:  multiplication (the asterisk <kbd>*</kbd> is optional, for example, <kbd>2u</kbd> or <kbd>2(x+y)</kbd> are acceptable)</li>
367<li> <em>x</em> <kbd>/</kbd> <em>y</em>:  division</li>
368<li> <em>x</em> <kbd>%</kbd> <em>y</em>:  modulo</li>
369<li> <em>x</em> <kbd>+</kbd> <em>y</em>:  addition</li>
370<li> <em>x</em> <kbd>-</kbd> <em>y</em>:  subtraction</li>
371<li> <em>x</em> <kbd>&lt;&lt;</kbd> <em>y</em>:  left shift</li>
372<li> <em>x</em> <kbd>&gt;&gt;</kbd> <em>y</em>:  right shift</li>
373<li> <em>x</em> <kbd>&lt;</kbd> <em>y</em>:  boolean relation, return value 1.0 if <em>x</em> &lt; <em>y</em>,  otherwise 0.0</li>
374<li> <em>x</em> <kbd>&lt;=</kbd> <em>y</em>:  boolean relation, return value 1.0 if <em>x</em> &lt;= <em>y</em>,  otherwise 0.0</li>
375<li> <em>x</em> <kbd>&gt;</kbd> <em>y</em>:  boolean relation, return value 1.0 if <em>x</em> &gt; <em>y</em>,  otherwise 0.0</li>
376<li> <em>x</em> <kbd>&gt;=</kbd> <em>y</em>:  boolean relation, return value 1.0 if <em>x</em> &gt;= <em>y</em>,  otherwise 0.0</li>
377<li> <em>x</em> <kbd>==</kbd> <em>y</em>:  boolean relation, return value 1.0 if <em>x </em>==<em> y</em>, otherwise 0.0</li>
378<li> <em>x</em> <kbd>!=</kbd> <em>y</em>:  boolean relation, return value 1.0 if <em>x </em>!=<em> y</em>, otherwise 0.0</li>
379<li> <em>x</em> <kbd>&amp;</kbd> <em>y</em>:  binary AND</li>
380<li> <em>x</em> <kbd>|</kbd> <em>y</em>:  binary OR</li>
381<li> <em>x</em> <kbd>&amp;&amp;</kbd> <em>y</em>:  logical AND connective, return value 1.0 if <em>x</em> &gt; 0 and <em>y</em> &gt; 0,  otherwise 0.0</li>
382<li> <em>x</em> <kbd>||</kbd> <em>y</em>:  logical OR connective (inclusive), return value 1.0 if <em>x</em> &gt; 0 or <em>y</em> &gt; 0 (or both),  otherwise 0.0</li>
383<li> <kbd>~</kbd><em>x</em>:  logical NOT operator, return value 1.0 if <em>not</em> <em>x</em> &gt; 0,  otherwise 0.0</li>
384<li> <kbd>+</kbd><em>x</em>:  unary plus, return 1.0*value</li>
385<li> <kbd>-</kbd><em>x</em>:  unary minus, return -1.0*value</li>
386<li> <em>x</em> <kbd>?</kbd> <em>y</em> : <em>z</em>: ternary conditional expression, return value <em>y</em> if <em>x</em> != 0, otherwise <em>z</em>; only one ternary conditional permitted per statement</li>
387<li> <em>x</em> <kbd>=</kbd> <em>y</em>: assignment; assignment variables are restricted to letter combinations only (e.g. Xi not X1)</li>
388<li> <em>x</em> <kbd>;</kbd> <em>y</em>: statement separator </li>
389<li> <kbd>pi</kbd>:  constant (3.141659...)</li>
390<li> <kbd>e</kbd>:  constant (2.71828...)</li>
391<li> <kbd>QuantumRange</kbd>:  constant maximum pixel value (255 for Q8, 65535 for Q16)</li>
392<li> <kbd>QuantumScale</kbd>:  constant 1.0/<kbd>QuantumRange</kbd></li>
393<li> <kbd>intensity</kbd>:  pixel intensity; equivalent to 0.299*red+0.587*green+0.114*blue</li>
394<li> <kbd>hue</kbd>:  pixel hue</li>
395<li> <kbd>saturation</kbd>:  pixel saturation</li>
396<li> <kbd>lightness</kbd>:  pixel lightness; equivalent to 0.5*max(red,green,blue) + 0.5*min(red,green,blue)</li>
397<li> <kbd>luminance</kbd>:  pixel luminance; equivalent to 0.2126*red + 0.7152*green + 0.0722*blue</li>
398<li> <kbd>red, green, blue</kbd>, etc.:  color names</li>
399<li> <kbd>#ccc, #cbfed0, #b9e1cc00</kbd>, etc.:  color hex values</li>
400<li> <kbd>rgb(), rgba(), cmyk(), cmyka(), hsl(), hsla()</kbd>:  color functions</li>
401<li> <kbd>s, t, u, v, n, i, j, w, h, z, r, g, b, a, o, c, y, m, k</kbd>:  symbols</li>
402<li> <kbd>abs(</kbd><em>x</em><kbd>)</kbd>:  absolute value function</li>
403<li> <kbd>acos(</kbd><em>x</em><kbd>)</kbd>:  arc cosine function</li>
404<li> <kbd>airy(</kbd><em>x</em><kbd>)</kbd>:  Airy function (max=1, min=0); <kbd>airy(</kbd><em>x</em><kbd>)</kbd>=<kbd>[jinc(</kbd><em>x</em><kbd>)]<sup>2</sup></kbd>=<kbd>[2*j1(</kbd><em>pi*x</em><kbd>)/(</kbd><em>pi*x</em><kbd>)]<sup>2</sup></kbd></li>
405<li> <kbd>alt(</kbd><em>x</em><kbd>)</kbd>:  sign alternation function (return 1.0 if <kbd>int(</kbd><em>x</em><kbd>)</kbd> is even, -1.0 if <kbd>int(</kbd><em>x</em><kbd>)</kbd> is odd)</li>
406<li> <kbd>asin(</kbd><em>x</em><kbd>)</kbd>:  arc sine function</li>
407<li> <kbd>atan(</kbd><em>x</em><kbd>)</kbd>:  arc tangent function</li>
408<li> <kbd>atan2(</kbd><em>y</em>,<em>x</em><kbd>)</kbd>:  arc tangent function of two variables</li>
409<li> <kbd>ceil(</kbd><em>x</em><kbd>)</kbd>: smallest integral value not less than argument</li>
410<li> <kbd>channel(</kbd><em>r</em>,<em>g</em>,<em>b</em>,<em>a</em><kbd>)</kbd>: select numeric argument based on current channel</li>
411<li> <kbd>channel(</kbd><em>c</em>,<em>m</em>,<em>y</em>,<em>k</em>,<em>a</em><kbd>)</kbd>: select numeric argument based on current channel</li>
412<li> <kbd>cos(</kbd><em>x</em><kbd>)</kbd>:  cosine function</li>
413<li> <kbd>cosh(</kbd><em>x</em><kbd>)</kbd>:  hyperbolic cosine function</li>
414<li> <kbd>debug(</kbd><em>x</em><kbd>)</kbd>:  print <em>x</em> (useful for debugging your expression)</li>
415<li> <kbd>exp(</kbd><em>x</em><kbd>)</kbd>:  natural exponential function (<em>e<sup>x</sup></em>)</li>
416<li> <kbd>floor(</kbd><em>x</em><kbd>)</kbd>:  largest integral value not greater than argument</li>
417<li> <kbd>hypot(</kbd><em>x</em>,<em>y</em><kbd>)</kbd>:  the square root of x<sup>2</sup>+y<sup>2</sup></li>
418<li> <kbd>int(</kbd><em>x</em><kbd>)</kbd>:  greatest integer function (return greatest integer less than or equal to <em>x</em>)</li>
419<li> <kbd>j0(</kbd><em>x</em><kbd>)</kbd>:  Bessel functions of <em>x</em> of the first kind of order 0</li>
420<li> <kbd>j1(</kbd><em>x</em><kbd>)</kbd>:  Bessel functions of <em>x</em> of the first kind of order 1</li>
421<li> <kbd>jinc(</kbd><em>x</em><kbd>)</kbd>:  jinc function (max=1, min=-0.1323); <kbd>jinc(</kbd><em>x</em><kbd>)</kbd>=<kbd>2*j1(</kbd><em>pi*x</em><kbd>)/(</kbd><em>pi*x</em><kbd>)</kbd></li>
422<li> <kbd>ln(</kbd><em>x</em><kbd>)</kbd>:  natural logarithm function</li>
423<li> <kbd>log(</kbd><em>x</em><kbd>)</kbd>:  logarithm base 10</li>
424<li> <kbd>logtwo(</kbd><em>x</em><kbd>)</kbd>:  logarithm base 2</li>
425<li> <kbd>ln(</kbd><em>x</em><kbd>)</kbd>:  natural logarithm</li>
426<li> <kbd>max(</kbd><em>x</em>, <em>y</em><kbd>)</kbd>:  maximum of <em>x</em> and <em>y</em></li>
427<li> <kbd>min(</kbd><em>x</em>, <em>y</em><kbd>)</kbd>:  minimum of <em>x</em> and <em>y</em></li>
428<li> <kbd>mod(</kbd><em>x</em>, <em>y</em><kbd>)</kbd>:  floating-point remainder function</li>
429<li> <kbd>pow(</kbd><em>x</em>,<em>y</em><kbd>)</kbd>:  power function (<em>x<sup>y</sup></em>)</li>
430<li> <kbd>rand()</kbd>:  value uniformly distributed over the interval [0.0, 1.0) with a 2 to the 128th-1 period</li>
431<li> <kbd>round()</kbd>:  round to integral value, regardless of rounding direction</li>
432<li> <kbd>sign(</kbd><em>x</em><kbd>)</kbd>:  return -1.0 if <em>x</em> is less than 0.0 otherwise 1.0</li>
433<li> <kbd>sin(</kbd><em>x</em><kbd>)</kbd>:  sine function</li>
434<li> <kbd>sinc(</kbd><em>x</em><kbd>)</kbd>:  sinc function (max=1, min=-0.21); <kbd>sinc(</kbd><em>x</em><kbd>)</kbd>=<kbd>sin(</kbd><em>pi*x</em><kbd>)/(</kbd><em>pi*x</em><kbd>)</kbd></li>
435<li> <kbd>sinh(</kbd><em>x</em><kbd>)</kbd>:  hyperbolic sine function</li>
436<li> <kbd>sqrt(</kbd><em>x</em><kbd>)</kbd>:  square root function</li>
437<li> <kbd>tan(</kbd><em>x</em><kbd>)</kbd>:  tangent function</li>
438<li> <kbd>tanh(</kbd><em>x</em><kbd>)</kbd>:  hyperbolic tangent function</li>
439<li> <kbd>trunc(</kbd><em>x</em><kbd>)</kbd>:  round to integer, towards zero</li>
440</ul>
441
442<p>The expression semantics include these rules:</p>
443
444<ul>
445<li>symbols are case insensitive</li>
446<li>only one ternary conditional (e.g. x ? y : z) per statement</li>
447<li>statements are assignments or the final expression to return</li>
448<li>an assignment starts a statement, it is not an operator</li>
449<li>assignments to built-ins do not throw an exception and have no effect;  e.g. <kbd>r=3.0; r</kbd> returns the pixel red color value, not 3.0</li>
450<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>
451<li>Similarly, care must be exercised when using the slash ('/') symbol. The string of characters <em>1/2x</em> 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>
452</ul>
453
454
455</div>
456
457<h3>Source Images</h3>
458<div class="doc-section">
459
460<p>The symbols <kbd>u</kbd> and <kbd>v</kbd> 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 <kbd>u</kbd>), with a zero index for the beginning of the sequence. A negative index counts from the end.  For example, <kbd>u[0]</kbd> is the first image in the sequence, <kbd>u[2]</kbd> is the third, <kbd>u[-1]</kbd> is the last image, and <kbd>u[t]</kbd> is the current image. The current image can also be referenced by <kbd>s</kbd>. If the sequence number exceeds the length of the sequence, the count is wrapped. Thus in a 3-image sequence,  <kbd>u[-1]</kbd>, <kbd>u[2]</kbd>, and <kbd>u[5]</kbd> all refer to the same (third) image.</p>
461
462<p>As an example, we form an image by averaging the first image and third images:</p>
463
464<p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert image1.jpg image2.jpg image3.jpg -fx "(u+u[2])/2.0" image.jpg</span></p>
465<p>By default, the image to which <kbd>p</kbd>, <kbd>r</kbd>, <kbd>g</kbd>, <kbd>b</kbd>, <kbd>a</kbd>, etc., are applied is the first image <kbd>u</kbd> in the image list, which may also be referenced using <kbd>s</kbd>. </p>
466
467<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="/www/command-line-options.html#channel">-channel red</a> instructs <a href="/www/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>
468
469<p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert -channel red logo: -flop logo: -resize "20%"  -fx "(u+v)/2" image.jpg</span></p>
470<div class="image">
471  <a href="/images/logo-sm-flop.png"><img src="/images/logo-sm-flop.png" width="128" height="96" alt="logo-sm-flop.png" /></a>
472  <a href="/images/logo-sm.png"><img src="/images/logo-sm.png" width="128" height="96" alt="logo-sm.png" /></a>
473<img style="margin-top:38px; margin-bottom:38px;" src="/images/right.gif" width="20" height="20" alt="==>" />
474  <a href="/images/logo-sm-fx.png"><img src="/images/logo-sm-fx.png" width="128" height="96" alt="logo-sm-fx.png" /></a>
475</div>
476
477
478</div>
479
480<h3>Accessing Pixels</h3>
481<div class="doc-section">
482
483<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>
484
485<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 <kbd>p</kbd>. For example,</p>
486
487<pre class="text">
488   p[-1].g      green value of pixel to the immediate left of the current pixel
489   p[-1,-1].r   red value of the pixel diagonally left and up from current pixel
490</pre>
491
492<p>To specify an absolute position, use braces, rather than brackets.</p>
493
494<pre class="text">
495  p{0,0}.r     red value of the pixel in the upper left corner of the image
496  p{12,34}.b   blue pixel value at column number 12, row 34 of the image
497</pre>
498
499<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="/www/command-line-options.html#interpolate">-interpolate</a> setting.</p>
500
501<p>A position outside the boundary of the image retrieves a value dictated by the <a href="/www/command-line-options.html#virtual-pixel">-virtual-pixel</a> option setting.</p>
502
503</div>
504
505<h3>Apply an Expression to Select Image Channels</h3>
506<div class="doc-section">
507
508<p>Use the <a href="/www/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 <kbd>alpha.png</kbd> with the average of the green channels from the images <kbd>alpha.png</kbd> and <kbd>beta.png</kbd>, use:</p>
509
510<p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert alpha.png beta.png -channel red -fx "(u.g+v.g)/2" gamma.png</span></p>
511</div>
512
513<h3>Results</h3>
514<div class="doc-section">
515
516<p>The <a href="/www/command-line-options.html#fx">-fx</a> operator evaluates the given expression for each channel (set by <a href="/www/command-line-options.html#channel">-channel</a>) of each pixel in the first image (<kbd>u</kbd>) 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 <kbd>alpha.png</kbd> replaces both of the original images, <kbd>alpha.png</kbd> and <kbd>beta.png</kbd>, before being saved as <kbd>gamma.png</kbd>.</p>
517
518<p>The current image <kbd>s</kbd> is set to the first image in the sequence (<kbd>u</kbd>), and <kbd>t</kbd> to its index, 0.  The symbols <kbd>i</kbd> and <kbd>j</kbd> reference the current pixel being processed.</p>
519
520
521<p>For use with <a href="/www/command-line-options.html#format_identify_">-format</a>, the value-escape <kbd>%[fx: ]</kbd> is evaluated just once for each image in the current image sequence. As each image in the sequence is being evaluated, <kbd>s</kbd> and <kbd>t</kbd> successively refer to the current image and its index, while <kbd>i</kbd> and <kbd>j</kbd> are set to zero, and the current channel set to red (<a href="/www/command-line-options.html#channel">-channel</a> is ignored). An example:</p>
522
523<p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert  canvas:'rgb(25%,50%,75%)' rose: -colorspace rgb  \ <br/>
524  -format 'Red channel of NW corner of image #%[fx:t] is %[fx: s]' info:</span><span class='crtout'>Red channel of NW corner of image #0 is 0.453758 <br/>
525  Red channel of NW corner of image #1 is 0.184588</span></p>
526<p>Here we use the image indexes to <em>rotate</em> each image differently, and use <kbd>-set</kbd> with the image index to set a different <em>pause delay</em> on the first image in the animation:</p>
527
528<p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert rose: -duplicate 29 -virtual-pixel Gray -distort SRT '%[fx:360.0*t/n]' \ <br />
529    -set delay '%[fx:t == 0 ? 240 : 10]' -loop 0 rose.gif</span></p><p>The color-escape <kbd>%[pixel: ]</kbd> is evaluated once per image and per color channel in that image (<a href="/www/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 <kbd>i</kbd> and <kbd>j</kbd> are set to zero, and <kbd>s</kbd> and <kbd>t</kbd> refer to each successively current image and index.</p>
530
531</div>
532</div>
533
534</div>
535
536<div id="linkbar">
537    <span id="linkbar-west">&nbsp;</span>
538    <span id="linkbar-center">
539      <a href="http://www.imagemagick.org/discourse-server/">Discourse Server</a> &bull;
540      <a href="http://www.imagemagick.org/MagickStudio/scripts/MagickStudio.cgi">Studio</a>
541    </span>
542    <span id="linkbar-east">&nbsp;</span>
543  </div>
544  <div class="footer">
545    <span id="footer-west">&copy; 1999-2011 ImageMagick Studio LLC</span>
546    <span id="footer-east"> <a href="http://www.imagemagick.org/script/contact.php">Contact the Wizards</a></span>
547  </div>
548  <div style="clear: both; margin: 0; width: 100%; "></div>
549  <script type="text/javascript">
550    var _gaq = _gaq || [];
551    _gaq.push(['_setAccount', 'UA-17690367-1']);
552    _gaq.push(['_trackPageview']);
553
554    (function() {
555      var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
556      ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
557      var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
558    })();
559  </script>
560</body>
561</html>
562