Post

Imagemagick Cheatsheet

Overview of Imagemagick

ImageMagick is a free and open-source software suite for displaying, converting, and editing raster image and vector image files. It can read and write images in a variety of formats (over 200) including PNG, JPEG, JPEG-2000, GIF, TIFF, DPX, EXR, WebP, Postscript, PDF, and SVG. ImageMagick can resize, flip, mirror, rotate, distort, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves. It is available for Windows, macOS, Linux, and other operating systems.

Prerequisites

imagemagick installed on your computer

Change File type

You can use ImageMagick’s convert command-line tool to change the file type of an image. Here’s the basic syntax:

1
convert input_file.jpg output_file.png

Compress Images

To compress images using ImageMagick, you can use the convert command along with the -quality option to specify the compression level. Here’s a basic example:

1
convert input.jpg -quality 80 output.jpg

Cropping images

Cropping from top left

Cropping images in ImageMagick is straightforward and can be done using the convert command-line tool. Here’s a basic command to crop an image:

1
convert input.jpg -crop WIDTHxHEIGHT+XOFFSET+YOFFSET output.jpg

Replace input.jpg with the name of your input image file and output.jpg with the desired name for the cropped image.

  • WIDTH and HEIGHT are the dimensions of the cropped region.
  • XOFFSET and YOFFSET specify the starting point for the crop, measured from the top-left corner of the original image.

For example, to crop a 100x100 pixel region starting at coordinates (50, 50) from the top-left corner of an image named input.jpg, you would use:

1
convert input.jpg -crop 100x100+50+50 output.jpg

Cropping from bottom left

To crop an image from the bottom-left corner using ImageMagick, you can use the convert command with the -crop option. Here’s how you can do it:

1
convert input.jpg -crop WxH+0+0 output.jpg

Replace input.jpg with the filename of your input image and output.jpg with the desired output filename. W and H represent the width and height of the cropped area, respectively. Set W to the desired width of the cropped area and H to the desired height of the cropped area. For example, if you want to crop a 100x100 pixel area from the bottom-left corner, you would set W to 100 and H to 100.

The +0+0 part of the command specifies the starting point of the crop, where +0 indicates the distance from the left edge of the image (0 pixels) and +0 indicates the distance from the top edge of the image (0 pixels), which means it starts from the bottom-left corner.

Here’s an example:

1
convert input.jpg -crop 100x100+0+0 output.jpg

This command will crop a 100x100 pixel area from the bottom-left corner of the input.jpg image and save the result as output.jpg. Adjust the width, height, and output filename as needed for your specific use case.

Flip Images

Flip image vertically

1
convert input.png -flip output.png

Flip image horizontally

1
convert input.png -flop output.png

Resize images

The below command will resize an image named input.jpg and resize it to 300px wide. Then output an image called output.jpg

1
convert input.jpg -resize 300 output.jpg

Resize to specific height and width

1
convert input_image.jpg -resize WIDTHxHEIGHT output_image.jpg

Resize multiple images with the 1 command

1
convert input_image1.jpg input_image2.jpg input_image3.jpg -resize WIDTHxHEIGHT output_directory/output1.jpg output_directory/output2.jpg output_directory/output3.jpg

Resize images directly

without creating a copy or duplicating the image. Resize the images directly inplace using the mogrify command

1
2
mogrify -resize 800x600 *.jpg

Using a wildcard

1
convert input_images/*.jpg -resize WIDTHxHEIGHT output_directory/%d_output.jpg

Invert Images

1
convert input.png -negate output.png
This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.