mogrify Command in Linux



mogrify command is a powerful tool in Linux for batch processing of images. It allows users to convert, resize, crop, and perform various other transformations on images directly within the command line interface. Unlike other image manipulation commands, mogrify overwrites the original files, so it's essential to use with caution.

Table of Contents

Here is a comprehensive guide to the options available with the mogrify command −

Syntax of mogrify Command

To run the mogrify command in Linux, follow this basic syntax −

mogrify [options] image_file

Where −

  • [options] are optional parameters that modify the command's behavior.
  • image_file refers to the image file or files you wish to process.

mogrify Command Options

Below are some various options you can apply with the Linux mogrify command −

Options Description
-resize Resizes the image to the specified dimensions.
-crop Crops the image to the specified geometry.
-rotate Rotates the image by the specified angle.
-format Changes the image format to the specified type.
-quality Sets the compression level of the output image.
-strip Creates a thumbnail of the specified size.
-brightness-contrast Adjusts the brightness and contrast of the image.
-sharpen Sharpens the image with the specified radius and sigma.
-blur Blurs the image with the specified radius and sigma.
-modulate Modulates the brightness, saturation, and hue of the image.
-annotate Adds text annotation to the image.
-fill Sets the color to be used for annotations and drawing.
-stroke Sets the color to be used for stroke operations.
-gravity Sets the gravity to be used when placing annotations or cropping.

Examples of mogrify Command in Linux

Let's examine a few ways to utilize the command mogrify in a Linux environment −

  • Resizing Images
  • Converting Image Format
  • Cropping Images
  • Rotating Images
  • Adjusting Quality
  • Creating Thumbnails
  • Stripping Metadata
  • Annotating Images

Resizing Images

When you need to resize multiple images to a specific dimension, you can use the mogrify command with the -resize option −

mogrify -resize 800x600 pic.jpg

This command resizes pic.jpg image in the current directory to 800x600 pixels. It's particularly useful for preparing images for web use or creating uniform image sizes for a project.

mogrify Command in Linux1

Converting Image Format

To convert images from one format to another, such as from .png to .jpg, use the -format option −

mogrify -format jpg pic.png

This command converts pic.png image in the current directory to .jpg format, overwriting the original files. This is helpful when you need to change image formats for compatibility or other requirements.

mogrify Command in Linux2

Cropping Images

If you need to crop images to a specific geometry, use the -crop option −

mogrify -crop 200x200+10+10 pic.jpg

This command crops all .jpg images in the current directory to a 200x200 pixel area, starting 10 pixels from the top and 10 pixels from the left. It's useful for focusing on specific parts of images.

mogrify Command in Linux3

Rotating Images

To rotate images by a specific angle, use the -rotate option −

mogrify -rotate 90 *.jpg

This command rotates all .jpg images in the current directory by 90 degrees. It's beneficial for correcting the orientation of images or creating artistic effects.

Adjusting Quality

To set the compression level for the output images, use the -quality option −

mogrify -quality 85 *.jpg

This command compresses all .jpg images in the current directory to 85% quality. It's useful for reducing file sizes while maintaining acceptable image quality.

Creating Thumbnails

If you need to create thumbnails of your images, use the -thumbnail option −

mogrify -thumbnail 100x100 *.jpg

This command creates 100x100 pixel thumbnails for all .jpg images in the current directory. Thumbnails are helpful for creating previews or managing large image galleries.

Stripping Metadata

To remove all profiles and comments from images, use the -strip option −

mogrify -strip *.jpg

This command removes all metadata from all .jpg images in the current directory. It's useful for protecting privacy and reducing file sizes.

Annotating Images

To add text annotations to images, use the -annotate option −

mogrify -annotate +10+10 'Sample Text' *.jpg

This command adds the text "Sample Text" to all .jpg images in the current directory, 10 pixels from the top and left. Annotations are useful for labeling images or adding watermarks.

Conclusion

The mogrify command is a versatile tool for batch processing images in Linux. By mastering its various options and understanding how to apply them, you can efficiently manage and transform large numbers of images.

Whether you're resizing a collection of photos, converting formats, or adding annotations, mogrify streamlines your workflow and ensures precise results.

Advertisements