Skip to content

imagemagick

Comprehensive guide to image processing with ImageMagick’s magick CLI, including format conversion, resizing, transparency, optimization, compositing, metadata stripping, batch automation, and advanced manipulation techniques


Make background transparent

magick favicon.png \
  -fuzz 5% \
  -transparent "#1E2029" \
  -alpha on \
  -channel A -blur 0x1 -level 50%,100% +channel \
  favicon-transparent.png

Create favicon.ico the proper way

magick favicon.png \
 -background none \
 -define icon:auto-resize=16,32,48 \
 favicon.ico   

Show version + supported formats (quick sanity check)

magick -version
magick -list format | less

Identify image info (dimensions, colorspace, alpha, etc.)

magick identify -verbose image.png | less

Convert between formats

magick input.webp output.png
magick input.png  output.jpg

Make background transparent

magick favicon.png \
  -fuzz 5% \
  -transparent "#1E2029" \
  -alpha on \
  -channel A -blur 0x1 -level 50%,100% +channel \
  favicon-transparent.png

Create favicon.ico the proper way

magick favicon.png \
 -background none \
 -define icon:auto-resize=16,32,48 \
 favicon.ico

Basics

Resize (keep aspect ratio)

magick input.png -resize 800x output.png

Resize to fit inside box (no upscaling)

magick input.jpg -resize 1920x1080\> output.jpg

Exact size crop from center (square thumbnail)

magick input.jpg -resize 512x512^ -gravity center -extent 512x512 thumb.png

Rotate + auto-orient from EXIF

magick input.jpg -auto-orient -rotate 90 output.jpg

Strip metadata (EXIF/IPTC) for smaller files

magick input.jpg -strip output.jpg

Optimize PNG (strip + max compression)

magick input.png -strip -define png:compression-level=9 output.png

Quality control for JPEG

magick input.jpg -strip -quality 85 output.jpg

Convert to grayscale

magick input.png -colorspace Gray output.png

Add a solid border

magick input.png -bordercolor "#1E2029" -border 20 output.png

Advanced techniques

Smart trim whitespace + add padding

magick input.png -trim +repage -bordercolor none -border 24 output.png

Remove a background color to alpha (more robust than -transparent)

magick input.png \
  -alpha set \
  -fuzz 7% \
  -fill none -draw "alpha 0,0 floodfill" \
  output.png

Feather edges of an alpha mask (clean cutouts)

magick input.png \
  -alpha on \
  -channel A -blur 0x2 -level 40%,100% +channel \
  output.png

Overlay a watermark (bottom-right, with opacity)

magick base.jpg \
  \( watermark.png -alpha on -channel A -evaluate multiply 0.35 +channel \) \
  -gravity southeast -geometry +30+30 -composite \
  out.jpg

Annotate text (with stroke for readability)

magick input.jpg \
  -gravity south -pointsize 42 \
  -fill white -stroke black -strokewidth 2 \
  -annotate +0+30 "wuseman" \
  out.jpg

Create a drop shadow for a transparent PNG

magick input.png \
  \( +clone -background black -shadow 60x6+0+12 \) \
  +swap -background none -layers merge +repage \
  out.png

Convert to WebP with good defaults (quality + metadata strip)

magick input.png -strip -quality 82 output.webp

Limit colors (e.g., for icons/pixel art)

magick input.png -dither FloydSteinberg -colors 32 output.png

Create a contact sheet from many images

magick *.jpg \
  -thumbnail 320x320^ -gravity center -extent 320x320 \
  -montage -tile 6x -geometry +12+12 \
  contact-sheet.png

Stack images vertically (append)

magick top.png bottom.png -append out.png

Stack images horizontally (+append)

magick left.png right.png +append out.png

Make a side-by-side diff (useful for before/after)

magick compare -metric AE before.png after.png diff.png 2>&1

Create a mask-based cutout (apply external alpha mask)

magick subject.png mask.png \
  -alpha off -compose CopyOpacity -composite \
  cutout.png

Batch / automation

Batch convert PNG → WebP (keeps filenames)

for f in *.png; do
  magick "$f" -strip -quality 82 "${f%.png}.webp"
done

Batch resize a folder into ./out (no upscaling)

mkdir -p out
for f in *.jpg; do
  magick "$f" -auto-orient -strip -resize 1920x1080\> "out/$f"
done

Safely handle spaces/newlines in filenames (find -print0)

mkdir -p out
find . -maxdepth 1 -type f -name '*.png' -print0 |
  while IFS= read -r -d '' f; do
    magick "$f" -strip -resize 1024x1024\> "out/${f#./}"
  done

Quick tips

Prefer explicit colorspace before heavy ops (avoid surprises)

magick input.png -colorspace sRGB -strip output.png

Use parentheses to isolate operations (critical for overlays)

magick base.png \
  \( overlay.png -resize 25% \) \
  -gravity center -composite out.png