fx.html revision 5f2356f90e287a7534129beb42868e72dd1ff9d8
1
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5<head>
6  <meta name="verify-v1" content="g222frIIxcQTrvDR3NBRUSKP3AnMNoqxOkIniCEkV7U=" />
7  <link rel="meta" type="application/rdf+xml" title="ICI" href="http://imagemagick.org/ici.rdf" />
8  <style type="text/css" media="screen,projection"><!--
9    @import url("/www/magick.css");
10  --></style>
11  <link rel="shortcut icon" href="/images/wand.ico"  type="images/vnd.microsoft.icon"/>
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="Generator" content="PHP"/>
17  <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"/>
18  <meta name="Description" content="ImageMagick® is a software suite to create, edit, and compose bitmap images. It can read, convert and write images in a variety of formats (about 100) including GIF, JPEG, JPEG-2000, PNG, PDF, PhotoCD, TIFF, and DPX. Use ImageMagick to translate, flip, mirror, rotate, scale, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves.  ImageMagick is free software delivered as a ready-to-run binary distribution or as source code that you can freely use, copy, modify, and distribute. Its license is compatible with the GPL. It runs on all major operating systems.  The functionality of ImageMagick is typically utilized from the command line or you can use the features from programs written in your favorite programming language. Choose from these interfaces: MagickCore (C), MagickWand (C), ChMagick (Ch), Magick++ (C++), JMagick (Java), L-Magick (Lisp), PascalMagick (Pascal), PerlMagick (Perl), MagickWand for PHP (PHP), PythonMagick (Python), RMagick (Ruby), or TclMagick (Tcl/TK). With a language interface, use ImageMagick to modify or create images automagically and dynamically."/>
19  <meta name="Rating" content="GENERAL"/>
20  <meta name="Robots" content="INDEX, FOLLOW"/>
21  <meta name="Generator" content="ImageMagick Studio LLC"/>
22  <meta name="Author" content="ImageMagick Studio LLC"/>
23  <meta name="Revisit-after" content="2 DAYS"/>
24  <meta name="Resource-type" content="document"/>
25  <meta name="Copyright" content="Copyright (c) 1999-2009 ImageMagick Studio LLC"/>
26  <meta name="Distribution" content="Global"/>
27</head>
28
29<body id="www-imagemagick-org">
30<div class="titlebar">
31<a href="/index.html">
32  <img src="/images/script.png" alt="[ImageMagick]"
33  style="width: 350px; height: 60px; margin: 28px auto; float: left;" /></a>
34<a href="http://www.networkredux.com">
35  <img src="/images/networkredux.png" alt="[sponsor]"
36  style="margin: 45px auto; border: 0px; float: left;" /></a>
37<a href="http://www.imagemagick.org/discourse-server/">
38  <img src="/images/logo.jpg" alt=""
39  style="width: 114px; height: 118px; border: 0px; float: right;" /></a>
40<a href="/index.html">
41  <img src="/images/sprite.jpg" alt=""
42  style="width: 114px; height: 118px; border: 0px; float: right;" /></a>
43</div>
44
45<div class="eastbar">
46
47<div class="menu">
48  <a href="/index.html">About ImageMagick</a>
49</div>
50<div class="sep"></div>
51<div class="menu">
52  <a href="/www/command-line-tools.html">Command-line Tools</a>
53</div>
54<div class="sub">
55    <a href="/www/command-line-processing.html">Processing</a>
56</div>
57<div class="sub">
58    <a href="/www/command-line-options.html">Options</a>
59</div>
60<div class="sub">
61    <a href="http://www.imagemagick.org/Usage/">Usage</a>
62</div>
63<div class="menu">
64  <a href="/www/api.html">Program Interfaces</a>
65</div>
66<div class="sub">
67    <a href="/www/magick-wand.html">MagickWand</a>
68</div>
69<div class="sub">
70    <a href="/www/magick-core.html">MagickCore</a>
71</div>
72<div class="sub">
73    <a href="/www/perl-magick.html">PerlMagick</a>
74</div>
75<div class="sub">
76    <a href="/Magick++/">Magick++</a>
77</div>
78<div class="menu">
79  <a href="/www/architecture.html">Architecture</a>
80</div>
81<div class="sep"></div>
82<div  class="menu">
83   <a href="/www/install-source.html">Install from Source</a>
84</div>
85<div class="sub">
86    <a href="/www/install-source.html#unix">Unix</a>
87</div>
88<div class="sub">
89    <a href="/www/install-source.html#windows">Windows</a>
90 </div>
91<div class="menu">
92  <a href="/www/binary-releases.html">Binary Releases</a>
93</div>
94<div class="sub">
95    <a href="/www/binary-releases.html#unix">Unix</a>
96</div>
97<div class="sub">
98    <a href="/www/binary-releases.html#macosx">Mac OS X</a>
99</div>
100<div class="sub">
101    <a href="/www/binary-releases.html#windows">Windows</a>
102</div>
103<div class="menu">
104  <a href="/www/resources.html">Resources</a>
105</div>
106<div class="sep"></div>
107<div class="menu">
108  <a href="/www/download.html">Download</a>
109</div>
110<div class="sep"></div>
111<div class="menu">
112  <a href="http://www.imagemagick.org/script/search.php">Search</a>
113</div>
114<div class="sep"></div>
115<div class="menu">
116  <a href="/www/sitemap.html">Site Map</a>
117</div>
118<div  class="sub">
119    <a href="/www/links.html">Links</a>
120</div>
121<div class="sep"></div>
122<div  class="menu">
123  <a href="/www/sponsors.html">Sponsors:</a>
124
125<div class="sponsbox">
126<div  class="sponsor">
127   <a href="http://www.abi-stoff.de/abizeitung/" title="Abibuch">Abizeitung</a><!-- 20101101000200 -->
128</div>
129<div  class="sponsor">
130  <a href="http://www.online-kredit-index.de">Kredit</a><!-- 201004010120 Buchhorn -->
131</div>
132<div  class="sponsor">
133  <a href="http://www.blumenversender.com">Blumenversand</a><!-- 201005010120 -->
134</div>
135<div  class="sponsor">
136  <a href="http://www.print24.de/">Druckerei</a><!-- 201009010720 -->
137</div>
138<div  class="sponsor">
139   <a href="http://www.goyax.de">Börse</a><!-- 201001000240 Gewiese digital-finance.de -->
140</div>
141<div  class="sponsor">
142   <a href="http://www.allesdruck.de">Druckerei Online</a><!-- 201012001200 allesdruck.de-->
143</div>
144<div  class="sponsor">
145  <a href="http://www.who-sells-it.com/">Free Catalogs</a><!-- 201002010000 -->
146</div>
147</div>
148</div>
149
150
151</div>
152
153<div class="main">
154
155<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>
156
157<h2><a name="fx"></a>The Fx Special Effects Image Operator</h2>
158<div class="doc-section">
159
160<p>Use the Fx special effects image operator to apply a mathematical expression to an image or image channels.  Use Fx to:</p>
161
162<ul>
163  <li>create canvases, gradients, mathematical colormaps</li>
164  <li>move color values between images and channels</li>
165  <li>translate, flip, mirror, rotate, scale, shear and generally distort images</li>
166  <li>merge or composite multiple images together</li>
167  <li>convolve or merge neighboring pixels together</li>
168  <li>generate image metrics or 'fingerprints'</li>
169</ul>
170
171<p>The expression can be simple:</p>
172
173<p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert -size 64x64 xc:black -channel blue -fx "1/2" fx_navy.png</span></p>
174<p>Here, we convert a black to a navy blue image:</p>
175
176<p class="image">
177  <a href="/images/black.png"><img src="/images/black.png" width="64" height="64" alt="black" /></a>
178  <img style="margin-top:22px; margin-bottom:22px;" src="/images/right.gif" width="20" height="20" alt="==>" />
179  <a href="/images/navy.png"><img src="/images/navy.png" width="64" height="64" alt="navy" /></a>
180</p>
181
182<p>Or the expression can be complex:</p>
183
184<p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert rose.jpg \  <br/>
185          -fx "(1.0/(1.0+exp(10.0*(0.5-u)))-0.006693)*1.0092503" \ <br/>
186          rose-sigmoidal.png</span></p>
187<p>This expression results in a high contrast version of the image:</p>
188
189<p class="image">
190  <a href="/images/rose.jpg"><img src="/images/rose.jpg" width="70" height="46" alt="rose" /></a>
191  <img style="margin-top:13px; margin-bottom:13px;" src="/images/right.gif" width="20" height="20" alt="==>" />
192  <a href="/images/rose-sigmoidal.png"><img src="/images/rose-sigmoidal.png" width="70" height="46" alt="rose-sigmoidal" /></a>
193</p>
194
195<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>
196
197<p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert -size 70x70 xc: \  <br/>
198          -fx "Xi=i-w/2; Yj=j-h/2; 1.2*(0.5-hypot(Xi,Yj)/70.0)+0.5" \ <br/>
199          radial-gradient.png</span></p>
200<p>The command above returns this image:</p>
201
202<p class="image">
203  <a href="/images/radial-gradient.png"><img src="/images/radial-gradient.png" width="70" height="70" alt="radial-gradient" /></a>
204</p>
205<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>
206
207<p>The next section discusses the Fx expression language.</p>
208
209</div>
210
211<h2><a name="anatomy"></a>The Anatomy of an Fx Expression</h2>
212<div class="doc-section">
213
214<h3>The Fx Expression Language</h3>
215<div class="doc-section">
216
217<p>The formal Fx expression language is defined here:</p>
218
219<dl class="doc">
220  <dt class="doc"> numbers:</dt>
221  	<dd> integer, floating point, or scientfic notation (+/- required, e.g. 3.81469e-06)</dd>
222  <dt class="doc"> constants: </dt>
223    <dd> e, Epsilon, QuantumRange, QuantumScale, Opaque, Pi, Transparent</dd>
224  <dt class="doc"> Fx operators (in order of precedence): </dt>
225     <dd> ^ (power), *, /, % (modulo), +, -,
226     &lt;&lt;, &gt;&gt;, &lt;, &lt;=, &gt;, &gt;=, ==, !=,
227     &amp; (bitwise AND),   | (bitwise OR),
228     &amp;&amp; (logical AND),  || (logical OR),
229     ~ (logical NOT),  ?: (ternary conditional)</dd>
230  <dt class="doc"> math functions: </dt>
231     <dd> abs(), acos(), alt(), asin(), atan(), atan2(), ceil(), cos(), cosh(), debug(), exp(), floor(),
232     hypot(), int(), ln(), log(), logtwo(), max(), min(), mod(), pow(), rand(), round(),
233     sign(), sin(), sinh(), sqrt(), tan(), tanh()</dd>
234  <dt class="doc"> channel functions: </dt>
235    <dd> channel(r,g,b,a), channel(c,m,y,k,a)</dd>
236  <dt class="doc"> color names:</dt>
237    <dd> red, cyan, black, etc.</dd>
238  <dt class="doc"> color functions:</dt>
239    <dd> rgb(), rgba(), cmyk(), cmyka(), hsl(), hsla()</dd>
240  <dt class="doc"> color hex values:</dt>
241    <dd> #ccc, #cbfed0, #b9e1cc00</dd>
242  <dt class="doc"> symbols:</dt>
243     <dd style="white-space:pre">
244     s = current image in sequence
245     t = scene number of current image in sequence
246     u = first image in sequence
247     v = second image in sequence
248     n = number of images in sequence
249
250     i = column offset
251     j = row offset
252     p = pixel to use (absolute or relative to current pixel)
253
254     w = width of this image
255     h = height of this image
256     z = channel depth
257
258     r = red value (from RGBA), of a specific or current pixel
259     g = green    ''
260     b = blue     ''
261     a = alpha    ''
262     o = opacity  ''
263
264     c = cyan value of CMYK color of pixel
265     y = yellow   ''
266     m = magenta  ''
267     k = black    ''
268
269     intensity = pixel intensity
270
271     hue = pixel hue
272     saturation = pixel saturation
273     lightness = pixel lightness
274     luminance = pixel luminance
275
276     page.width = page width
277     page.height = page height
278     page.x = page x offset
279     page.y = page y offset
280
281     resolution.x = x resolution
282     resolution.y = y resolution
283
284     depth = image depth
285     minima = image minima
286     maxima = image maxima
287     mean = image mean
288     standard_deviation = image standard deviation
289     kurtosis = image kurtosis
290     skewness = image skewness
291       (add a channel specifier to compute a statistic for that channel, e.g. depth.r)
292     </dd>
293</dl>
294
295</div>
296
297<h3>The Fx Expression</h3>
298<div class="doc-section">
299
300<p>An Fx expression may include any combination of the following:</p>
301<ul>
302<li> <em>x</em> <kbd>^</kbd> <em>y</em>:  exponentiation (<em>x<sup>y</sup></em>)</li>
303<li> <kbd>(</kbd> ... <kbd>)</kbd>:  grouping</li>
304<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>
305<li> <em>x</em> <kbd>/</kbd> <em>y</em>:  division</li>
306<li> <em>x</em> <kbd>%</kbd> <em>y</em>:  modulo</li>
307<li> <em>x</em> <kbd>+</kbd> <em>y</em>:  addition</li>
308<li> <em>x</em> <kbd>-</kbd> <em>y</em>:  subtraction</li>
309<li> <em>x</em> <kbd>&lt;&lt;</kbd> <em>y</em>:  left shift</li>
310<li> <em>x</em> <kbd>&gt;&gt;</kbd> <em>y</em>:  right shift</li>
311<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>
312<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>
313<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>
314<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>
315<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>
316<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>
317<li> <em>x</em> <kbd>&amp;</kbd> <em>y</em>:  binary AND</li>
318<li> <em>x</em> <kbd>|</kbd> <em>y</em>:  binary OR</li>
319<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>
320<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>
321<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>
322<li> <kbd>+</kbd><em>x</em>:  unary plus, return 1.0*value</li>
323<li> <kbd>-</kbd><em>x</em>:  unary minus, return -1.0*value</li>
324<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>
325<li> <em>x</em> <kbd>=</kbd> <em>y</em>: assignment; assignment variables are restricted to letter combinations only (e.g. Xi not X1)</li>
326<li> <em>x</em> <kbd>;</kbd> <em>y</em>: statement separator </li>
327<li> <kbd>pi</kbd>:  constant (3.141659...)</li>
328<li> <kbd>e</kbd>:  constant (2.71828...)</li>
329<li> <kbd>QuantumRange</kbd>:  constant maximum pixel value (255 for Q8, 65535 for Q16)</li>
330<li> <kbd>QuantumScale</kbd>:  constant 1.0/<kbd>QuantumRange</kbd></li>
331<li> <kbd>intensity</kbd>:  pixel intensity; equivalent to 0.299*red+0.587*green+0.114*blue</li>
332<li> <kbd>hue</kbd>:  pixel hue</li>
333<li> <kbd>saturation</kbd>:  pixel saturation</li>
334<li> <kbd>lightness</kbd>:  pixel lightness; equivalent to 0.5*max(red,green,blue) + 0.5*min(red,green,blue)</li>
335<li> <kbd>luminance</kbd>:  pixel luminance; equivalent to 0.2126*red + 0.7152*green + 0.0722*blue</li>
336<li> <kbd>red, green, blue</kbd>, etc.:  color names</li>
337<li> <kbd>#ccc, #cbfed0, #b9e1cc00</kbd>, etc.:  color hex values</li>
338<li> <kbd>rgb(), rgba(), cmyk(), cmyka(), hsl(), hsla()</kbd>:  color functions</li>
339<li> <kbd>s, t, u, v, n, i, j, w, h, z, r, g, b, a, o, c, y, m, k</kbd>:  symbols</li>
340<li> <kbd>abs(</kbd><em>x</em><kbd>)</kbd>:  absolute value function</li>
341<li> <kbd>acos(</kbd><em>x</em><kbd>)</kbd>:  arc cosine function</li>
342<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>
343<li> <kbd>asin(</kbd><em>x</em><kbd>)</kbd>:  arc sine function</li>
344<li> <kbd>atan(</kbd><em>x</em><kbd>)</kbd>:  arc tangent function</li>
345<li> <kbd>atan2(</kbd><em>y</em>,<em>x</em><kbd>)</kbd>:  arc tangent function of two variables</li>
346<li> <kbd>ceil(</kbd><em>x</em><kbd>)</kbd>: smallest integral value not less than argument</li>
347<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>
348<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>
349<li> <kbd>cos(</kbd><em>x</em><kbd>)</kbd>:  cosine function</li>
350<li> <kbd>cosh(</kbd><em>x</em><kbd>)</kbd>:  hyperbolic cosine function</li>
351<li> <kbd>debug(</kbd><em>x</em><kbd>)</kbd>:  print <em>x</em> (useful for debugging your expression)</li>
352<li> <kbd>exp(</kbd><em>x</em><kbd>)</kbd>:  natural exponential function (<em>e<sup>x</sup></em>)</li>
353<li> <kbd>floor(</kbd><em>x</em><kbd>)</kbd>:  largest integral value not greater than argument</li>
354<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>
355<li> <kbd>int(</kbd><em>x</em><kbd>)</kbd>:  greatest integer function (return greatest integer less than or equal to <em>x</em>)</li>
356<li> <kbd>ln(</kbd><em>x</em><kbd>)</kbd>:  natural logarithm function</li>
357<li> <kbd>log(</kbd><em>x</em><kbd>)</kbd>:  logarithm base 10</li>
358<li> <kbd>logtwo(</kbd><em>x</em><kbd>)</kbd>:  logarithm base 2</li>
359<li> <kbd>ln(</kbd><em>x</em><kbd>)</kbd>:  natural logarithm</li>
360<li> <kbd>max(</kbd><em>x</em>, <em>y</em><kbd>)</kbd>:  maximum of <em>x</em> and <em>y</em></li>
361<li> <kbd>min(</kbd><em>x</em>, <em>y</em><kbd>)</kbd>:  minimum of <em>x</em> and <em>y</em></li>
362<li> <kbd>mod(</kbd><em>x</em>, <em>y</em><kbd>)</kbd>:  floating-point remainder function</li>
363<li> <kbd>pow(</kbd><em>x</em>,<em>y</em><kbd>)</kbd>:  power function (<em>x<sup>y</sup></em>)</li>
364<li> <kbd>rand()</kbd>:  value uniformly distributed over the interval [0.0, 1.0) with a 2 to the 128th-1 period</li>
365<li> <kbd>round()</kbd>:  round to integral value, regardless of rounding direction</li>
366<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>
367<li> <kbd>sin(</kbd><em>x</em><kbd>)</kbd>:  sine function</li>
368<li> <kbd>sinh(</kbd><em>x</em><kbd>)</kbd>:  hyperbolic sine function</li>
369<li> <kbd>sqrt(</kbd><em>x</em><kbd>)</kbd>:  square root function</li>
370<li> <kbd>tan(</kbd><em>x</em><kbd>)</kbd>:  tangent function</li>
371<li> <kbd>tanh(</kbd><em>x</em><kbd>)</kbd>:  hyperbolic tangent function</li>
372</ul>
373
374<p>The expression semantics include these rules:</p>
375
376<ul>
377<li>symbols are case insensitive</li>
378<li>only one ternary conditional (e.g. x ? y : z) per statement</li>
379<li>statements are assignments or the final expression to return</li>
380<li>an assignment starts a statement, it is not an operator</li>
381<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>
382</ul>
383
384</div>
385
386<h3>Source Images</h3>
387<div class="doc-section">
388
389<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>
390
391<p>As an example, we form an image by averaging the first image and third images:</p>
392
393<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>
394<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>
395
396<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, any other images being used only for their data. As an illustrative example, comsider 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>
397
398<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>
399<div class="image">
400<p>
401  <a href="/images/logo-sm-flop.png"><img src="/images/logo-sm-flop.png" width="128" height="96" alt="logo-sm-flop.png" /></a>
402  <a href="/images/logo-sm.png"><img src="/images/logo-sm.png" width="128" height="96" alt="logo-sm.png" /></a>
403<img style="margin-top:38px; margin-bottom:38px;" src="/images/right.gif" width="20" height="20" alt="==>" />
404  <a href="/images/logo-sm-fx.png"><img src="/images/logo-sm-fx.png" width="128" height="96" alt="logo-sm-fx.png" /></a>
405</p>
406</div>
407
408
409</div>
410
411<h3>Accessing Pixels</h3>
412<div class="doc-section">
413
414<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>
415
416<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>
417
418<pre class="text">
419   p[-1].g      green value of pixel to the immediate left of the current pixel
420   p[-1,-1].r   red value of the pixel diagonally left and up from current pixel
421</pre>
422
423<p>To specify an absolute position, use braces, rather than brackets.</p>
424
425<pre class="text">
426  p{0,0}.r   red value of the pixel in the upper left corner of the image
427  p{12,34}.b   blue pixel value at column number 12, row 34 of the image
428</pre>
429
430<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>
431
432<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>
433
434</div>
435
436<h3>Apply an Expression to Select Image Channels</h3>
437<div class="doc-section">
438
439<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>
440
441<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>
442</div>
443
444<h3>Results</h3>
445<div class="doc-section">
446
447<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>
448
449<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>
450
451
452<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>
453
454<p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert  xc:'rgb(25%,50%,75%)' rose: -colorspace rgb  \ <br/>
455  -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.250004 <br/>
456  Red channel of NW corner of image # 1 is 0.188235</span></p>
457<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>
458
459</div>
460</div>
461
462</div>
463
464<div id="linkbar">
465 <!--    <span id="linkbar-west">&nbsp;</span>  -->
466    <span id="linkbar-center">
467      <a href="http://www.imagemagick.org/discourse-server/">Discourse Server</a> &bull;
468      <a href="/www/mailing-list.html">Mailing Lists</a> &bull;
469    <a href="http://www.imagemagick.org/MagickStudio/scripts/MagickStudio.cgi">Studio</a>
470    </span>
471    <span id="linkbar-east">&nbsp;</span>
472  </div>
473  <div class="footer">
474    <span id="footer-west">&copy; 1999-2009 ImageMagick Studio LLC</span>
475    <span id="footer-east"> <a href="http://www.imagemagick.org/script/contact.php">Contact the Wizards</a></span>
476  </div>
477  <div style="clear: both; margin: 0; width: 100%; "></div>
478</body>
479</html>
480