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 *.jpgImage 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 *.jpgApplying 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 *.jpgAdding 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'" *.jpgImage Rotation Mogrify can rotate images to a specified angle. For example, rotating all images in a directory by 90 degrees clockwise
mogrify -rotate 90 *.jpgImage 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 *.jpgThe -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 *.jpgImage 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% *.jpga 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-
-path output: The “-path” option specifies the output directory for the processed images. The directory must already exist (run
mkdir outputfirst).mogrifydoes not create it and fails with a “No such file or directory” error otherwise. -
-filter Triangle: The “-filter” option sets the filter used for resizing the image. The “Triangle” filter provides a balance between sharpness and smoothness during the resizing process.
-
-define filter:support=2: The “-define” option allows us to set specific parameters. In this case, it sets the support value for the chosen filter to 2, influencing the sharpness and clarity of the resulting image.
-
-unsharp 0.25×0.25+8+0.065: The “-unsharp” option applies an unsharp mask filter to enhance sharpness. The values “0.25×0.25” represent the radius and sigma of the filter, while “8” and “0.065” control the amount of sharpening and the threshold, respectively.
-
-dither None: The “-dither” option specifies the dithering method to be used during color reduction. Setting it to “None” ensures no dithering is applied, maintaining a clean and smooth appearance.
-
-posterize 136: The “-posterize” option reduces the number of color levels per channel to the specified value. In this case, each channel is quantized to 136 levels, which flattens gradients slightly.
-
-quality 100: The “-quality” option determines the compression level for the output image format. Setting it to 100 asks for the highest JPEG quality setting, which gives large files and is still lossy. For PNG output, the quality value controls compression, not fidelity.
-
-define jpeg:fancy-upsampling=off: The “-define” option allows us to set specific parameters for JPEG compression. By setting “fancy-upsampling” to “off,” we disable a technique that smooths edges during upsampling, preserving sharpness.
-
-define png:compression-filter=5 -define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude-chunk=all: The “-define” option also allows us to define parameters for PNG compression. These settings configure the compression filter, level, strategy, and exclude all optional chunks, resulting in optimized PNG files.
-
-interlace none: The “-interlace” option determines the type of interlacing for the output image. Setting it to “none” disables interlacing, so JPEGs are written as baseline (not progressive) files. Use
-interlace Planefor progressive JPEGs. -
-colorspace sRGB: The “-colorspace” option specifies the color space to be used for the output image. Setting it to “sRGB” ensures accurate and consistent color representation.
-
-strip: The “-strip” option removes any profile and comment data from the image file, resulting in a smaller file size.
-
-resize 1920×1080: The “-resize” option scales the image to the specified dimensions. In this case, the image will be resized to a resolution of 1920×1080 pixels.
-
.PNG: The “.PNG” represents the input images, using a wildcard pattern to process all PNG files in the current directory.
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.