[ImageMagick] [sponsor]
Processing
Options
Usage
MagickWand
MagickCore
PerlMagick
Magick++
Unix
Windows
Unix
Mac OS X
Windows
Links

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:

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:

black ==> navy

-- or complex:

  convert rose.jpg \
          -fx "(1.0/(1.0+exp(10.0*(0.5-u)))-0.006693)*1.0092503" \
          rose-sigmoidal.png

This expression results in a high contrast version of the image:

rose ==> rose-sigmoidal

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:

  convert -size 70x70 xc: \
          -fx "Xi=i-w/2; Yj=j-h/2; 1.2*(0.5-hypot(Xi,Yj)/70.0)+0.5" \
          radial-gradient.png

These statements return this image:

radial-gradient

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 here:

  numbers: integer, floating point, or scientfic notation (+/- required, e.g. 3.81469e-06)
  constants: e, Epsilon, QuantumRange, QuantumScale, Opaque, Pi, Transparent
  Fx operators (in order of precedence):
     ^ (power), *, /, % (modulo), +, -,
     <<, >>, <, <=, >, >=, ==, !=,
     & (bitwise AND),   | (bitwise OR),
     && (logical AND),  || (logical OR),
     ~ (logical NOT),  ?: (ternary conditional)
  math functions:
     abs(), acos(), alt(), asin(), atan(), atan2(), ceil(), cos(), cosh(), debug(), exp(), floor(),
     hypot(), int(), ln(), log(), logtwo(), max(), min(), mod(), pow(), rand(), round(),
     sign(), sin(), sinh(), sqrt(), tan(), tanh()
  channel functions: channel(r,g,b,a), channel(c,m,y,k,a)
  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
     lightness = pixel lightness
     luminance = pixel luminance

     page.width = page width
     page.height = page height
     page.x = page x offset
     page.y = page y offset

     resolution.x = x resolution
     resolution.y = y resolution

     depth = image depth
     minima = image minima
     maxima = image maxima
     mean = image mean
     standard_deviation = image standard deviation
     kurtosis = image kurtosis
     skewness = image skewness
       (add a channel specifier to compute a statistic for that channel, e.g. depth.r)
The Fx Expression

The Fx expressions includes any combination of the following:

The expression semantics include these rules:

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. Refer to a particular image in a sequence by appending its index to any image reference (usually u). A negative index counts from the end. For example, u[2] is the third image the sequence, u[-1] is the last image, and u[t] is s.

As an example, we reduce the intensity of the red channel by 50%:

  convert image.png -channel red -fx "u/2.0" image.jpg
Accessing Pixels

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).

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   blue pixel value at the 12th column and 34th row of the image

The other symbols specify the value you wish to retrieve.

Integer pixel references retrieve the color of the pixel referred to, whereas non-integer references return a blended color according to the current -interpolate setting.

A pixel outside the boundary of the image has a value dictated by the -virtual-pixel option setting.

Apply an Expression to Select Image Channels

Use the -channel 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, 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
Results

The -fx operator evaluates the given expression once for each pixel in the first image (u) in the sequence for each channel. The computed 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 initially set to the first image in the sequence (u), 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.