Linux: ImageMagick Command Line Tutorial

, , …,

This page shows you how to do common image editing tasks using the command line software ImageMagick or GraphicsMagick.

The following shows ImageMagick commands. If you are using GraphicsMagick, just add gm in front of the command.

To install: sudo apt-get install imagemagick

jpg png Conversion

Convert from gif to png.

convert p1.gif p2.png

Convert from png to jpg. Use “-quality” for compression/quality.

convert -scale 50% -quality 80% old.png new.jpg

You can also convert to the tens of image formats such as gif, tiff. Remember, the destination format should support all the features of the format you are converting from, otherwise you may lose info. For example, converting from gif to png is ok, but png to gif may lose colors because gif only support a max of 256 colors.

Also, it is ok to convert png to jpg, but remember that jpg is a lossy format.

Scale, Crop

Scale a image.

convert -scale 50% old.gif new.png

Autocrop border

convert -trim cat.png cat.png

Cropping (cutting) a image.

convert -crop 853x368+0+56 old.png new.png

The 853 and 368 would be the new image's width and height. The 0 is the offset on x-axis, and 56 is the offset of y-axis. The x and y axes's origin starts at the upper left corner.

To crop by specifying percentage of sides to cut, use “-shave”.

Color, Brightness, Saturation…

Increase brightness.

convert -modulate 150,100,100 old.png new.png

The above increase brightness by the multiplier 150%. To decrease, use values less than 100.

The 3 numbers means: brightness, saturation, hue. They are all interpreted as percentages.

Increase saturation.

convert -modulate 100,130,100 old.png new.png

The above increase color saturation by the multiplier 130%. To decrease, use values less than 100.

Color, bits per pixel, file size.

Change color image to gray scale.

convert -type Grayscale old.png new.png

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

Reduce bits per pixel.

Use -depth. In particular, for a grayscale line art in png, you can do: convert -depth 8 old.png new.png. That makes it 8 bits. For clean black and white line art, you can use “-depth 2”. This is great for reducing file size. (see also “-colors”)

Reduce color.

convert -dither -colors 256 old.png new.png

(dither adds dots)

To reduce color without dithering, use +dither instead of -dither. Note that reducing colors does not necessarily reduce file size.

To reduce png file's size when reducing color, use -depth for values of {2, 3, 4, 8}.

steve jobs original steve jobs original 4bits no dither steve jobs original 4bits dither
Original; 4bits no dither; 4bits dithered

Image Filtering

Sharpen a image.

convert -sharpen 2 old.png new.png

Blur a image.

convert -blur 1 old.png new.png

Image Editing

Insert copyright notice.

convert -fill red -draw 'text 20 20 "© 2006"' old.png new.png

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

How to add a border?

convert -border 1 -bordercolor black cat.png cat2.png

Flip & Rotate

How to rotate a image?

convert -rotate 90 x.png x.png

Positive degree means counter-clockwise.

How to flip a image?

# left/right flip
convert -flop x.png x.png

# up/down flip
convert -flip x.png x.png

Combine Images

How to combine 2 images?

montage -mode concatenate -tile 1x cat1.jpg cat2.jpg out.jpg

Batch Processing

Batch processing.

There are many ways to process all files in a directory in one shot. You can use the unix shell utils “find” and “xargs”.

Suppose you want to convert all files in a dir from png to jpg. Here's GNU/Linux example:.

find . -name "*png" | xargs -l -i basename "{}" ".png" | xargs -l -i  convert -quality 85% "{}.png" "{}.jpg"

The “-l” makes it process one line at a time. The “-i” makes the “{}” to stand for file name. The “basename” strips the suffix.


To get meta info, such as file's comment, date, camera info, use exiftool. See: How to View Comments in JPEG, PNG, MP3 files?.

Other options to explore:

blog comments powered by Disqus