ImageMagick: The Fx Special Effects Image Operator
Author: ImageMagick Studio LLC
Description: 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 (P [cell cut: 1167 characters, first 1000 kept]
Keywords: the, fx, special, effects, image, operator, ImageMagick, ImageMagic, MagickCore, MagickWand, PerlMagick, Magick++, RMagick, PythonMagick, JMagick, TclMagick, Image, Magick, Magic, Wand

## The Fx Special Effects Image Operator Use the Fx special effects image operator to apply a mathematical expression to an image or image channels. Use Fx to: move color values between images and channels colors translate, flip, mirror, rotate, scale, shear and transform images colors merge multiple images, in just about every way possible adjust image colors create canvases from existing images The expression can be simple--
      convert -size 64x64 xc:black -channel blue -fx '1/2' fx_navy.png
Here, we convert a black to a navy blue image: -- or complex:
        convert rose.jpg -fx '(1.0/(1.0+exp(10.0*(0.5-u)))-0.0066928509)*1.0092503' rose-sigmoidal.png
This expression results in a high contrast version of the image: See Using FX, The Special Effects Image Operator for more examples on how to apply a mathematical expression to an image or image channels. The next section discusses the Fx expression language. ## The Anatomy of an Fx Expression ##### The Fx Expression Language The formal Fx expression language is defined in this table:
      constants: QuantumRange, QuantumScale, Opaque, Transparent, e, Pi
      Fx operators (in order of precedence):
         ^ (power), *, /, % (modulo), +, -, <<, >>, <, <=, >, >=, ==, !=, & (bitwise AND),
         | (bitwise OR), && (logical AND), || (logical OR), ~ (logical NOT), ?: (ternary conditional)
      math functions:
         abs(), acos(), asin(), alt(), atan(), atan2(), ceil(), cos(), debug(), exp(), floor(), int(),
         log(), ln(), mod(), pow(), rand(), sign(), sin(), sqrt(), tan()
      color names: red, cyan, black, etc.
      color functions: rgb(), rgba(), cmyk(), cmyka(), hsl(), hsla()
      color hex values: #ccc, #cbfed0, #b9e1cc00
      symbols:
         s  = current image in sequence
         t  = scene number of current image in sequence
         u  = first image in sequence
         v  = second image in sequence
         n  = number of images in sequence
         i  = column offset
         j  = row offset
         p  = pixel to use (absolute or relative to current pixel)
         w  = width of this image
         h  = height of this image
         z  = channel depth
         r  = red value (from RGBA), of a specific or current pixel
         g  = green    ''
         b  = blue     ''
         a  = alpha    ''
         o  = opacity  ''
         c  = cyan value of CMYK color of pixel
         y  = yellow   ''
         m  = magenta  ''
         k  = black    ''
         intensity = pixel intensity
         hue = pixel hue
         saturation = pixel saturation
         luminosity = pixel luminosity
         image.resolution.x
         image.resolution.y
##### The Fx Expression The Fx expressions includes any combination of the following: x `^` y: exponentiation (xy) `(` ... `)`: grouping x `*` y: multiplication (the asterisk `*` is optional, for example, `2u` or `2(x+y)` are acceptable) x `/` y: division x `%` y: modulo x `+` y: addition x `-` y: subtraction x `<<` y: left shift x `>>` y: right shift x `<` y: boolean relation, returns value 1.0 if x < y, otherwise 0.0 x `<=` y: boolean relation, returns value 1.0 if x <= y, otherwise 0.0 x `>` y: boolean relation, returns value 1.0 if x > y, otherwise 0.0 x `>=` y: boolean relation, returns value 1.0 if x >= y, otherwise 0.0 x `==` y: boolean relation, returns value 1.0 if x == y, otherwise 0.0 x `!=` y: boolean relation, returns value 1.0 if x != y, otherwise 0.0 x `&` y: binary AND x `|` y: binary OR x `&&` y: logical AND connective, returns value 1.0 if x > 0 and y > 0, otherwise 0.0 x `||` y: logical OR connective (inclusive), returns value 1.0 if x > 0 or y > 0 (or both), otherwise 0.0 `~`x: logical NOT operator, returns value 1.0 if not x > 0, otherwise 0.0 `+`x: unary plus, returns 1.0*value `-`x: unary minus, returns -1.0*value x `?` y : z: ternary conditional expression, returns value y if x > 0, otherwise z; only one ternary conditional permitted per statement x `=` y: assignment x `;` y: statement separator `pi`: constant (3.141659...) `e`: constant (2.71828...) `QuantumRange`: constant maximum pixel value (255 for Q8, 65535 for Q16) `QuantumScale`: constant 1.0/`QuantumRange` `intensity`: pixel intensity `hue`: pixel hue `saturation`: pixel saturation `luminosity`: pixel luminosity `red, green, blue`, etc.: color names `#ccc, #cbfed0, #b9e1cc00`, etc.: color hex values `rgb(), rgba(), cmyk(), cmyka(), hsl(), hsla()`: color functions `s, t, u, v, n, i, j, w, h, z, r, g, b, a, o, c, y, m, k`: symbols `abs(`x`)`: absolute value function `acos(`x`)`: arc cosine function `alt(`x`)`: sign alternation function (returns 1 if `int(`x`)` is even, -1 if `int(`x`)` is odd) `asin(`x`)`: arc sine function `atan(`x`)`: arc tangent function `atan2(`y,x`)`: arc tangent function of two variables `ceil(`x`)`: smallest integral value not less than argument `cos(`x`)`: cosine function `debug(`x`)`: print value for debugging `exp(`x`)`: natural exponential function (ex) `floor(`x`)`: largest integral value not greater than argument `int(`x`)`: greatest integer function (returns greatest integer less than or equal to x) `ln(`x`)`: natural logarithm function `log(`x`)`: logarithm base 10 `max(`x, y`)`: maximum of x and y `min(`x, y`)`: minimum of x and y `mod(`x, y`)`: floating-point remainder function `pow(`x,y`)`: power function (xy) `rand()`: value uniformly distributed over the interval [0.0, 1.0) with a 2 to the 128th-1 period `sign(`x`)`: returns 1 if x is less than 0 otherwise 1 `sin(`x`)`: sine function `sqrt(`x`)`: square root function `tan(`x`)`: tangent function The expression semantics include these rules: symbols are case insensitive only one ternary conditional (e.g. x ? y : z) per statement statements are assignments or the final expression to return an assignment starts a statement, it is not an operator assignments to built-ins do not throw errors and have no effect; e.g. `r=3.0; r` returns the pixel red color value, not 3.0 ##### Source Image The default image for `p`, `r`, `g`, `b`, `a`, etc. is `s` which refers to the current image being looked at in %[fx: ] and %[pixel: ] escapes. The `-fx` image `s` defaults to the first (zeroth) image `u`. The index of `s` is given by `t`. The symbol `u` refers to the first (zeroth) image in the current image sequence, `v` the second image. Other images can be referred to using a index on any image reference (usually `u`), a negative index counts from the end. For example, `u[2]` is the third image, `u[-1]` is the last image, and `u[t]` is also image `s`. For example to reduce the intensity of the red channel by 50%, use.
      convert image.png -channel red -fx 'u/2.0' image.jpg
##### Accessing Pixels All color values are in the range of 0.0 to 1.0. The alpha channel ranges from 0.0 (fully transparent) to 1.0 (fully opaque). The pixels are processed one at a time, but a different pixel of a image can be specified with a pixel index represented by `p`. For example,
       p[-1].g      Green value of pixel to the immediate left of current
       p[-1,-1].r   Red value, diagonally left and up from current pixel
To specify an absolute position, use braces, rather than brackets.
      p{12,34}.b   is the blue pixel at image location 12,34
The other symbols specify the value you wish to retrieve. A pixel outside the boundary of the image has a value dictated by the -virtual-pixel option setting. Use the -interpolate setting to specify how pixels are blended. The default is bi-linear interpolation. ##### Apply an Expression to Select Image Channels The -channel setting can be used 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, suppose you want to replace the red channel of `alpha.png` with the average of the green channels from the images `alpha.png` and `beta.png`, use:
      convert alpha.png beta.png -channel red -fx '(u.g+v.g)/2' gamma.png
Integer pixel references retrieve the color of the pixel referred to, however non-integer references return a blended color according to the current -interpolate setting. ##### Results The `-fx` operator evaluates the given expression once for each pixel in the first u image for each defined -channel. The resulting value is placed in a copy (clone) of that first image. This new image replaces all images in the current image sequence. As such in the previous example, the updated `alpha.png` replaces both the original `alpha.png` and `beta.png` images, before being saved as `gamma.png` The current image `s` is set to the first `u` image, and `t` to its index, 0. The symbols `i` and `j` define the current pixel being processed. The value escape `%[fx: ]` is evaluated once only for each image in the current image sequence, with `s` and `t` specifying which image is being referred to `i` and `j` is set to zero and the current channel is set to red (-channel is ignored). The color escape `%[pixel: ]` is evaluated once per image and per color channel in that image (-channel is ignored), The values generated are then converted into a color string (named color or hex color value). The symbols `i` and `j` are set to zero and `s` and `t` define the current image. |

Discourse Server • Mailing Lists • ImageMagick Studio