blob: 4ed77e69f8f445289d8e825d12c8be53c6d40d69 [file] [log] [blame]
cristy417eeb12013-07-28 18:48:22 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="-//W3C//DTD XHTML 1.1//EN" xml:lang="en" xsi:schemaLocation="http://www.w3.org/1999/xhtml http://www.w3.org/MarkUp/SCHEMA/xhtml11.xsd">
cristyad41c0c2012-10-31 00:49:28 +00003<head>
cristy417eeb12013-07-28 18:48:22 +00004 <meta http-equiv="content-type" content="text/html; charset=UTF-8"></meta>
5 <meta name="google-site-verification" content="_bMOCDpkx9ZAzBwb2kF3PRHbfUUdFj2uO8Jd1AXArz4"></meta>
cristyffb69ed2010-12-25 00:06:48 +00006 <title>ImageMagick: The Fx Special Effects Image Operator</title>
cristy417eeb12013-07-28 18:48:22 +00007 <meta http-equiv="content-language" content="en-US"></meta>
8 <meta http-equiv="content-type" content="text/html; charset=utf-8"></meta>
9 <meta http-equiv="reply-to" content="magick-users@imagemagick.org"></meta>
10 <meta name="application-name" content="ImageMagick"></meta>
11 <meta name="description" content="Use ImageMagick to convert, edit, or compose bitmap images in a variety of formats. In addition, resize, rotate, shear, distort or transform images automagically."></meta>
12 <meta name="application-url" content="http://www.imagemagick.org"></meta>
13 <meta name="generator" content="PHP"></meta>
14 <meta name="keywords" content="the, fx, special, effects, image, operator, ImageMagick, PerlMagick, image processing, OpenMP, software development library, image, photo, software, Magick++, MagickWand"></meta>
15 <meta name="rating" content="GENERAL"></meta>
16 <meta name="robots" content="INDEX, FOLLOW"></meta>
17 <meta name="generator" content="ImageMagick Studio LLC"></meta>
18 <meta name="author" content="ImageMagick Studio LLC"></meta>
19 <meta name="revisit-after" content="2 DAYS"></meta>
20 <meta name="resource-type" content="document"></meta>
21 <meta name="copyright" content="Copyright (c) 1999-2012 ImageMagick Studio LLC"></meta>
22 <meta name="distribution" content="Global"></meta>
23 <link rel="icon" href="../images/wand.png"></link>
24 <link rel="shortcut icon" href="../images/wand.ico" type="images/x-icon"></link>
cristy149d58c2013-08-07 19:44:54 +000025 <link rel="canonical" href="../index.html"></link>
cristy417eeb12013-07-28 18:48:22 +000026 <link rel="meta" type="application/rdf+xml" title="ICI" href="http://imagemagick.org/ici.rdf"></link>
cristy8ee7f242013-06-20 16:08:44 +000027
cristyad41c0c2012-10-31 00:49:28 +000028 <!-- Add jQuery library -->
cristy417eeb12013-07-28 18:48:22 +000029 <script type="text/javascript" src="http://localhost/ajax/libs/jquery/1.7/jquery.min.js"></script>
cristyad41c0c2012-10-31 00:49:28 +000030
cristy8ee7f242013-06-20 16:08:44 +000031 <!-- Add mousewheel plugin (this is optional) -->
32 <script type="text/javascript" src="http://www.imagemagick.org/fancybox/lib/jquery.mousewheel-3.0.6.pack.js"></script>
cristyad41c0c2012-10-31 00:49:28 +000033
34 <!-- Add fancyBox -->
cristy417eeb12013-07-28 18:48:22 +000035 <link rel="stylesheet" href="http://www.imagemagick.org/fancybox/source/jquery.fancybox.css?v=2.0.6" type="text/css" media="screen"></link>
cristy8ee7f242013-06-20 16:08:44 +000036 <script type="text/javascript" src="http://www.imagemagick.org/fancybox/source/jquery.fancybox.pack.js?v=2.0.6"></script>
cristyad41c0c2012-10-31 00:49:28 +000037
cristy8ee7f242013-06-20 16:08:44 +000038 <!-- Optionally add helpers - button, thumbnail and/or media -->
cristy417eeb12013-07-28 18:48:22 +000039 <link rel="stylesheet" href="http://www.imagemagick.org/fancybox/source/helpers/jquery.fancybox-buttons.css?v=1.0.2" type="text/css" media="screen"></link>
cristy8ee7f242013-06-20 16:08:44 +000040 <script type="text/javascript" src="http://www.imagemagick.org/fancybox/source/helpers/jquery.fancybox-buttons.js?v=1.0.2"></script>
41 <script type="text/javascript" src="http://www.imagemagick.org/fancybox/source/helpers/jquery.fancybox-media.js?v=1.0.0"></script>
cristyad41c0c2012-10-31 00:49:28 +000042
cristy417eeb12013-07-28 18:48:22 +000043 <link rel="stylesheet" href="http://www.imagemagick.org/fancybox/source/helpers/jquery.fancybox-thumbs.css?v=2.0.6" type="text/css" media="screen"></link>
cristy8ee7f242013-06-20 16:08:44 +000044 <script type="text/javascript" src="http://www.imagemagick.org/fancybox/source/helpers/jquery.fancybox-thumbs.js?v=2.0.6"></script>
45
cristyad41c0c2012-10-31 00:49:28 +000046 <script type="text/javascript">
47 $(document).ready(function() {
48 $(".fancybox").fancybox();
49 });
50 </script>
cristy8ee7f242013-06-20 16:08:44 +000051
cristyad41c0c2012-10-31 00:49:28 +000052 <!-- ImageMagick style -->
cristyad41c0c2012-10-31 00:49:28 +000053 <style type="text/css" media="all">
54 @import url("../www/magick.css");
55 </style>
56</head>
57
58<body id="www-imagemagick-org">
cristy8ee7f242013-06-20 16:08:44 +000059
cristy8ee7f242013-06-20 16:08:44 +000060
cristy8ee7f242013-06-20 16:08:44 +000061
cristyad41c0c2012-10-31 00:49:28 +000062<div class="titlebar">
63<div style="margin: 17px auto; float: left;">
64 <script type="text/javascript">
65 <!--
66 google_ad_client = "pub-3129977114552745";
67 google_ad_slot = "5439289906";
68 google_ad_width = 728;
69 google_ad_height = 90;
70 //-->
71 </script>
cristy417eeb12013-07-28 18:48:22 +000072
cristyad41c0c2012-10-31 00:49:28 +000073</div>
cristy149d58c2013-08-07 19:44:54 +000074<a href="../discourse-server">
cristy417eeb12013-07-28 18:48:22 +000075 <img src="../images/logo.jpg" alt="ImageMagick Logo" style="width: 123px; height: 118px; border: 0px; float: right;"></img></a>
cristyad41c0c2012-10-31 00:49:28 +000076<a href="../index.html">
cristy417eeb12013-07-28 18:48:22 +000077 <img src="../images/sprite.jpg" alt="ImageMagick Sprite" style="width: 114px; height: 118px; border: 0px; float: right;"></img></a>
cristyad41c0c2012-10-31 00:49:28 +000078</div>
79
80<div class="westbar">
81
cristy3ed852e2009-09-05 21:47:34 +000082<div class="menu">
cristy83a272e2010-05-07 20:40:35 +000083 <a title="About ImageMagick" href="../index.html">About ImageMagick</a>
cristy3ed852e2009-09-05 21:47:34 +000084</div>
cristy3ed852e2009-09-05 21:47:34 +000085<div class="menu">
cristy751980d2012-06-03 23:18:35 +000086 <a title="Binary Releases" href="binary-releases.html">Binary Releases</a>
cristybb503372010-05-27 20:51:26 +000087</div>
88<div class="sub">
cristy751980d2012-06-03 23:18:35 +000089 <a title="Binary Release: Unix" href="binary-releases.html#unix">Unix</a>
cristybb503372010-05-27 20:51:26 +000090</div>
91<div class="sub">
cristy751980d2012-06-03 23:18:35 +000092 <a title="Binary Release: MacOS X" href="binary-releases.html#macosx">Mac OS X</a>
cristybb503372010-05-27 20:51:26 +000093</div>
94<div class="sub">
cristy751980d2012-06-03 23:18:35 +000095 <a title="Binary Release: iOS" href="binary-releases.html#iOS">iOS</a>
cristyda16f162011-02-19 23:52:17 +000096</div>
97<div class="sub">
cristy751980d2012-06-03 23:18:35 +000098 <a title="Binary Release: Windows" href="binary-releases.html#windows">Windows</a>
cristybb503372010-05-27 20:51:26 +000099</div>
cristyad41c0c2012-10-31 00:49:28 +0000100<div class="sep"></div>
cristybb503372010-05-27 20:51:26 +0000101<div class="menu">
cristy751980d2012-06-03 23:18:35 +0000102 <a title="Command-line Tools" href="command-line-tools.html">Command-line Tools</a>
cristy3ed852e2009-09-05 21:47:34 +0000103</div>
104<div class="sub">
cristy751980d2012-06-03 23:18:35 +0000105 <a title="Command-line Tools: Processing" href="command-line-processing.html">Processing</a>
cristy3ed852e2009-09-05 21:47:34 +0000106</div>
107<div class="sub">
cristy751980d2012-06-03 23:18:35 +0000108 <a title="Command-line Tools: Options" href="command-line-options.html">Options</a>
cristy3ed852e2009-09-05 21:47:34 +0000109</div>
110<div class="sub">
cristy83a272e2010-05-07 20:40:35 +0000111 <a title="Command-line Tools: Usage" href="http://www.imagemagick.org/Usage/">Usage</a>
cristy3ed852e2009-09-05 21:47:34 +0000112</div>
113<div class="menu">
cristy751980d2012-06-03 23:18:35 +0000114 <a title="Program Interfaces" href="api.html">Program Interfaces</a>
cristy3ed852e2009-09-05 21:47:34 +0000115</div>
116<div class="sub">
cristy751980d2012-06-03 23:18:35 +0000117 <a title="Program Interface: MagickWand" href="magick-wand.html">MagickWand</a>
cristy3ed852e2009-09-05 21:47:34 +0000118</div>
119<div class="sub">
cristy751980d2012-06-03 23:18:35 +0000120 <a title="Program Interface: MagickCore" href="magick-core.html">MagickCore</a>
cristy3ed852e2009-09-05 21:47:34 +0000121</div>
122<div class="sub">
cristy751980d2012-06-03 23:18:35 +0000123 <a title="Program Interface: PerlMagick" href="perl-magick.html">PerlMagick</a>
cristy3ed852e2009-09-05 21:47:34 +0000124</div>
125<div class="sub">
cristy751980d2012-06-03 23:18:35 +0000126 <a title="Program Interface: Magick++" href="magick++.html">Magick++</a>
cristy3ed852e2009-09-05 21:47:34 +0000127</div>
cristyad41c0c2012-10-31 00:49:28 +0000128<div class="sep"></div>
cristy417eeb12013-07-28 18:48:22 +0000129<div class="menu">
cristy751980d2012-06-03 23:18:35 +0000130 <a title="Install from Source" href="install-source.html">Install from Source</a>
cristy3ed852e2009-09-05 21:47:34 +0000131</div>
132<div class="sub">
cristy751980d2012-06-03 23:18:35 +0000133 <a title="Install from Source: Unix" href="install-source.html#unix">Unix</a>
cristy3ed852e2009-09-05 21:47:34 +0000134</div>
135<div class="sub">
cristy751980d2012-06-03 23:18:35 +0000136 <a title="Install from Source: Windows" href="install-source.html#windows">Windows</a>
cristy3ed852e2009-09-05 21:47:34 +0000137 </div>
138<div class="menu">
cristy751980d2012-06-03 23:18:35 +0000139 <a title="Resources" href="resources.html">Resources</a>
cristy3ed852e2009-09-05 21:47:34 +0000140</div>
cristybb503372010-05-27 20:51:26 +0000141<div class="menu">
cristy751980d2012-06-03 23:18:35 +0000142 <a title="Architecture" href="architecture.html">Architecture</a>
cristybb503372010-05-27 20:51:26 +0000143</div>
cristy3ed852e2009-09-05 21:47:34 +0000144<div class="menu">
cristy751980d2012-06-03 23:18:35 +0000145 <a title="Download" href="download.html">Download</a>
cristy3ed852e2009-09-05 21:47:34 +0000146</div>
cristyad41c0c2012-10-31 00:49:28 +0000147<div class="sep"></div>
cristy3ed852e2009-09-05 21:47:34 +0000148<div class="menu">
cristy8ee7f242013-06-20 16:08:44 +0000149 <a title="Search" href="search.html">Search</a>
cristy3ed852e2009-09-05 21:47:34 +0000150</div>
cristyad41c0c2012-10-31 00:49:28 +0000151<div class="sep"></div>
cristy3ed852e2009-09-05 21:47:34 +0000152<div class="menu">
cristy751980d2012-06-03 23:18:35 +0000153 <a title="Site Map" href="sitemap.html">Site Map</a>
cristy3ed852e2009-09-05 21:47:34 +0000154</div>
cristy417eeb12013-07-28 18:48:22 +0000155<div class="sub">
cristy751980d2012-06-03 23:18:35 +0000156 <a title="Site Map: Links" href="links.html">Links</a>
cristy3ed852e2009-09-05 21:47:34 +0000157</div>
cristyad41c0c2012-10-31 00:49:28 +0000158<div class="sep"></div>
cristyeed6cb62012-01-28 19:38:34 +0000159<div class="menu">
cristy8ee7f242013-06-20 16:08:44 +0000160 <a title="Support ImageMagick Development" href="support.html">Support ImageMagick Development</a>
cristyce69bb02010-07-27 19:49:46 +0000161</div>
cristyad41c0c2012-10-31 00:49:28 +0000162</div>
163
164<div class="eastbar">
cristy417eeb12013-07-28 18:48:22 +0000165
cristy149d58c2013-08-07 19:44:54 +0000166 <div> <a class="FlattrButton" style="display:none;" rev="flattr;button:compact;" href="../index.html"></a></div>
cristy8ee7f242013-06-20 16:08:44 +0000167 <noscript>
168 <a href="http://flattr.com/thing/947300/Convert-Edit-And-Compose-Images" target="_blank">
cristy417eeb12013-07-28 18:48:22 +0000169 <img src="http://api.flattr.com/button/flattr-badge-large.png" alt="Flattr this" title="Flattr this" width="93" height="20" border="0"></img></a>
cristy8ee7f242013-06-20 16:08:44 +0000170 </noscript>
cristy417eeb12013-07-28 18:48:22 +0000171
172
cristyad41c0c2012-10-31 00:49:28 +0000173</div>
174
175<div class="main">
176
cristydcca3fa2010-12-29 21:50:07 +0000177<h1>Apply Special Effects to an Image with an Fx Expression</h1>
cristy417eeb12013-07-28 18:48:22 +0000178<p class="navigation-index">[<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 +0000179
cristydcca3fa2010-12-29 21:50:07 +0000180<a id="fx"></a>
cristy3ed852e2009-09-05 21:47:34 +0000181<div class="doc-section">
182
183<p>Use the Fx special effects image operator to apply a mathematical expression to an image or image channels. Use Fx to:</p>
184
185<ul>
186 <li>create canvases, gradients, mathematical colormaps</li>
187 <li>move color values between images and channels</li>
188 <li>translate, flip, mirror, rotate, scale, shear and generally distort images</li>
189 <li>merge or composite multiple images together</li>
190 <li>convolve or merge neighboring pixels together</li>
191 <li>generate image metrics or 'fingerprints'</li>
192</ul>
193
194<p>The expression can be simple:</p>
195
cristy417eeb12013-07-28 18:48:22 +0000196<p class="crt"><span class="crtprompt"> $ </span><span class="crtin">convert -size 64x64 canvas:black -channel blue -fx "1/2" fx_navy.png</span></p>
cristy3ed852e2009-09-05 21:47:34 +0000197<p>Here, we convert a black to a navy blue image:</p>
198
199<p class="image">
cristy417eeb12013-07-28 18:48:22 +0000200 <a href="../images/black.png"><img src="../images/black.png" width="64" height="64" alt="black"></img></a>
201 <img style="margin-top:22px; margin-bottom:22px;" src="../images/right.gif" width="20" height="20" alt="==&gt;"></img>
202 <a href="../images/navy.png"><img src="../images/navy.png" width="64" height="64" alt="navy"></img></a>
cristy3ed852e2009-09-05 21:47:34 +0000203</p>
204
205<p>Or the expression can be complex:</p>
206
cristy417eeb12013-07-28 18:48:22 +0000207<p class="crt"><span class="crtprompt"> $ </span><span class="crtin">convert rose.jpg \ <br></br>
208 -fx "(1.0/(1.0+exp(10.0*(0.5-u)))-0.006693)*1.0092503" \ <br></br>
cristy3ed852e2009-09-05 21:47:34 +0000209 rose-sigmoidal.png</span></p>
210<p>This expression results in a high contrast version of the image:</p>
211
212<p class="image">
cristy417eeb12013-07-28 18:48:22 +0000213 <a href="../images/rose.jpg"><img src="../images/rose.jpg" width="70" height="46" alt="rose"></img></a>
214 <img style="margin-top:13px; margin-bottom:13px;" src="../images/right.gif" width="20" height="20" alt="==&gt;"></img>
215 <a href="../images/rose-sigmoidal.png"><img src="../images/rose-sigmoidal.png" width="70" height="46" alt="rose-sigmoidal"></img></a>
cristy3ed852e2009-09-05 21:47:34 +0000216</p>
217
218<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>
219
cristy417eeb12013-07-28 18:48:22 +0000220<p class="crt"><span class="crtprompt"> $ </span><span class="crtin">convert -size 70x70 canvas: \ <br></br>
221 -fx "Xi=i-w/2; Yj=j-h/2; 1.2*(0.5-hypot(Xi,Yj)/70.0)+0.5" \ <br></br>
cristy3ed852e2009-09-05 21:47:34 +0000222 radial-gradient.png</span></p>
223<p>The command above returns this image:</p>
224
225<p class="image">
cristy417eeb12013-07-28 18:48:22 +0000226 <a href="../images/radial-gradient.png"><img src="../images/radial-gradient.png" width="70" height="70" alt="radial-gradient"></img></a>
cristy3ed852e2009-09-05 21:47:34 +0000227</p>
cristye9a32c32011-04-07 01:11:05 +0000228
229<p>This FX expression adds random noise to an image:</p>
230
cristy417eeb12013-07-28 18:48:22 +0000231<p class="crt"><span class="crtprompt"> $ </span><span class="crtin">convert photo.jpg -fx 'iso=32; rone=rand(); rtwo=rand(); \<br></br>
232 myn=sqrt(-2*ln(rone))*cos(2*Pi*rtwo); myntwo=sqrt(-2*ln(rtwo))* \<br></br>
233 cos(2*Pi*rone); pnoise=sqrt(p)*myn*sqrt(iso)* \ <br></br>
cristye9a32c32011-04-07 01:11:05 +0000234 channel(4.28,3.86,6.68,0)/255; max(0,p+pnoise)' noisy.png</span></p>
cristy3ed852e2009-09-05 21:47:34 +0000235<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>
236
237<p>The next section discusses the Fx expression language.</p>
238
239</div>
240
cristydcca3fa2010-12-29 21:50:07 +0000241<h2><a id="anatomy"></a>The Anatomy of an Fx Expression</h2>
cristy3ed852e2009-09-05 21:47:34 +0000242<div class="doc-section">
243
244<h3>The Fx Expression Language</h3>
245<div class="doc-section">
246
247<p>The formal Fx expression language is defined here:</p>
248
249<dl class="doc">
250 <dt class="doc"> numbers:</dt>
cristyeed6cb62012-01-28 19:38:34 +0000251 <dd> integer, floating point, scientfic notation (+/- required, e.g. 3.81469e-06), International System number postfixes (.e.g KB, Mib, GB, etc.)</dd>
cristy3ed852e2009-09-05 21:47:34 +0000252 <dt class="doc"> constants: </dt>
cristyeed6cb62012-01-28 19:38:34 +0000253 <dd> E (Euler's number), Epsilon, QuantumRange, QuantumScale, Opaque, Phi (golden ratio), Pi, Transparent</dd>
cristy3ed852e2009-09-05 21:47:34 +0000254 <dt class="doc"> Fx operators (in order of precedence): </dt>
cristyd92fed52010-10-25 13:48:19 +0000255 <dd> ^ (power), unary -, *, /, % (modulo), +, -,
cristy3ed852e2009-09-05 21:47:34 +0000256 &lt;&lt;, &gt;&gt;, &lt;, &lt;=, &gt;, &gt;=, ==, !=,
257 &amp; (bitwise AND), | (bitwise OR),
258 &amp;&amp; (logical AND), || (logical OR),
259 ~ (logical NOT), ?: (ternary conditional)</dd>
260 <dt class="doc"> math functions: </dt>
cristyeed6cb62012-01-28 19:38:34 +0000261 <dd> abs(), acos(), acosh(), airy(), alt(), asin(), asinh(), atan(), atanh(), atan2(), ceil(), cos(), cosh(), debug(), drc(), 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>
cristy3ed852e2009-09-05 21:47:34 +0000262 <dt class="doc"> channel functions: </dt>
263 <dd> channel(r,g,b,a), channel(c,m,y,k,a)</dd>
264 <dt class="doc"> color names:</dt>
265 <dd> red, cyan, black, etc.</dd>
266 <dt class="doc"> color functions:</dt>
cristy751980d2012-06-03 23:18:35 +0000267 <dd> srgb(), srgba(), rgb(), rgba(), cmyk(), cmyka(), hsl(), hsla(), etc.</dd>
cristy3ed852e2009-09-05 21:47:34 +0000268 <dt class="doc"> color hex values:</dt>
cristy751980d2012-06-03 23:18:35 +0000269 <dd> #ccc, #cbfed0, #b9e1cc00, etc.</dd>
cristy3ed852e2009-09-05 21:47:34 +0000270 <dt class="doc"> symbols:</dt>
271 <dd style="white-space:pre">
cristy8ee7f242013-06-20 16:08:44 +0000272 u = first image in list
273 v = second image in list
274 s = current image in list (for %[fx:] otherwise = u)
275 t = index of current image (s) in list
276 n = number of images in list
cristy3ed852e2009-09-05 21:47:34 +0000277
278 i = column offset
279 j = row offset
280 p = pixel to use (absolute or relative to current pixel)
281
282 w = width of this image
283 h = height of this image
284 z = channel depth
285
286 r = red value (from RGBA), of a specific or current pixel
287 g = green ''
288 b = blue ''
289 a = alpha ''
290 o = opacity ''
291
292 c = cyan value of CMYK color of pixel
293 y = yellow ''
294 m = magenta ''
295 k = black ''
296
297 intensity = pixel intensity
298
299 hue = pixel hue
300 saturation = pixel saturation
301 lightness = pixel lightness
cristy149d58c2013-08-07 19:44:54 +0000302 luma = pixel luma
cristy3ed852e2009-09-05 21:47:34 +0000303
304 page.width = page width
305 page.height = page height
306 page.x = page x offset
307 page.y = page y offset
308
309 resolution.x = x resolution
310 resolution.y = y resolution
311
312 depth = image depth
313 minima = image minima
314 maxima = image maxima
315 mean = image mean
316 standard_deviation = image standard deviation
317 kurtosis = image kurtosis
318 skewness = image skewness
319 (add a channel specifier to compute a statistic for that channel, e.g. depth.r)
320 </dd>
cristyeed6cb62012-01-28 19:38:34 +0000321 <dt class="doc"> iterators:</dt>
322 <dd> while()</dd>
cristy3ed852e2009-09-05 21:47:34 +0000323</dl>
324
325</div>
326
327<h3>The Fx Expression</h3>
328<div class="doc-section">
329
330<p>An Fx expression may include any combination of the following:</p>
331<ul>
cristy8ee7f242013-06-20 16:08:44 +0000332<li> <em>x</em> <code>^</code> <em>y</em>: exponentiation (<em>x<sup>y</sup></em>)</li>
333<li> <code>(</code> ... <code>)</code>: grouping</li>
334<li> <em>x</em> <code>*</code> <em>y</em>: multiplication (the asterisk <code>*</code> is optional, for example, <code>2u</code> or <code>2(x+y)</code> are acceptable)</li>
335<li> <em>x</em> <code>/</code> <em>y</em>: division</li>
336<li> <em>x</em> <code>%</code> <em>y</em>: modulo</li>
337<li> <em>x</em> <code>+</code> <em>y</em>: addition</li>
338<li> <em>x</em> <code>-</code> <em>y</em>: subtraction</li>
339<li> <em>x</em> <code>&lt;&lt;</code> <em>y</em>: left shift</li>
340<li> <em>x</em> <code>&gt;&gt;</code> <em>y</em>: right shift</li>
341<li> <em>x</em> <code>&lt;</code> <em>y</em>: boolean relation, return value 1.0 if <em>x</em> &lt; <em>y</em>, otherwise 0.0</li>
342<li> <em>x</em> <code>&lt;=</code> <em>y</em>: boolean relation, return value 1.0 if <em>x</em> &lt;= <em>y</em>, otherwise 0.0</li>
343<li> <em>x</em> <code>&gt;</code> <em>y</em>: boolean relation, return value 1.0 if <em>x</em> &gt; <em>y</em>, otherwise 0.0</li>
344<li> <em>x</em> <code>&gt;=</code> <em>y</em>: boolean relation, return value 1.0 if <em>x</em> &gt;= <em>y</em>, otherwise 0.0</li>
345<li> <em>x</em> <code>==</code> <em>y</em>: boolean relation, return value 1.0 if <em>x </em>==<em> y</em>, otherwise 0.0</li>
346<li> <em>x</em> <code>!=</code> <em>y</em>: boolean relation, return value 1.0 if <em>x </em>!=<em> y</em>, otherwise 0.0</li>
347<li> <em>x</em> <code>&amp;</code> <em>y</em>: binary AND</li>
348<li> <em>x</em> <code>|</code> <em>y</em>: binary OR</li>
349<li> <em>x</em> <code>&amp;&amp;</code> <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>
350<li> <em>x</em> <code>||</code> <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>
351<li> <code>~</code><em>x</em>: logical NOT operator, return value 1.0 if <em>not</em> <em>x</em> &gt; 0, otherwise 0.0</li>
352<li> <code>+</code><em>x</em>: unary plus, return 1.0*value</li>
353<li> <code>-</code><em>x</em>: unary minus, return -1.0*value</li>
354<li> <em>x</em> <code>?</code> <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>
355<li> <em>x</em> <code>=</code> <em>y</em>: assignment; assignment variables are restricted to letter combinations only (e.g. Xi not X1)</li>
356<li> <em>x</em> <code>;</code> <em>y</em>: statement separator </li>
357<li> <code>phi</code>: constant (1.618034...)</li>
358<li> <code>pi</code>: constant (3.141659...)</li>
359<li> <code>e</code>: constant (2.71828...)</li>
360<li> <code>QuantumRange</code>: constant maximum pixel value (255 for Q8, 65535 for Q16)</li>
361<li> <code>QuantumScale</code>: constant 1.0/<code>QuantumRange</code></li>
362<li> <code>intensity</code>: pixel intensity; equivalent to 0.299*red+0.587*green+0.114*blue</li>
363<li> <code>hue</code>: pixel hue</li>
364<li> <code>saturation</code>: pixel saturation</li>
365<li> <code>lightness</code>: pixel lightness; equivalent to 0.5*max(red,green,blue) + 0.5*min(red,green,blue)</li>
366<li> <code>luminance</code>: pixel luminance; equivalent to 0.212656*red + 0.715158*green + 0.072186*blue</li>
367<li> <code>red, green, blue</code>, etc.: color names</li>
368<li> <code>#ccc, #cbfed0, #b9e1cc00</code>, etc.: color hex values</li>
369<li> <code>rgb(), rgba(), cmyk(), cmyka(), hsl(), hsla()</code>: color functions</li>
370<li> <code>s, t, u, v, n, i, j, w, h, z, r, g, b, a, o, c, y, m, k</code>: symbols</li>
371<li> <code>abs(</code><em>x</em><code>)</code>: absolute value function</li>
372<li> <code>acos(</code><em>x</em><code>)</code>: arc cosine function</li>
373<li> <code>acosh(</code><em>x</em><code>)</code>: inverse hyperbolic cosine function</li>
374<li> <code>airy(</code><em>x</em><code>)</code>: Airy function (max=1, min=0); airy(<em>x</em>)=[jinc(<em>x</em>)]<sup>2</sup>=[2*j1(<em>pi*x</em>)/(<em>pi*x</em>)]<sup>2</sup></li>
375<li> <code>alt(</code><em>x</em><code>)</code>: sign alternation function (return 1.0 if int(<em>x</em>) is even, -1.0 if int(<em>x</em>) is odd)</li>
376<li> <code>asin(</code><em>x</em><code>)</code>: arc sine function</li>
377<li> <code>asinh(</code><em>x</em><code>)</code>: inverse hyperbolic sine function</li>
378<li> <code>atan(</code><em>x</em><code>)</code>: arc tangent function</li>
379<li> <code>atanh(</code><em>x</em><code>)</code>: inverse hyperbolic tangent function</li>
380<li> <code>atan2(</code><em>y</em>,<em>x</em><code>)</code>: arc tangent function of two variables</li>
381<li> <code>ceil(</code><em>x</em><code>)</code>: smallest integral value not less than argument</li>
382<li> <code>channel(</code><em>r</em>,<em>g</em>,<em>b</em>,<em>a</em><code>)</code>: select numeric argument based on current channel</li>
383<li> <code>channel(</code><em>c</em>,<em>m</em>,<em>y</em>,<em>k</em>,<em>a</em><code>)</code>: select numeric argument based on current channel</li>
384<li> <code>cos(</code><em>x</em><code>)</code>: cosine function</li>
385<li> <code>cosh(</code><em>x</em><code>)</code>: hyperbolic cosine function</li>
386<li> <code>debug(</code><em>x</em><code>)</code>: print <em>x</em> (useful for debugging your expression)</li>
387<li> <code>drc(</code><em>x</em>,<em>y</em><code>)</code>: dynamic range compression (knee curve); drc(<em>x</em>,<em>y</em>)=(<em>x</em>)/(<em>y</em>*(<em>x</em>-1)+1); -1&lt;<em>y</em>&lt;1 </li>
388<li> <code>exp(</code><em>x</em><code>)</code>: natural exponential function (<em>e<sup>x</sup></em>)</li>
389<li> <code>floor(</code><em>x</em><code>)</code>: largest integral value not greater than argument</li>
390<li> <code>gauss(</code><em>x</em><code>)</code>: gaussian function; gauss(<em>x</em>)=exp(<em>-x*x/2</em>)/sqrt(2*pi)</li>
391<li> <code>gcd(</code><em>x</em>,<em>y</em><code>)</code>: greatest common denominator</li>
392<li> <code>hypot(</code><em>x</em>,<em>y</em><code>)</code>: the square root of x<sup>2</sup>+y<sup>2</sup></li>
393<li> <code>int(</code><em>x</em><code>)</code>: greatest integer function (return greatest integer less than or equal to <em>x</em>)</li>
394<li> <code>isnan(</code><em>x</em><code>)</code>: return 1.0 if <em>x</em> is NAN, 0.0 otherwise</li>
395<li> <code>j0(</code><em>x</em><code>)</code>: Bessel functions of <em>x</em> of the first kind of order 0</li>
396<li> <code>j1(</code><em>x</em><code>)</code>: Bessel functions of <em>x</em> of the first kind of order 1</li>
397<li> <code>jinc(</code><em>x</em><code>)</code>: jinc function (max=1, min=-0.1323); jinc(<em>x</em>)=2*j1(pi*<em>x</em>)/(pi*<em>*x</em>)</li>
398<li> <code>ln(</code><em>x</em><code>)</code>: natural logarithm function</li>
399<li> <code>log(</code><em>x</em><code>)</code>: logarithm base 10</li>
400<li> <code>logtwo(</code><em>x</em><code>)</code>: logarithm base 2</li>
401<li> <code>ln(</code><em>x</em><code>)</code>: natural logarithm</li>
402<li> <code>max(</code><em>x</em>, <em>y</em><code>)</code>: maximum of <em>x</em> and <em>y</em></li>
403<li> <code>min(</code><em>x</em>, <em>y</em><code>)</code>: minimum of <em>x</em> and <em>y</em></li>
404<li> <code>mod(</code><em>x</em>, <em>y</em><code>)</code>: floating-point remainder function</li>
405<li> <code>not(</code><em>x</em><code>)</code>: return 1.0 if <em>x</em> is zero, 0.0 otherwise</li>
406<li> <code>pow(</code><em>x</em>,<em>y</em><code>)</code>: power function (<em>x<sup>y</sup></em>)</li>
407<li> <code>rand()</code>: value uniformly distributed over the interval [0.0, 1.0) with a 2 to the 128th-1 period</li>
408<li> <code>round()</code>: round to integral value, regardless of rounding direction</li>
409<li> <code>sign(</code><em>x</em><code>)</code>: return -1.0 if <em>x</em> is less than 0.0 otherwise 1.0</li>
410<li> <code>sin(</code><em>x</em><code>)</code>: sine function</li>
411<li> <code>sinc(</code><em>x</em><code>)</code>: sinc function (max=1, min=-0.21); sinc(<em>x</em>)=sin(<em>pi*x</em>)/(<em>pi*x</em>)</li>
412<li> <code>squish(</code><em>x</em><code>)</code>: squish function; squish(<em>x</em>)=1.0/(1.0+exp(<em>-x</em>))</li>
413<li> <code>sinh(</code><em>x</em><code>)</code>: hyperbolic sine function</li>
414<li> <code>sqrt(</code><em>x</em><code>)</code>: square root function</li>
415<li> <code>tan(</code><em>x</em><code>)</code>: tangent function</li>
416<li> <code>tanh(</code><em>x</em><code>)</code>: hyperbolic tangent function</li>
417<li> <code>trunc(</code><em>x</em><code>)</code>: round to integer, towards zero</li>
418<li> <code>while(</code><em>condition</em>,<em>expression</em><code>)</code>: interate while the condition is not equal to 0</li>
cristy3ed852e2009-09-05 21:47:34 +0000419</ul>
420
421<p>The expression semantics include these rules:</p>
422
423<ul>
424<li>symbols are case insensitive</li>
425<li>only one ternary conditional (e.g. x ? y : z) per statement</li>
426<li>statements are assignments or the final expression to return</li>
427<li>an assignment starts a statement, it is not an operator</li>
cristy8ee7f242013-06-20 16:08:44 +0000428<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 +0000429<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>
cristyce66ab12010-10-21 23:36:32 +0000430<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>
cristy3ed852e2009-09-05 21:47:34 +0000431</ul>
432
cristyce66ab12010-10-21 23:36:32 +0000433
cristy3ed852e2009-09-05 21:47:34 +0000434</div>
435
436<h3>Source Images</h3>
437<div class="doc-section">
438
cristy8ee7f242013-06-20 16:08:44 +0000439<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 +0000440
cristy8ee7f242013-06-20 16:08:44 +0000441<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 +0000442
cristy417eeb12013-07-28 18:48:22 +0000443<p class="crt"><span class="crtprompt"> $ </span><span class="crtin">convert image1.jpg image2.jpg image3.jpg -fx "(u+u[2])/2.0" image.jpg</span></p>
cristy8ee7f242013-06-20 16:08:44 +0000444<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 equivelent to <code>u</code> except when used in an escape sequence <code>%[fx:...]</code>. </p>
cristy3ed852e2009-09-05 21:47:34 +0000445
cristy751980d2012-06-03 23:18:35 +0000446<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 +0000447
cristy417eeb12013-07-28 18:48:22 +0000448<p class="crt"><span class="crtprompt"> $ </span><span class="crtin">convert -channel red logo: -flop logo: -resize "20%" -fx "(u+v)/2" image.jpg</span></p>
cristy3ed852e2009-09-05 21:47:34 +0000449<div class="image">
cristy417eeb12013-07-28 18:48:22 +0000450 <a href="../images/logo-sm-flop.png"><img src="../images/logo-sm-flop.png" width="128" height="96" alt="logo-sm-flop.png"></img></a>
451 <a href="../images/logo-sm.png"><img src="../images/logo-sm.png" width="128" height="96" alt="logo-sm.png"></img></a>
452<img style="margin-top:38px; margin-bottom:38px;" src="../images/right.gif" width="20" height="20" alt="==&gt;"></img>
453 <a href="../images/logo-sm-fx.png"><img src="../images/logo-sm-fx.png" width="128" height="96" alt="logo-sm-fx.png"></img></a>
cristy3ed852e2009-09-05 21:47:34 +0000454</div>
455
456
457</div>
458
459<h3>Accessing Pixels</h3>
460<div class="doc-section">
461
462<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>
463
cristy8ee7f242013-06-20 16:08:44 +0000464<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 +0000465
466<pre class="text">
467 p[-1].g green value of pixel to the immediate left of the current pixel
468 p[-1,-1].r red value of the pixel diagonally left and up from current pixel
469</pre>
470
471<p>To specify an absolute position, use braces, rather than brackets.</p>
472
473<pre class="text">
cristyee742642011-02-26 02:49:59 +0000474 p{0,0}.r red value of the pixel in the upper left corner of the image
cristy3ed852e2009-09-05 21:47:34 +0000475 p{12,34}.b blue pixel value at column number 12, row 34 of the image
476</pre>
477
cristy751980d2012-06-03 23:18:35 +0000478<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 +0000479
cristy751980d2012-06-03 23:18:35 +0000480<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 +0000481
482</div>
483
484<h3>Apply an Expression to Select Image Channels</h3>
485<div class="doc-section">
486
cristy8ee7f242013-06-20 16:08:44 +0000487<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 +0000488
cristy417eeb12013-07-28 18:48:22 +0000489<p class="crt"><span class="crtprompt"> $ </span><span class="crtin">convert alpha.png beta.png -channel red -fx "(u.g+v.g)/2" gamma.png</span></p>
cristy3ed852e2009-09-05 21:47:34 +0000490</div>
491
492<h3>Results</h3>
493<div class="doc-section">
494
cristy8ee7f242013-06-20 16:08:44 +0000495<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 +0000496
cristy8ee7f242013-06-20 16:08:44 +0000497<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 +0000498
499
cristy8ee7f242013-06-20 16:08:44 +0000500<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 +0000501
cristy417eeb12013-07-28 18:48:22 +0000502<p class="crt"><span class="crtprompt"> $ </span><span class="crtin">convert canvas:'rgb(25%,50%,75%)' rose: -colorspace rgb \ <br></br>
503 -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></br>
cristy3cd7cc92011-04-01 01:00:28 +0000504 Red channel of NW corner of image #1 is 0.184588</span></p>
cristy8ee7f242013-06-20 16:08:44 +0000505<p>Here we use the image indexes to <em>rotate</em> each image differently, and use <code>-set</code> with the image index to set a different <em>pause delay</em> on the first image in the animation:</p>
cristy3cd7cc92011-04-01 01:00:28 +0000506
cristy417eeb12013-07-28 18:48:22 +0000507<p class="crt"><span class="crtprompt"> $ </span><span class="crtin">convert rose: -duplicate 29 -virtual-pixel Gray -distort SRT '%[fx:360.0*t/n]' \ <br></br>
cristy8ee7f242013-06-20 16:08:44 +0000508 -set delay '%[fx:t == 0 ? 240 : 10]' -loop 0 rose.gif</span></p><p>The color-escape <code>%[pixel: ]</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 +0000509
510</div>
511</div>
cristyad41c0c2012-10-31 00:49:28 +0000512
513</div>
514
515<div id="linkbar">
cristyac1b4322013-07-28 13:58:06 +0000516 <span id="linkbar-west"> </span>
cristy8ee7f242013-06-20 16:08:44 +0000517
cristyad41c0c2012-10-31 00:49:28 +0000518 <span id="linkbar-center">
cristy149d58c2013-08-07 19:44:54 +0000519 <a href="http://pgp.mit.edu:11371/pks/lookup?op=get&amp;search=0x89AB63D48277377A">Public Key</a>
520 <a href="../discourse-server">Discourse Server</a>
cristy417eeb12013-07-28 18:48:22 +0000521 <a href="http://www.imagemagick.org/MagickStudio/scripts/MagickStudio.cgi">Studio</a>
cristy8ee7f242013-06-20 16:08:44 +0000522 <a href="http://jqmagick.imagemagick.org/">JqMagick</a>
cristyad41c0c2012-10-31 00:49:28 +0000523 </span>
cristyac1b4322013-07-28 13:58:06 +0000524 <span id="linkbar-east"> </span>
cristy149d58c2013-08-07 19:44:54 +0000525</div>
526<div class="footer">
527 <span id="footer-west">© 1999-2013 ImageMagick Studio LLC</span>
528 <span id="footer-east"> <a href="contact.html">Contact the Wizards</a></span>
529</div>
530<div style="clear: both; margin: 0; width: 100%; "></div>
cristyad41c0c2012-10-31 00:49:28 +0000531</body>
532</html>