Operations 19 min read

ImageMagick Command‑Line Guide: Installation, Basic Commands, Formats and Practical Examples

This tutorial explains how to install ImageMagick, use its core command‑line tools such as magick, convert, identify, mogrify and composite, format image resize commands, add watermarks, generate captchas, clone and append images, convert between GIF, PDF and other formats, and integrate the tool with Node.js scripts.

JD Tech
JD Tech
JD Tech
ImageMagick Command‑Line Guide: Installation, Basic Commands, Formats and Practical Examples

ImageMagick is a powerful command‑line suite for creating, editing, converting and compositing images, supporting over 200 formats including PNG, JPEG, GIF, PDF and SVG.

Installation – on macOS you can install it with brew install imagemagick . The official download page also provides binaries for Linux, Windows and other platforms.

Basic commands – after installation the following sub‑commands are available:

magick : general image creation, editing and conversion.

convert : alias of magick for backward compatibility.

identify : prints image format, resolution, size, colorspace, etc.

mogrify : like magick but overwrites the original file.

composite : combines one or more images into a new image.

montage : creates a composite image from multiple independent images.

compare : compares two images mathematically and visually.

display , animate , import , conjure , stream and others for various advanced tasks.

Command format follows the Unix style: command [options] input_image output_image . For multiple inputs you can use command [options] image1 [options] image2 output_image .

Resize example – to create a 150×100 thumbnail from goods.jpg :

convert -resize 150x100 -quality 70 -strip goods.jpg thumbnail.jpg

The -resize flag also accepts modifiers:

! – force exact dimensions.

> – resize only if the image is larger than the given size.

< – resize only if the image is smaller.

Watermark example – add a centered transparent text watermark:

convert -draw 'text 0,0 "JD.COM"' -fill 'rgba(221,34,17,0.25)' -pointsize 36 -font 'cochin.ttc' -gravity center joy.jpg watermark.jpg

For tiled, rotated watermarks you can first create a transparent canvas with -size and xc:none , draw the text, then pipe it to composite using -tile and -dissolve .

Captcha generation – a fixed‑value example that draws four characters with different colors, rotations and bezier noise:

convert 'xc:[100x40!]' -pointsize 20 -font 'cochin.ttc' \
  -gravity NorthWest -strokewidth 1 -fill '#b72b36' -stroke '#b72b36' \
  -draw 'translate 13,19 rotate 10 text -5,-8 "5"' \
  -fill '#821d70' -stroke '#821d70' -draw 'translate 36,13 rotate -8 text -8,-8 "C"' \
  -fill '#c7960a' -stroke '#c7960a' -draw 'translate 60,23 rotate 5 text -5,-8 "2"' \
  -fill '#03610a' -stroke '#03610a' -draw 'translate 85,25 rotate 13 text -8,-8 "E"' \
  -strokewidth 2 -stroke 'rgba(248,100,30,0.5)' -fill 'rgba(0,0,0,0)' \
  -draw 'bezier -20,30 -16,10 20,2 50,20' -draw 'bezier 50,20 78,42 138,36 140,16' +noise Impulse captcha.jpg

Clone and append – using image stacks to crop, resize, flip and combine images:

convert \ 
  ( -crop 300x300+10+25 joy.jpg ) \ 
  ( -resize 400x400 -crop 300x300+50+0 logo: ) -swap 0,1 +append \ 
  ( -clone 0 -flop -flip ) -append -resize 200x200 combined.jpg

GIF ↔ image conversion – extract frames:

convert -coalesce rain.gif frame_%d.jpg

Or extract a specific frame:

convert -coalesce 'rain.gif[0]' first_frame.jpg

To re‑assemble frames into an animated GIF with infinite looping:

convert -loop 0 frame-*.jpg rain_animation.gif

Use leading zeros ( frame-001.jpg ) to keep correct order.

PDF ↔ image conversion – requires Ghostscript ( brew install ghostscript ). Two approaches:

convert -density 150 -flatten 'download.pdf[0]' first_page.jpg

or

convert -density 150 -background white -alpha remove download.pdf download.jpg

The -density option sets DPI for clearer output.

Node.js integration – execute ImageMagick commands via child_process :

const util = require('util');
const exec = util.promisify(require('child_process').exec);
await exec(`convert -resize '150x100!' -strip goods.jpg thumbnail.jpg`);

For streaming data use spawn :

const cp = require('child_process');
const fs = require('fs');
const args = ['-', '-resize', '150x100!', '-strip', 'jpg:-'];
const proc = cp.spawn('convert', args);
fs.createReadStream('/path/to/goods.jpg').pipe(proc.stdin);
proc.stdout.pipe(response);

The article ends with a call‑to‑action for the JD Tech public account.

Graphicsimage-processingNode.jscommand-lineImageMagickshell scripting
JD Tech
Written by

JD Tech

Official JD technology sharing platform. All the cutting‑edge JD tech, innovative insights, and open‑source solutions you’re looking for, all in one place.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.