The FX Special Effects Image Operator • The Anatomy of an FX Expression
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 canvas:black -channel blue -fx "1/2" fx_navy.png
Here, we convert a black to a navy blue image:
Or the expression can be complex:
convert rose: \
-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 source image:
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 canvas: \
-fx "Xi=i-w/2; Yj=j-h/2; 1.2*(0.5-hypot(Xi,Yj)/70.0)+0.5" \
radial-gradient.png
The command above returns this image:
This FX expression adds random noise to an image:
convert photo.jpg -fx 'iso=32; rone=rand(); rtwo=rand(); \
myn=sqrt(-2*ln(rone))*cos(2*Pi*rtwo); myntwo=sqrt(-2*ln(rtwo))* \
cos(2*Pi*rone); pnoise=sqrt(p)*myn*sqrt(iso)* \
channel(4.28,3.86,6.68,0)/255; max(0,p+pnoise)' noisy.png
This FX script utilizes a loop to create a Julia set:
convert -size 400x400 xc:black -colorspace gray -fx " \
Xi=2.4*i/w-1.2; \
Yj=2.4*j/h-1.2; \
for (pixel=0.0, (hypot(Xi,Yj) < 2.0) && (pixel < 1.0), \
delta=Xi^2-Yj^2; \
Yj=2.0*Xi*Yj+0.2; \
Xi=delta+0.4; \
pixel+=0.00390625 \
); \
pixel == 1.0 ? 0.0 : pixel" \
\( -size 1x1 xc:white xc:red xc:orange xc:yellow xc:green1 xc:cyan xc:blue \
xc:blueviolet xc:white -reverse +append -filter Cubic -resize 1024x1! \) \
-clut -rotate -90 julia-set.png
This FX script prints the first 10 prime numbers:
convert xc: -channel gray -fx " \
for (prime=2, prime < 30, composite=0; \
for (nn=2, nn < (prime/2+1), if ((prime % nn) == 0, composite++, ); nn++); \
if (composite <= 0, debug(prime), ); prime++)" null:
See Using FX, The Special Effects Image Operator for more examples.
The next section discusses the FX expression language.
The formal FX expression language is defined here:
u
: first image in listv
: second image in lists
: current image in list (for %[fx:] otherwise = u)t
: index of current image (s) in listn
: number of images in listi
: column offsetj
: row offsetp
: pixel to use (absolute or relative to current pixel)w
: width of this imageh
: height of this imagez
: channel depthr
: red value (from RGBA), of a specific or current pixelg
: greenb
: bluea
: alphao
: opacityc
: cyan value of CMYK color of pixely
: yellowm
: magentak
: blackintensity
: pixel intensityhue
: pixel huesaturation
: pixel saturationlightness
: pixel lightnessluma
: pixel lumapage.width
: page widthpage.height
: page heightpage.x
: page x offsetpage.y
: page y offsetprintsize.x
: x printsizeprintsize.y
: y printsizeresolution.x
: x resolutionresolution.y
: y resolutiondepth
: image depthextent
: image extentminima
: image minimamaxima
: image maximamean
: image meanstandard_deviation
: image standard deviationkurtosis
: image kurtosisskewness
: image skewness (add a channel specifier to compute a statistic for that channel, e.g. depth.r)An FX expression may include any combination of the following:
^
y(
... )
*
y*
is optional, for example, 2u
or 2(x+y)
are acceptable)/
y%
y+
y-
y<<
y>>
y<
y<=
y>
y>=
y==
y!=
y&
y|
y&&
y||
y~
x+
x-
x?
y :
z =
y;
yphi
pi
e
QuantumRange
QuantumScale
QuantumRange
intensity
hue
saturation
lightness
luminance
0.212656*red + 0.715158*green + 0.072186*blue
red, green, blue
, etc.#ccc, #cbfed0, #b9e1cc00
, etc.rgb(), rgba(), cmyk(), cmyka(), hsl(), hsla()
s, t, u, v, n, i, j, w, h, z, r, g, b, a, o, c, y, m, k
abs(
x)
acos(
x)
acosh(
x)
airy(
x)
alt(
x)
asin(
x)
asinh(
x)
atan(
x)
atanh(
x)
atan2(
y,x)
ceil(
x)
channel(
r,g,b,a)
channel(
c,m,y,k,a)
clamp(
x)
cos(
x)
cosh(
x)
debug(
x)
while(
expression, condition test)
drc(
x,y)
erf(
x)
exp(
x)
floor(
x)
for(
initialization, condition test, expression)
gauss(
x)
gcd(
x,y)
hypot(
x,y)
if(
condition test, nonzero-expression, zero-expression)
int(
x)
isnan(
x)
j0(
x)
j1(
x)
jinc(
x)
ln(
x)
log(
x)
logtwo(
x)
ln(
x)
max(
x, y)
min(
x, y)
mod(
x, y)
not(
x)
pow(
x,y)
rand()
round()
sign(
x)
sin(
x)
sinc(
x)
squish(
x)
sinh(
x)
sqrt(
x)
tan(
x)
tanh(
x)
trunc(
x)
while(
condition test, expression)
image.depth, image.kurtosis, image.maxima, image.minima, image.resolution.x, image.resolution.y, image.skewness, image.standard_deviation
The expression semantics include these rules:
r=3.0; r
returns the pixel red color value, not 3.0The symbols u
and v
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 u
), with a zero index for the beginning of the sequence. A negative index counts from the end. For example, u[0]
is the first image in the sequence, u[2]
is the third, u[-1]
is the last image, and u[t]
is the current image. The current image can also be referenced by s
. If the sequence number exceeds the length of the sequence, the count is wrapped. Thus in a 3-image sequence, u[-1]
, u[2]
, and u[5]
all refer to the same (third) image.
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):
convert image1.jpg image2.jpg image3.jpg -fx "(u+u[2])/2.0" image.jpg
By default, the image to which p
, r
, g
, b
, a
, etc., are applied is the current image s
in the image list. This is equivalent to u
except when used in an escape sequence %[fx:...]
.
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 -channel red instructs -fx 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.
convert -channel red logo: -flop logo: -resize "20%" -fx "(u+v)/2" image.jpg
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 an image can be specified using a pixel index represented by p
. For example,
p[-1].g green value of pixel to the immediate left of the current pixel
p[-1,-1].r red value of the pixel diagonally left and up from current pixel
To specify an absolute position, use braces, rather than brackets.
p{0,0}.r red value of the pixel in the upper left corner of the image
p{12,34}.b blue pixel value at column number 12, row 34 of the image
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 -interpolate setting.
A position outside the boundary of the image retrieves a value dictated by the -virtual-pixel option setting.
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, 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
The -fx operator evaluates the given expression for each channel (set by -channel) of each pixel in the first image (u
) 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 alpha.png
replaces both of the original images, alpha.png
and beta.png
, before being saved as gamma.png
.
The current image s
is set to the first image in the sequence (u
), and t
to its index, 0. The symbols i
and j
reference the current pixel being processed.
For use with -format, the value-escape %[fx:]
is evaluated just once for each image in the current image sequence. As each image in the sequence is being evaluated, s
and t
successively refer to the current image and its index, while i
and j
are set to zero, and the current channel set to red (-channel is ignored). An example:
convert canvas:'rgb(25%,50%,75%)' rose: -colorspace rgb \
-format 'Red channel of NW corner of image #%[fx:t] is %[fx:s]\n' info:
Red channel of NW corner of image #0 is 0.464883
Red channel of NW corner of image #1 is 0.184582
Here we use the image indexes to rotate each image differently, and use -set
with the image index to set a different pause delay on the first image in the animation:
convert rose: -duplicate 29 -virtual-pixel Gray -distort SRT '%[fx:360.0*t/n]' \
-set delay '%[fx:t == 0 ? 240 : 10]' -loop 0 rose.gif"
The color-escape %[pixel:]
or %[hex:]
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 (a named color or hex color value). The symbols i
and j
are set to zero, and s
and t
refer to each successively current image and index.