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

much better is google's cwebp.

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 or .PNG in a dir to jpg
# file name should not contain tttt. if so, change tttt to hhh or something random
find . -iname "*png" -maxdepth 1 -print0 | xargs -0 -L1 -I tttt magick tttt tttt.jpg

ImageMagick

image processing