ImageMagick Tutorial

By Xah Lee. Date: . Last updated: .

What is ImageMagick

ImageMagick is a command line tool to process images. it can convert, resize, crop, rotate, flit, change color, etc.

show version

magick --version

this page covers version 7 command line syntax.

Command Line Difference Between Version 6 and 7

Convert png to jpg, or others

jpg

magick xx.png xx.jpg
magick xx.png -quality 90% xx.jpg

when -quality is omitted, default is 92.

webp

magick xx.png xx.webp

Summary of popular formats

formatlossy or notnumber colorsanimation
PNGlosslessallno
JPEGlossyallno
WebPlossy or not lossyallyes
GIFlossless256yes

Lossy means each time you save to the format, the quality degrades.

If the destination format do not support some features, such as colors, it'll be lost. E.g. Converting from png to gif.

Scale

magick xx.jpg -scale 50% yy.png

autocrop (Delete Border of Same Color)

magick xx.png -trim yy.png

crop

magick xx.png -crop 90x50+0+20 yy.png

Color, Brightness, Saturation

brightness

magick xx.png -modulate 150,100,100 yy.png

saturation

magick xx.png -modulate 100,50,100 yy.png

remove transparency with white

magick xx.png -flatten yy.png

to gray scale

magick xx.png -type Grayscale yy.png

Note: this does not force the png image format to use indexed color for smaller file size.

reduce number of color

make it just 2 colors.

magick xx.png -colors 2 yy.png

Transparency, Color, Bits Per Pixel

reduce bits per pixel channel

to make each red green blue to be just 2 values.

magick xx.png -depth 2 yy.png

Image Filtering

sharpen

magick xx.png -sharpen 1 yy.png

blur

magick xx.png -blur 1 yy.png

Image Editing

insert copyright notice

magick xx.png -fill red -draw 'text 20 20 "© 2024 Xah Lee"' yy.png

Use -gravity SouthEast -font arial to put the text in other corners, and change font.

add a border

magick xx.png -bordercolor red -border 5 yy.png

Flip, Rotate

rotate

magick xx.png -rotate 90 yy.png

clockwise

flip

flip around y axis

magick xx.png -flop yy.png

flip around x axis

magick xx.png -flip yy.png

Combine Images

combine 2 images

join images vertically

magick xx.png xx2.png -append yy.png

join images horizontally

magick xx.png xx2.png +append yy.png

do all in a dir

# convert all png in a dir to jpg
for ff in *.png; do magick $ff $ff.jpg; done

or

# convert all png in a dir to jpg
# file name should not contain ttt. if so, change ttt to hhh or something random
find . -name "*png" -print0 | xargs -0 -L1 -I ttt magick ttt ttt.jpg

Reference