Master ImageMagick's advanced geometric operations: distort with the affine, perspective, bilinear, and barrel methods, the shear and wave effects, and automatic correction of tilted images with deskew.

In episode 17 we covered advanced compositing & layers — how to combine many images with blend operators, alpha, and masks. Now we go up one more level: instead of arranging how two images overwrite each other, we'll arrange how one image is placed on a geometric plane it didn't occupy before.
Imagine you have a sheet of fabric with an image on it. The basic transforms we've already learned — resize, rotate, flip — are like sliding or turning that fabric as a whole, without changing its shape. Distort is different: it's like stretching, bending, and re-sewing the fabric to fit a new plane. In this episode 18 we'll cover -distort with the affine, perspective, bilinear, and barrel methods, the -shear and -wave effects, then close with correction: -deskew and manual perspective correction.
Basic geometric operations can only map a square to a square. In the real world, almost nothing is that simple:
All the cases above need the ability to map pixel coordinates to new coordinates with more flexible rules than mere translation, rotation, or scale. That's the job of -distort.
-distort CommandIts general form looks like this:
magick input.png -distort METHOD 'list of coordinates' output.pngMETHOD determines the type of geometric mapping, and the list of coordinates contains source → target coordinate pairs that define the mapping rule. Because -distort performs pixel re-mapping, two important things to remember:
-virtual-pixel background so empty areas are filled with the background color, or -virtual-pixel edge so they're filled with the edge pixel colors.-define distort:viewport=WxH+X+Y to set the result canvas size. Without it, ImageMagick computes the bounding box automatically.Affine is the simplest method: it accepts 3 coordinate pairs (6 numbers). An affine mapping keeps straight lines straight and parallel lines parallel — enough for a combination of translation, rotation, scale, and simple shear in one step:
magick logo.png -virtual-pixel background -distort affine \
'0,0 10,0 100,0 120,10 0,100 5,95' affine.pngThe three coordinate pairs in the example above mean: point (0,0) shifts to (10,0), point (100,0) shifts to (120,10), and point (0,100) shifts to (5,95). From those three points, ImageMagick computes the complete linear transform.
Tip
Rule of thumb: if you only need to rewrite all coordinates with a combination of translation, rotation, and scale — for example placing a watermark following an object's angle — affine is always faster and more precise than chained rotations.
Perspective accepts 4 coordinate pairs (8 numbers). This is the most used method for perspective correction and mockups. Unlike affine, perspective can make previously parallel lines non-parallel — exactly like the effect of a photo taken from a tilted angle.
A real example: placing a digital banner onto a billboard viewed from the side:
magick banner.png -virtual-pixel background -distort perspective \
'0,0 30,40 300,0 320,25 300,120 290,150 0,120 10,150' billboard.pngThe coordinates are read as pairs source_x,source_y target_x,target_y for the 4 corners: top-left, top-right, bottom-right, bottom-left. This order must stay consistent — wrong order produces a "folded" image.
Bilinear also accepts 4 coordinate pairs, but with a different characteristic: it keeps straight lines straight while mapping a square to any quadrilateral, including ones whose corners aren't 90 degrees. The result is flatter than perspective and suits panel or texture effects stretched over non-orthogonal surfaces without a depth illusion:
magick ui.png -virtual-pixel background -distort bilinear \
'0,0 20,50 200,0 220,20 200,120 210,140 0,120 10,130' panel.pngBarrel doesn't use coordinate pairs, but rather 4 coefficients a b c d that correct lens distortion. Coefficient a and b control the barrel curve (bulging outward), c controls pincushion (pinching inward), and d controls scale. A value of d=1.0 means the image size is preserved:
magick photo.jpg -distort barrel '0.12 0.0 -0.06 1.0' corrected.jpgA simple analogy: imagine writing text on a balloon. The text in the center looks normal, but the closer to the edge, the more curved it gets. Positive barrel a pulls the edges back inward, negative c pushes the center slightly away — the result is straight lines restored to normal.
-shear and -wave EffectsBeyond coordinate-based distort, ImageMagick provides two transform effects often used for visuals:
-shear tilts the image along the X and Y axes at a given angle. Because the result has transparent areas, combine it with -background before -flatten:magick text.png -background white -shear 20x0 sheared.png-wave applies a sinusoidal wave effect with the parameters amplitude x wavelength. Often used for text or banner effects:magick banner.png -wave 40x180 waved.png-deskew automatically detects the tilt of text/images and rotates it straight. The optional parameter is a threshold (percentage) that determines how aggressive the detection is:
magick scan.png -deskew 40% -background white -flatten deskewed.pngThe number 40% means pixels considered "background" (the dominant color) are treated neutrally; the smaller the percentage, the more aggressive the angle search. For scanned documents, values of 20–40% are usually safest.
When -deskew isn't enough — for example a page photographed from the side so it forms a trapezoid rather than a rectangle — manual perspective correction is the way out. The flow:
identify -verbose.-distort perspective.identify -verbose scan.jpg | rg 'Geometry'
magick scan.jpg -distort perspective \
'120,80 0,0 900,140 800,0 960,1080 800,1200 60,1020 0,1200' flat.pngThe result: a page that was a trapezoid becomes a full rectangle — just like scanning from above. This technique is also the basis of many "straighten document" tools you see in camera apps.
| Method | Coordinates | Properties Preserved | Use Case |
|---|---|---|---|
affine | 3 pairs (6 numbers) | Straight and parallel lines stay parallel | Translation, rotation, scale, watermark |
perspective | 4 pairs (8 numbers) | Straight lines stay straight | Perspective correction, billboard mockups |
bilinear | 4 pairs (8 numbers) | Straight lines stay straight, no depth illusion | Flat panel stretching |
barrel | 4 coefficients | Lens distortion pattern | Camera barrel/pincushion correction |
-deskew | Threshold % | Automatic rotation | Scan and OCR |
In this episode 18 you've passed the limit of "square to square" operations: you can stretch images according to affine and perspective rules, fix lens distortion with barrel, add shear and wave effects, and straighten tilted documents — automatically with -deskew or manually with perspective correction. These capabilities are the foundation for all "real-world geometry" tasks: from OCR pipelines to product mockups.
But the more complex the operation, the bigger the price paid in CPU and memory. In episode 19 we'll discuss Performance & Optimization: how to limit resource usage, control OpenMP parallelism, tune coders with -define, and produce small files with -strip and palette quantization.