blob: 528838d4d1765d8717595321d7f25871b9bcbc8b [file] [log] [blame]
cristy91469932014-07-25 11:00:25 +00001
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">
cristyad41c0c2012-10-31 00:49:28 +00009<head>
cristy91469932014-07-25 11:00:25 +000010 <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
11 <meta name=viewport content="width=device-width, initial-scale=0.3333334">
12 <meta name="google-site-verification" content="_bMOCDpkx9ZAzBwb2kF3PRHbfUUdFj2uO8Jd1AXArz4"/>
cristyffb69ed2010-12-25 00:06:48 +000013 <title>ImageMagick: The Fx Special Effects Image Operator</title>
cristy91469932014-07-25 11:00:25 +000014 <meta http-equiv="content-language" content="en-US"/>
15 <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
16 <meta http-equiv="reply-to" content="magick-users@imagemagick.org"/>
17 <meta name="application-name" content="ImageMagick"/>
18 <meta name="description" content="A collection of tools and libraries to read, write, and manipulate an image in any of the more popular image formats."/>
19 <meta name="application-url" content="http://www.imagemagick.org"/>
20 <meta name="generator" content="PHP"/>
21 <meta name="keywords" content="the, fx, special, effects, image, operator, convert, edit, compose, image, pixel"/>
22 <meta name="rating" content="GENERAL"/>
23 <meta name="robots" content="INDEX, FOLLOW"/>
24 <meta name="generator" content="ImageMagick Studio LLC"/>
25 <meta name="author" content="ImageMagick Studio LLC"/>
26 <meta name="revisit-after" content="2 DAYS"/>
27 <meta name="resource-type" content="document"/>
28 <meta name="copyright" content="Copyright (c) 1999-2012 ImageMagick Studio LLC"/>
29 <meta name="distribution" content="Global"/>
30 <link rel="icon" href="../images/wand.png"/>
31 <link rel="shortcut icon" href="../images/wand.ico" type="images/x-icon"/>
32 <link rel="canonical" href="http://www.imagemagick.org" />
33 <link rel="meta" type="application/rdf+xml" title="ICI" href="http://imagemagick.org/ici.rdf"/>
cristy8ee7f242013-06-20 16:08:44 +000034
cristyad41c0c2012-10-31 00:49:28 +000035 <!-- Add jQuery library -->
cristy91469932014-07-25 11:00:25 +000036 <script type="text/javascript" src="http://localhost/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
cristyad41c0c2012-10-31 00:49:28 +000037
cristy8ee7f242013-06-20 16:08:44 +000038 <!-- Add mousewheel plugin (this is optional) -->
39 <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 +000040
41 <!-- Add fancyBox -->
cristy91469932014-07-25 11:00:25 +000042 <link rel="stylesheet" href="http://www.imagemagick.org/fancybox/source/jquery.fancybox.css?v=2.1.5" type="text/css" media="screen" />
43 <script type="text/javascript" src="http://www.imagemagick.org/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
cristyad41c0c2012-10-31 00:49:28 +000044
cristy8ee7f242013-06-20 16:08:44 +000045 <!-- Optionally add helpers - button, thumbnail and/or media -->
cristy91469932014-07-25 11:00:25 +000046 <link rel="stylesheet" href="http://www.imagemagick.org/fancybox/source/helpers/jquery.fancybox-buttons.css?v=1.0.5" type="text/css" media="screen" />
47 <script type="text/javascript" src="http://www.imagemagick.org/fancybox/source/helpers/jquery.fancybox-buttons.js?v=1.0.5"></script>
48 <script type="text/javascript" src="http://www.imagemagick.org/fancybox/source/helpers/jquery.fancybox-media.js?v=1.0.6"></script>
cristyad41c0c2012-10-31 00:49:28 +000049
cristy91469932014-07-25 11:00:25 +000050 <link rel="stylesheet" href="http://www.imagemagick.org/fancybox/source/helpers/jquery.fancybox-thumbs.css?v=1.0.7" type="text/css" media="screen" />
51 <script type="text/javascript" src="http://www.imagemagick.org/fancybox/source/helpers/jquery.fancybox-thumbs.js?v=1.0.7"></script>
cristy8ee7f242013-06-20 16:08:44 +000052
cristyad41c0c2012-10-31 00:49:28 +000053 <script type="text/javascript">
54 $(document).ready(function() {
55 $(".fancybox").fancybox();
56 });
57 </script>
cristy8ee7f242013-06-20 16:08:44 +000058
cristyad41c0c2012-10-31 00:49:28 +000059 <!-- ImageMagick style -->
cristyad41c0c2012-10-31 00:49:28 +000060 <style type="text/css" media="all">
61 @import url("../www/magick.css");
62 </style>
63</head>
64
65<body id="www-imagemagick-org">
cristy8ee7f242013-06-20 16:08:44 +000066
cristy91469932014-07-25 11:00:25 +000067<script type="text/javascript">
68 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
69 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
70 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
71 })(window,document,'offline-script','//www.google-analytics.com/analytics.js','ga');
cristy8ee7f242013-06-20 16:08:44 +000072
cristy91469932014-07-25 11:00:25 +000073 ga('create', 'UA-40133312-1', 'imagemagick.org');
74 ga('send', 'pageview');
75</script>
cristy8ee7f242013-06-20 16:08:44 +000076
cristyad41c0c2012-10-31 00:49:28 +000077<div class="titlebar">
78<div style="margin: 17px auto; float: left;">
79 <script type="text/javascript">
80 <!--
81 google_ad_client = "pub-3129977114552745";
82 google_ad_slot = "5439289906";
83 google_ad_width = 728;
84 google_ad_height = 90;
85 //-->
86 </script>
cristy91469932014-07-25 11:00:25 +000087 <script type="text/javascript"
88 src="http://localhost/pagead/show_ads.js">
89 </script>
cristyad41c0c2012-10-31 00:49:28 +000090</div>
cristy149d58c2013-08-07 19:44:54 +000091<a href="../discourse-server">
cristy91469932014-07-25 11:00:25 +000092 <img src="../images/logo.jpg"
93 alt="ImageMagick Logo" width="123" height="118"
94 style="width: 123px; height: 118px; border: 0px; float: right;" /></a>
cristyf1b8a092014-02-20 15:46:11 +000095<a href="http://www.imagemagick.org/ImageMagick-7.0.0/index.html">
cristy91469932014-07-25 11:00:25 +000096 <img src="../images/sprite.jpg"
97 alt="ImageMagick Sprite" width="114" height="118"
98 style="width: 114px; height: 118px; border: 0px; float: right;" /></a>
cristyad41c0c2012-10-31 00:49:28 +000099</div>
100
101<div class="westbar">
102
cristy3ed852e2009-09-05 21:47:34 +0000103<div class="menu">
cristyf1b8a092014-02-20 15:46:11 +0000104 <a title="About ImageMagick" href="http://www.imagemagick.org/ImageMagick-7.0.0/index.html">About ImageMagick</a>
cristy3ed852e2009-09-05 21:47:34 +0000105</div>
cristy3ed852e2009-09-05 21:47:34 +0000106<div class="menu">
cristy751980d2012-06-03 23:18:35 +0000107 <a title="Binary Releases" href="binary-releases.html">Binary Releases</a>
cristybb503372010-05-27 20:51:26 +0000108</div>
109<div class="sub">
cristy751980d2012-06-03 23:18:35 +0000110 <a title="Binary Release: Unix" href="binary-releases.html#unix">Unix</a>
cristybb503372010-05-27 20:51:26 +0000111</div>
112<div class="sub">
cristy751980d2012-06-03 23:18:35 +0000113 <a title="Binary Release: MacOS X" href="binary-releases.html#macosx">Mac OS X</a>
cristybb503372010-05-27 20:51:26 +0000114</div>
115<div class="sub">
cristy751980d2012-06-03 23:18:35 +0000116 <a title="Binary Release: iOS" href="binary-releases.html#iOS">iOS</a>
cristyda16f162011-02-19 23:52:17 +0000117</div>
118<div class="sub">
cristy751980d2012-06-03 23:18:35 +0000119 <a title="Binary Release: Windows" href="binary-releases.html#windows">Windows</a>
cristybb503372010-05-27 20:51:26 +0000120</div>
cristyad41c0c2012-10-31 00:49:28 +0000121<div class="sep"></div>
cristybb503372010-05-27 20:51:26 +0000122<div class="menu">
cristy751980d2012-06-03 23:18:35 +0000123 <a title="Command-line Tools" href="command-line-tools.html">Command-line Tools</a>
cristy3ed852e2009-09-05 21:47:34 +0000124</div>
125<div class="sub">
cristy751980d2012-06-03 23:18:35 +0000126 <a title="Command-line Tools: Processing" href="command-line-processing.html">Processing</a>
cristy3ed852e2009-09-05 21:47:34 +0000127</div>
128<div class="sub">
cristy751980d2012-06-03 23:18:35 +0000129 <a title="Command-line Tools: Options" href="command-line-options.html">Options</a>
cristy3ed852e2009-09-05 21:47:34 +0000130</div>
131<div class="sub">
cristy83a272e2010-05-07 20:40:35 +0000132 <a title="Command-line Tools: Usage" href="http://www.imagemagick.org/Usage/">Usage</a>
cristy3ed852e2009-09-05 21:47:34 +0000133</div>
134<div class="menu">
cristy751980d2012-06-03 23:18:35 +0000135 <a title="Program Interfaces" href="api.html">Program Interfaces</a>
cristy3ed852e2009-09-05 21:47:34 +0000136</div>
137<div class="sub">
cristy751980d2012-06-03 23:18:35 +0000138 <a title="Program Interface: MagickWand" href="magick-wand.html">MagickWand</a>
cristy3ed852e2009-09-05 21:47:34 +0000139</div>
140<div class="sub">
cristy751980d2012-06-03 23:18:35 +0000141 <a title="Program Interface: MagickCore" href="magick-core.html">MagickCore</a>
cristy3ed852e2009-09-05 21:47:34 +0000142</div>
143<div class="sub">
cristy751980d2012-06-03 23:18:35 +0000144 <a title="Program Interface: PerlMagick" href="perl-magick.html">PerlMagick</a>
cristy3ed852e2009-09-05 21:47:34 +0000145</div>
146<div class="sub">
cristy751980d2012-06-03 23:18:35 +0000147 <a title="Program Interface: Magick++" href="magick++.html">Magick++</a>
cristy3ed852e2009-09-05 21:47:34 +0000148</div>
cristyad41c0c2012-10-31 00:49:28 +0000149<div class="sep"></div>
cristy91469932014-07-25 11:00:25 +0000150<div class="menu">
cristy751980d2012-06-03 23:18:35 +0000151 <a title="Install from Source" href="install-source.html">Install from Source</a>
cristy3ed852e2009-09-05 21:47:34 +0000152</div>
153<div class="sub">
cristy751980d2012-06-03 23:18:35 +0000154 <a title="Install from Source: Unix" href="install-source.html#unix">Unix</a>
cristy3ed852e2009-09-05 21:47:34 +0000155</div>
156<div class="sub">
cristy751980d2012-06-03 23:18:35 +0000157 <a title="Install from Source: Windows" href="install-source.html#windows">Windows</a>
cristy3ed852e2009-09-05 21:47:34 +0000158 </div>
159<div class="menu">
cristy751980d2012-06-03 23:18:35 +0000160 <a title="Resources" href="resources.html">Resources</a>
cristy3ed852e2009-09-05 21:47:34 +0000161</div>
cristybb503372010-05-27 20:51:26 +0000162<div class="menu">
cristy751980d2012-06-03 23:18:35 +0000163 <a title="Architecture" href="architecture.html">Architecture</a>
cristybb503372010-05-27 20:51:26 +0000164</div>
cristy3ed852e2009-09-05 21:47:34 +0000165<div class="menu">
cristy751980d2012-06-03 23:18:35 +0000166 <a title="Download" href="download.html">Download</a>
cristy3ed852e2009-09-05 21:47:34 +0000167</div>
cristyad41c0c2012-10-31 00:49:28 +0000168<div class="sep"></div>
cristy3ed852e2009-09-05 21:47:34 +0000169<div class="menu">
cristy8ee7f242013-06-20 16:08:44 +0000170 <a title="Search" href="search.html">Search</a>
cristy3ed852e2009-09-05 21:47:34 +0000171</div>
cristyad41c0c2012-10-31 00:49:28 +0000172<div class="sep"></div>
cristy3ed852e2009-09-05 21:47:34 +0000173<div class="menu">
cristy751980d2012-06-03 23:18:35 +0000174 <a title="Site Map" href="sitemap.html">Site Map</a>
cristy3ed852e2009-09-05 21:47:34 +0000175</div>
cristy91469932014-07-25 11:00:25 +0000176<div class="sub">
cristy751980d2012-06-03 23:18:35 +0000177 <a title="Site Map: Links" href="links.html">Links</a>
cristy3ed852e2009-09-05 21:47:34 +0000178</div>
cristyad41c0c2012-10-31 00:49:28 +0000179<div class="sep"></div>
cristyad41c0c2012-10-31 00:49:28 +0000180</div>
181
182<div class="eastbar">
cristy91469932014-07-25 11:00:25 +0000183 <script type="text/javascript">
184 /* <![CDATA[ */
185 window.___gcfg = {
186 lang: 'en-US'
187 };
188
189 (function() {
190 var po = document.createElement('offline-script');
191 po.type = 'text/javascript';
192 po.async = true;
193 po.src = 'https://apis.google.com/js/plusone.js';
194 var script = document.getElementsByTagName('offline-script')[0];
195 script.parentNode.insertBefore(po, script);
196 })();
197 var gplusone = document.getElementById("gplusone");
198 gplusone.setAttribute("data-size","medium");
199 gplusone.setAttribute("data-count","true");
200 /* ]]> */
201 </script>
202 <script type="text/javascript">
203 /* <![CDATA[ */
204 (function() {
205 var po = document.createElement('offline-script'), t = document.getElementsByTagName('offline-script')[0];
206 po.type = 'text/javascript';
207 po.async = true;
208 po.src = 'http://api.flattr.com/js/0.6/load.js?mode=auto';
209 var script = document.getElementsByTagName('offline-script')[0];
210 script.parentNode.insertBefore(po, script);
211 })();
212 /* ]]> */
213 </script>
cristyf1b8a092014-02-20 15:46:11 +0000214 <div> <a class="FlattrButton" style="display:none;" rev="flattr;button:compact;" href="http://www.imagemagick.org/"></a></div>
cristy8ee7f242013-06-20 16:08:44 +0000215 <noscript>
216 <a href="http://flattr.com/thing/947300/Convert-Edit-And-Compose-Images" target="_blank">
cristy91469932014-07-25 11:00:25 +0000217 <img src="http://api.flattr.com/button/flattr-badge-large.png" alt="Flattr this" title="Flattr this" width="93" height="20" border="0" /></a>
cristy8ee7f242013-06-20 16:08:44 +0000218 </noscript>
cristy91469932014-07-25 11:00:25 +0000219 <div class="g-plusone" data-size="standard" data-count="true"></div>
cristyad41c0c2012-10-31 00:49:28 +0000220</div>
221
222<div class="main">
223
cristydcca3fa2010-12-29 21:50:07 +0000224<h1>Apply Special Effects to an Image with an Fx Expression</h1>
cristy417eeb12013-07-28 18:48:22 +0000225<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 +0000226
cristydcca3fa2010-12-29 21:50:07 +0000227<a id="fx"></a>
cristy3ed852e2009-09-05 21:47:34 +0000228<div class="doc-section">
229
230<p>Use the Fx special effects image operator to apply a mathematical expression to an image or image channels. Use Fx to:</p>
231
232<ul>
233 <li>create canvases, gradients, mathematical colormaps</li>
234 <li>move color values between images and channels</li>
235 <li>translate, flip, mirror, rotate, scale, shear and generally distort images</li>
236 <li>merge or composite multiple images together</li>
237 <li>convolve or merge neighboring pixels together</li>
238 <li>generate image metrics or 'fingerprints'</li>
239</ul>
240
241<p>The expression can be simple:</p>
242
cristy91469932014-07-25 11:00:25 +0000243<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>
cristy3ed852e2009-09-05 21:47:34 +0000244<p>Here, we convert a black to a navy blue image:</p>
245
246<p class="image">
cristy91469932014-07-25 11:00:25 +0000247 <a href="../images/black.png"><img src="../images/black.png" width="64" height="64" alt="black" /></a>
248 <img style="margin-top:22px; margin-bottom:22px;" src="../images/right.gif" width="20" height="20" alt="==>" />
249 <a href="../images/navy.png"><img src="../images/navy.png" width="64" height="64" alt="navy" /></a>
cristy3ed852e2009-09-05 21:47:34 +0000250</p>
251
252<p>Or the expression can be complex:</p>
253
cristy91469932014-07-25 11:00:25 +0000254<p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert rose.jpg \ <br/>
255 -fx "(1.0/(1.0+exp(10.0*(0.5-u)))-0.006693)*1.0092503" \ <br/>
cristy3ed852e2009-09-05 21:47:34 +0000256 rose-sigmoidal.png</span></p>
257<p>This expression results in a high contrast version of the image:</p>
258
259<p class="image">
cristy91469932014-07-25 11:00:25 +0000260 <a href="../images/rose.jpg"><img src="../images/rose.jpg" width="70" height="46" alt="rose" /></a>
261 <img style="margin-top:13px; margin-bottom:13px;" src="../images/right.gif" width="20" height="20" alt="==>" />
262 <a href="../images/rose-sigmoidal.png"><img src="../images/rose-sigmoidal.png" width="70" height="46" alt="rose-sigmoidal" /></a>
cristy3ed852e2009-09-05 21:47:34 +0000263</p>
264
265<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>
266
cristy91469932014-07-25 11:00:25 +0000267<p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert -size 70x70 canvas: \ <br/>
268 -fx "Xi=i-w/2; Yj=j-h/2; 1.2*(0.5-hypot(Xi,Yj)/70.0)+0.5" \ <br/>
cristy3ed852e2009-09-05 21:47:34 +0000269 radial-gradient.png</span></p>
270<p>The command above returns this image:</p>
271
272<p class="image">
cristy91469932014-07-25 11:00:25 +0000273 <a href="../images/radial-gradient.png"><img src="../images/radial-gradient.png" width="70" height="70" alt="radial-gradient" /></a>
cristy3ed852e2009-09-05 21:47:34 +0000274</p>
cristye9a32c32011-04-07 01:11:05 +0000275
276<p>This FX expression adds random noise to an image:</p>
277
cristy91469932014-07-25 11:00:25 +0000278<p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert photo.jpg -fx 'iso=32; rone=rand(); rtwo=rand(); \<br />
279 myn=sqrt(-2*ln(rone))*cos(2*Pi*rtwo); myntwo=sqrt(-2*ln(rtwo))* \<br />
280 cos(2*Pi*rone); pnoise=sqrt(p)*myn*sqrt(iso)* \ <br />
cristye9a32c32011-04-07 01:11:05 +0000281 channel(4.28,3.86,6.68,0)/255; max(0,p+pnoise)' noisy.png</span></p>
cristy3ed852e2009-09-05 21:47:34 +0000282<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>
283
284<p>The next section discusses the Fx expression language.</p>
285
286</div>
287
cristydcca3fa2010-12-29 21:50:07 +0000288<h2><a id="anatomy"></a>The Anatomy of an Fx Expression</h2>
cristy3ed852e2009-09-05 21:47:34 +0000289<div class="doc-section">
290
291<h3>The Fx Expression Language</h3>
292<div class="doc-section">
293
294<p>The formal Fx expression language is defined here:</p>
295
296<dl class="doc">
297 <dt class="doc"> numbers:</dt>
cristyeed6cb62012-01-28 19:38:34 +0000298 <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 +0000299 <dt class="doc"> constants: </dt>
cristyeed6cb62012-01-28 19:38:34 +0000300 <dd> E (Euler's number), Epsilon, QuantumRange, QuantumScale, Opaque, Phi (golden ratio), Pi, Transparent</dd>
cristy3ed852e2009-09-05 21:47:34 +0000301 <dt class="doc"> Fx operators (in order of precedence): </dt>
cristyd92fed52010-10-25 13:48:19 +0000302 <dd> ^ (power), unary -, *, /, % (modulo), +, -,
cristy3ed852e2009-09-05 21:47:34 +0000303 &lt;&lt;, &gt;&gt;, &lt;, &lt;=, &gt;, &gt;=, ==, !=,
304 &amp; (bitwise AND), | (bitwise OR),
305 &amp;&amp; (logical AND), || (logical OR),
306 ~ (logical NOT), ?: (ternary conditional)</dd>
307 <dt class="doc"> math functions: </dt>
cristyeed6cb62012-01-28 19:38:34 +0000308 <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 +0000309 <dt class="doc"> channel functions: </dt>
310 <dd> channel(r,g,b,a), channel(c,m,y,k,a)</dd>
311 <dt class="doc"> color names:</dt>
312 <dd> red, cyan, black, etc.</dd>
313 <dt class="doc"> color functions:</dt>
cristy751980d2012-06-03 23:18:35 +0000314 <dd> srgb(), srgba(), rgb(), rgba(), cmyk(), cmyka(), hsl(), hsla(), etc.</dd>
cristy3ed852e2009-09-05 21:47:34 +0000315 <dt class="doc"> color hex values:</dt>
cristy751980d2012-06-03 23:18:35 +0000316 <dd> #ccc, #cbfed0, #b9e1cc00, etc.</dd>
cristy3ed852e2009-09-05 21:47:34 +0000317 <dt class="doc"> symbols:</dt>
318 <dd style="white-space:pre">
cristy8ee7f242013-06-20 16:08:44 +0000319 u = first image in list
320 v = second image in list
321 s = current image in list (for %[fx:] otherwise = u)
322 t = index of current image (s) in list
323 n = number of images in list
cristy3ed852e2009-09-05 21:47:34 +0000324
325 i = column offset
326 j = row offset
327 p = pixel to use (absolute or relative to current pixel)
328
329 w = width of this image
330 h = height of this image
331 z = channel depth
332
333 r = red value (from RGBA), of a specific or current pixel
334 g = green ''
335 b = blue ''
336 a = alpha ''
337 o = opacity ''
338
339 c = cyan value of CMYK color of pixel
340 y = yellow ''
341 m = magenta ''
342 k = black ''
343
344 intensity = pixel intensity
345
346 hue = pixel hue
347 saturation = pixel saturation
348 lightness = pixel lightness
cristy149d58c2013-08-07 19:44:54 +0000349 luma = pixel luma
cristy3ed852e2009-09-05 21:47:34 +0000350
351 page.width = page width
352 page.height = page height
353 page.x = page x offset
354 page.y = page y offset
355
356 resolution.x = x resolution
357 resolution.y = y resolution
358
359 depth = image depth
360 minima = image minima
361 maxima = image maxima
362 mean = image mean
363 standard_deviation = image standard deviation
364 kurtosis = image kurtosis
365 skewness = image skewness
366 (add a channel specifier to compute a statistic for that channel, e.g. depth.r)
367 </dd>
cristyeed6cb62012-01-28 19:38:34 +0000368 <dt class="doc"> iterators:</dt>
369 <dd> while()</dd>
cristy3ed852e2009-09-05 21:47:34 +0000370</dl>
371
372</div>
373
374<h3>The Fx Expression</h3>
375<div class="doc-section">
376
377<p>An Fx expression may include any combination of the following:</p>
378<ul>
cristy8ee7f242013-06-20 16:08:44 +0000379<li> <em>x</em> <code>^</code> <em>y</em>: exponentiation (<em>x<sup>y</sup></em>)</li>
380<li> <code>(</code> ... <code>)</code>: grouping</li>
381<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>
382<li> <em>x</em> <code>/</code> <em>y</em>: division</li>
383<li> <em>x</em> <code>%</code> <em>y</em>: modulo</li>
384<li> <em>x</em> <code>+</code> <em>y</em>: addition</li>
385<li> <em>x</em> <code>-</code> <em>y</em>: subtraction</li>
386<li> <em>x</em> <code>&lt;&lt;</code> <em>y</em>: left shift</li>
387<li> <em>x</em> <code>&gt;&gt;</code> <em>y</em>: right shift</li>
388<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>
389<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>
390<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>
391<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>
392<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>
393<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>
394<li> <em>x</em> <code>&amp;</code> <em>y</em>: binary AND</li>
395<li> <em>x</em> <code>|</code> <em>y</em>: binary OR</li>
396<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>
397<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>
398<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>
399<li> <code>+</code><em>x</em>: unary plus, return 1.0*value</li>
400<li> <code>-</code><em>x</em>: unary minus, return -1.0*value</li>
401<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>
402<li> <em>x</em> <code>=</code> <em>y</em>: assignment; assignment variables are restricted to letter combinations only (e.g. Xi not X1)</li>
403<li> <em>x</em> <code>;</code> <em>y</em>: statement separator </li>
404<li> <code>phi</code>: constant (1.618034...)</li>
405<li> <code>pi</code>: constant (3.141659...)</li>
406<li> <code>e</code>: constant (2.71828...)</li>
407<li> <code>QuantumRange</code>: constant maximum pixel value (255 for Q8, 65535 for Q16)</li>
408<li> <code>QuantumScale</code>: constant 1.0/<code>QuantumRange</code></li>
409<li> <code>intensity</code>: pixel intensity; equivalent to 0.299*red+0.587*green+0.114*blue</li>
410<li> <code>hue</code>: pixel hue</li>
411<li> <code>saturation</code>: pixel saturation</li>
412<li> <code>lightness</code>: pixel lightness; equivalent to 0.5*max(red,green,blue) + 0.5*min(red,green,blue)</li>
413<li> <code>luminance</code>: pixel luminance; equivalent to 0.212656*red + 0.715158*green + 0.072186*blue</li>
414<li> <code>red, green, blue</code>, etc.: color names</li>
415<li> <code>#ccc, #cbfed0, #b9e1cc00</code>, etc.: color hex values</li>
416<li> <code>rgb(), rgba(), cmyk(), cmyka(), hsl(), hsla()</code>: color functions</li>
417<li> <code>s, t, u, v, n, i, j, w, h, z, r, g, b, a, o, c, y, m, k</code>: symbols</li>
418<li> <code>abs(</code><em>x</em><code>)</code>: absolute value function</li>
419<li> <code>acos(</code><em>x</em><code>)</code>: arc cosine function</li>
420<li> <code>acosh(</code><em>x</em><code>)</code>: inverse hyperbolic cosine function</li>
421<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>
422<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>
423<li> <code>asin(</code><em>x</em><code>)</code>: arc sine function</li>
424<li> <code>asinh(</code><em>x</em><code>)</code>: inverse hyperbolic sine function</li>
425<li> <code>atan(</code><em>x</em><code>)</code>: arc tangent function</li>
426<li> <code>atanh(</code><em>x</em><code>)</code>: inverse hyperbolic tangent function</li>
427<li> <code>atan2(</code><em>y</em>,<em>x</em><code>)</code>: arc tangent function of two variables</li>
428<li> <code>ceil(</code><em>x</em><code>)</code>: smallest integral value not less than argument</li>
429<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>
430<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>
431<li> <code>cos(</code><em>x</em><code>)</code>: cosine function</li>
432<li> <code>cosh(</code><em>x</em><code>)</code>: hyperbolic cosine function</li>
433<li> <code>debug(</code><em>x</em><code>)</code>: print <em>x</em> (useful for debugging your expression)</li>
434<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>
435<li> <code>exp(</code><em>x</em><code>)</code>: natural exponential function (<em>e<sup>x</sup></em>)</li>
436<li> <code>floor(</code><em>x</em><code>)</code>: largest integral value not greater than argument</li>
437<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>
438<li> <code>gcd(</code><em>x</em>,<em>y</em><code>)</code>: greatest common denominator</li>
439<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>
440<li> <code>int(</code><em>x</em><code>)</code>: greatest integer function (return greatest integer less than or equal to <em>x</em>)</li>
441<li> <code>isnan(</code><em>x</em><code>)</code>: return 1.0 if <em>x</em> is NAN, 0.0 otherwise</li>
442<li> <code>j0(</code><em>x</em><code>)</code>: Bessel functions of <em>x</em> of the first kind of order 0</li>
443<li> <code>j1(</code><em>x</em><code>)</code>: Bessel functions of <em>x</em> of the first kind of order 1</li>
444<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>
445<li> <code>ln(</code><em>x</em><code>)</code>: natural logarithm function</li>
446<li> <code>log(</code><em>x</em><code>)</code>: logarithm base 10</li>
447<li> <code>logtwo(</code><em>x</em><code>)</code>: logarithm base 2</li>
448<li> <code>ln(</code><em>x</em><code>)</code>: natural logarithm</li>
449<li> <code>max(</code><em>x</em>, <em>y</em><code>)</code>: maximum of <em>x</em> and <em>y</em></li>
450<li> <code>min(</code><em>x</em>, <em>y</em><code>)</code>: minimum of <em>x</em> and <em>y</em></li>
451<li> <code>mod(</code><em>x</em>, <em>y</em><code>)</code>: floating-point remainder function</li>
452<li> <code>not(</code><em>x</em><code>)</code>: return 1.0 if <em>x</em> is zero, 0.0 otherwise</li>
453<li> <code>pow(</code><em>x</em>,<em>y</em><code>)</code>: power function (<em>x<sup>y</sup></em>)</li>
454<li> <code>rand()</code>: value uniformly distributed over the interval [0.0, 1.0) with a 2 to the 128th-1 period</li>
455<li> <code>round()</code>: round to integral value, regardless of rounding direction</li>
456<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>
457<li> <code>sin(</code><em>x</em><code>)</code>: sine function</li>
458<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>
459<li> <code>squish(</code><em>x</em><code>)</code>: squish function; squish(<em>x</em>)=1.0/(1.0+exp(<em>-x</em>))</li>
460<li> <code>sinh(</code><em>x</em><code>)</code>: hyperbolic sine function</li>
461<li> <code>sqrt(</code><em>x</em><code>)</code>: square root function</li>
462<li> <code>tan(</code><em>x</em><code>)</code>: tangent function</li>
463<li> <code>tanh(</code><em>x</em><code>)</code>: hyperbolic tangent function</li>
464<li> <code>trunc(</code><em>x</em><code>)</code>: round to integer, towards zero</li>
465<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 +0000466</ul>
467
468<p>The expression semantics include these rules:</p>
469
470<ul>
471<li>symbols are case insensitive</li>
472<li>only one ternary conditional (e.g. x ? y : z) per statement</li>
473<li>statements are assignments or the final expression to return</li>
474<li>an assignment starts a statement, it is not an operator</li>
cristy8ee7f242013-06-20 16:08:44 +0000475<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 +0000476<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 +0000477<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 +0000478</ul>
479
cristyce66ab12010-10-21 23:36:32 +0000480
cristy3ed852e2009-09-05 21:47:34 +0000481</div>
482
483<h3>Source Images</h3>
484<div class="doc-section">
485
cristy8ee7f242013-06-20 16:08:44 +0000486<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 +0000487
cristy8ee7f242013-06-20 16:08:44 +0000488<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 +0000489
cristy91469932014-07-25 11:00:25 +0000490<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>
cristy8ee7f242013-06-20 16:08:44 +0000491<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 +0000492
cristy751980d2012-06-03 23:18:35 +0000493<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 +0000494
cristy91469932014-07-25 11:00:25 +0000495<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>
cristy3ed852e2009-09-05 21:47:34 +0000496<div class="image">
cristy91469932014-07-25 11:00:25 +0000497 <a href="../images/logo-sm-flop.png"><img src="../images/logo-sm-flop.png" width="128" height="96" alt="logo-sm-flop.png" /></a>
498 <a href="../images/logo-sm.png"><img src="../images/logo-sm.png" width="128" height="96" alt="logo-sm.png" /></a>
499<img style="margin-top:38px; margin-bottom:38px;" src="../images/right.gif" width="20" height="20" alt="==>" />
500 <a href="../images/logo-sm-fx.png"><img src="../images/logo-sm-fx.png" width="128" height="96" alt="logo-sm-fx.png" /></a>
cristy3ed852e2009-09-05 21:47:34 +0000501</div>
502
503
504</div>
505
506<h3>Accessing Pixels</h3>
507<div class="doc-section">
508
509<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>
510
cristy8ee7f242013-06-20 16:08:44 +0000511<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 +0000512
513<pre class="text">
514 p[-1].g green value of pixel to the immediate left of the current pixel
515 p[-1,-1].r red value of the pixel diagonally left and up from current pixel
516</pre>
517
518<p>To specify an absolute position, use braces, rather than brackets.</p>
519
520<pre class="text">
cristyee742642011-02-26 02:49:59 +0000521 p{0,0}.r red value of the pixel in the upper left corner of the image
cristy3ed852e2009-09-05 21:47:34 +0000522 p{12,34}.b blue pixel value at column number 12, row 34 of the image
523</pre>
524
cristy751980d2012-06-03 23:18:35 +0000525<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 +0000526
cristy751980d2012-06-03 23:18:35 +0000527<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 +0000528
529</div>
530
531<h3>Apply an Expression to Select Image Channels</h3>
532<div class="doc-section">
533
cristy8ee7f242013-06-20 16:08:44 +0000534<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 +0000535
cristy91469932014-07-25 11:00:25 +0000536<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>
cristy3ed852e2009-09-05 21:47:34 +0000537</div>
538
539<h3>Results</h3>
540<div class="doc-section">
541
cristy8ee7f242013-06-20 16:08:44 +0000542<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 +0000543
cristy8ee7f242013-06-20 16:08:44 +0000544<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 +0000545
546
cristy8ee7f242013-06-20 16:08:44 +0000547<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 +0000548
cristy91469932014-07-25 11:00:25 +0000549<p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert canvas:'rgb(25%,50%,75%)' rose: -colorspace rgb \ <br/>
550 -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/>
cristy3cd7cc92011-04-01 01:00:28 +0000551 Red channel of NW corner of image #1 is 0.184588</span></p>
cristy8ee7f242013-06-20 16:08:44 +0000552<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 +0000553
cristy91469932014-07-25 11:00:25 +0000554<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 />
cristy8ee7f242013-06-20 16:08:44 +0000555 -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 +0000556
557</div>
558</div>
cristyad41c0c2012-10-31 00:49:28 +0000559
560</div>
561
562<div id="linkbar">
cristyac1b4322013-07-28 13:58:06 +0000563 <span id="linkbar-west"> </span>
cristy8ee7f242013-06-20 16:08:44 +0000564
cristyad41c0c2012-10-31 00:49:28 +0000565 <span id="linkbar-center">
cristy149d58c2013-08-07 19:44:54 +0000566 <a href="../discourse-server">Discourse Server</a>
cristy91469932014-07-25 11:00:25 +0000567 <a href="http://pgp.mit.edu:11371/pks/lookup?op=get&amp;search=0x89AB63D48277377A">Public Key</a>
cristy417eeb12013-07-28 18:48:22 +0000568 <a href="http://www.imagemagick.org/MagickStudio/scripts/MagickStudio.cgi">Studio</a>
cristy8ee7f242013-06-20 16:08:44 +0000569 <a href="http://jqmagick.imagemagick.org/">JqMagick</a>
cristyad41c0c2012-10-31 00:49:28 +0000570 </span>
cristyac1b4322013-07-28 13:58:06 +0000571 <span id="linkbar-east"> </span>
cristy149d58c2013-08-07 19:44:54 +0000572</div>
573<div class="footer">
cristyb56bb242014-11-25 17:12:48 +0000574 <span id="footer-west">© 1999-2015 ImageMagick Studio LLC</span>
cristy149d58c2013-08-07 19:44:54 +0000575 <span id="footer-east"> <a href="contact.html">Contact the Wizards</a></span>
576</div>
577<div style="clear: both; margin: 0; width: 100%; "></div>
cristyad41c0c2012-10-31 00:49:28 +0000578</body>
579</html>