Commmonly used imagemagick commands.
ImageMagick is an open source tool for editing and converting raster images created in 1987 by John Cristy.
I use ImageMagick on the command line for quick image edits such as cropping, resizing or rotating. It is also used in the batchVariants
script that prepares the images for this website.
Use convert
to create a new output file or mogrify
to overwrite the input file.
Rotating
mogrify -rotate "90" file.jpg
Rotates an image by 90 degrees clockwise.
Cropping
convert -crop 640x480+50+100 +repage input.jpg output.jpg
Crops the image at X:50 Y:100
to a size of W: 640 H:480
.+repage
will prevent export of the cropped area.
convert -crop 3:2+50+100 +repage input.jpg output.jpg
Crops the image at X:50 Y:100
to a ratio of W: 3 H:2
convert -crop 3:2+50+100 +repage input.jpg output.jpg
Crops the image at X:50 Y:100
to a ratio of W: 3 H:2
Resizing
mogrify -geometry 50% file.jpg
Resizes the image down by half.
mogrify -geometry 1024x file.jpg
Resizes the image to a width of 1024px.
mogrify -geometry x800 file.jpg
Resizes the image to a height of 800px.
mogrify -geometry 800x800 file.jpg
Resizes the image to 800px at the longer side.
mogrify -density 72 -units pixelsperinch file.jpg
Changes the DPI of the image to 72.
Compositing
convert input.png -colorspace RGB -background "rgb(255,255,255)" -flatten output.png
Adds a background color to a transparent png
convert input.png -gravity center -background white -extent 200x200 output.jpg
Extens the canvas of an image to specified size.
convert input.png -channel RGB -negate output.png
Inverts the colors of an image.
Dithering
convert input.jpg -dither FloydSteinberg -colors 8 output.jpg
Dithers the image with 8 colors.
Tiling
montage * -tile 2x2 output.jpg
Tiles all images in folder in 2 columns and 2 rows. Either number can be omited.
montage * -background "#000000" null: -mode Concatenate output.jpg
Sets the background color the black trying to minimize the space inbetween the images..
PDF conversion
convert "*.jpg" -quality 100 output.pdf
Converts all jpg files into a single pdf
convert file.pdf -flatten output.jpg
Converts a pdf to.jpg
mogrify -format jpg *.pdf
Converts all pdf files at location from pdf to jpg.
convert -density 300 input.pdf -brightness-contrast -20x20 -sharpen 0x1 output.pdf
Adjusts brightness, contrast and sharpness of a scanned pdf document.
mogrify -format png *.jpg
Converts all jpg files to png keeping the original basename.
Image Sequencing
convert -delay 200 -loop 0 *.jpg output.gif
Creates an infinitely looping GIF with 2 seconds delay between each frame.
Information
identify file.jpg
Displays information on the file such as width and height.
Offline Documentation
To make the documentation available offline, install the imagemagick-6-doc
package.
Colors
convert in.jpg -colorspace gray out.jpg
Converts an image to grayscale.
Cache Policy
In case a process exhausts the cache resources, find /etc/ImageMagick-6/policy.xml
and increase the value
property in this line:
<policy domain="resource" name="disk" value="1GiB"/>
Security Policy blocking conversion
In case conversion is blocked because of a security policy workaround for an older version of Ghostscript, comment out the whole block in /etc/ImageMagick-6/policy.xml
<policy domain="coder" rights="none" pattern="PS" /> <policy domain="coder" rights="none" pattern="PS2" /> <policy domain="coder" rights="none" pattern="PS3" /> <policy domain="coder" rights="none" pattern="EPS" /> <policy domain="coder" rights="none" pattern="PDF" /> <policy domain="coder" rights="none" pattern="XPS" />