Fx is a 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:

## 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, Pi
      standard operators (in order of precedence):
         ^ (power), *, /, % (modulo), +, -, <, <=, =, >=, >, <<, >>, & (binary AND), | (binary OR), : (binary XOR)
      math function name:
         abs(), acos(), asin(), atan(), ceil(), cos(), exp(), floor(), int(), log(), ln(), mod(), pow(), rand(),
         sign(), sin(), sqrt(), tan()
      color names: red, cyan, black, etc.
      color hex values: #ccc, #cbfed0, #b9e1cc00
      symbols:
         s  = current image in sequence
         t  = position 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

##### Source Image

Specify the image source using an image index represented by `u`, starting at zero for the first image, (eg: `u[3]` is the fourth image in the image sequence). A negative image index counts images from the end of the current image sequence, for example, `u[-1]` refers to the last image in the sequence.

Without an index, `s` represents the current image in the sequence, `u` represents the first image in the sequence, and `v` represents the second image of the sequence. If no image is specified, the current image (`s`) is assumed.

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

##### Conditional

Fx conditionals return 0 (false) or 1 (true). To use them, multiply the effect you want, to the condition defining that effect, then add them together. For example:

      -fx '0.25*(u < 0.5)+0.75*(0.5 < u)'

##### Results

Note that all the original images in the current image sequence are replaced by the updated `alpha.png` image.

Discourse Server • Mailing Lists • ImageMagick Studio
© 1999-2006 ImageMagick Studio LLC