Skip to content
Go back

Understanding the Mogrify Tool in Linux: A Guide to Image Manipulation

· Updated:
By KingPin 7 min read
Understanding the Mogrify Tool in Linux: A Guide to Image Manipulation
Contents

The mogrify tool is a versatile command-line utility in Linux that allows users to perform various image manipulation tasks with ease. It is part of the ImageMagick suite, a collection of software tools designed to create, edit, and convert images. In this article, we will dissect a specific mogrify command and explain each parameter to demonstrate its capabilities fully.

On ImageMagick 7 (current on Ubuntu 24.04, Debian 13 and Arch), mogrify is a symlink to the magick binary, and magick mogrify is the documented spelling. The flags below work the same either way. Older ImageMagick 6 installs only have the standalone mogrify.

Warning: mogrify overwrites the original files in place. Unless you use -format (which writes a new file next to the old one) or -path, test on a copy of your images first. For one-off edits, magick input.jpg -resize 800x output.jpg leaves the original alone.

examples of what you can do with mogrify :

Batch Image Conversion Mogrify can be used to convert multiple images from one format to another. For example, converting a directory of JPEG images to PNG format

mogrify -format png *.jpg

Image Resizing Mogrify is often used to resize images to specific dimensions. For example, resizing all images in a directory to a width of 800 pixels while maintaining the aspect ratio

mogrify -resize 800x *.jpg

Applying Image Effects Mogrify allows the application of various effects to images. For example, applying a grayscale effect to all images in a directory.

mogrify -type grayscale *.jpg

Adding Watermarks Mogrify can add watermarks or annotations to images. For example, adding a watermark to all images in a directory.

mogrify -gravity southeast -draw "text 10,10 'Watermark'" *.jpg

Image Rotation Mogrify can rotate images to a specified angle. For example, rotating all images in a directory by 90 degrees clockwise

mogrify -rotate 90 *.jpg

Image Cropping Mogrify can crop images to a specific size or aspect ratio. For example, cropping all images in a directory to a square shape

mogrify -gravity center -crop 1:1 +repage *.jpg

The -gravity center keeps the crop centered (otherwise it grabs from the top-left), and +repage resets the virtual canvas so you don’t end up with weird offsets baked into the output.

Image Compression Mogrify can be used to compress images, reducing file size without significant loss of quality. For example, compressing all images in a directory with a quality level of 80

mogrify -quality 80 *.jpg

Image Format Optimization Mogrify can optimize images for specific formats, applying format-specific optimizations. For example, optimizing all images in a directory for web display

mogrify -strip -interlace Plane -gaussian-blur 0.05 -quality 85% *.jpg

a more comprehensive example of how to upscale and sharpen multiple images at once

mogrify -path output -filter Triangle -define filter:support=2 -unsharp 0.25x0.25+8+0.065 -dither None -posterize 136 -quality 100 -define jpeg:fancy-upsampling=off -define png:compression-filter=5 -define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude-chunk=all -interlace none -colorspace sRGB -strip -resize 1920x1080 *.PNG

The mogrify tool in Linux, with its extensive range of options, provides a powerful and flexible solution for image manipulation tasks. By understanding the example command and its various parameters, you now have a working understanding of how to use mogrify to resize, filter, compress, and optimize images efficiently. Experiment with different settings to achieve desired results and explore the vast possibilities offered by this remarkable tool.

Common Questions

Does mogrify overwrite the original image?

Yes, mogrify overwrites the original files in place. The -format option writes a new file next to the old one, and -path sends output to a separate directory. Test on a copy first, or use magick input.jpg -resize 800x output.jpg for a one-off edit that keeps the original.

Is mogrify the same as magick in ImageMagick 7?

Yes, on ImageMagick 7 the mogrify command is a symlink to the magick binary, and magick mogrify is the documented spelling. The flags work the same either way. ImageMagick 6 installs only have the standalone mogrify, so use that name there.

Why does mogrify -path fail with “No such file or directory”?

The output directory does not exist yet. The -path option names the output directory, but mogrify does not create it. Run mkdir output first, then rerun the command. Without the directory, mogrify fails with a “No such file or directory” error.

How do I crop an image to a square with mogrify?

Run mogrify -gravity center -crop 1:1 +repage *.jpg. The -gravity center option keeps the crop centered, because the crop otherwise grabs from the top-left. The +repage option resets the virtual canvas, so the output carries no stray offsets. Remember that mogrify overwrites the originals.


Share this post on:

Send a Webmention

Written about this post on your own site? Send a webmention and it'll show up above once verified.


Previous Post
Creating Image Slideshows with Voiceovers using FFMPEG
Next Post
Machine Learning models (AI)

Discussion

Powered by Garrul . Sign in with GitHub or Google, or post anonymously.

Related Posts