The modern alternative to ImageMagick is easy to use if you learn the basic commands.

Table of Contents

Changing format

  • Convert to one format to another
    gm convert image.png image.jpg
    
  • Batch converting (from png to jpg)
    gm mogrify -format jpg *.png
    
    • -output-directory <directory>: specify an output directory, it must exist. If you type a relative path, it will be relative to input files directory. If the parameter is omitted, it will save files inside input files directory.

Resizing, cropping and compressing

  • Resize largest side (and keeping ratio)
    # Resize largest side to 1000px
    gm convert image.jpg -resize 1000 output.jpg
    
  • Resize shortest side (and keeping ratio)
    # Resize shortest side to 120px
    gm convert image.jpg -resize 120^ output.jpg
    
  • Change resolution only if image is larger
    # Resize largest side to 500px
    gm convert image.jpg -resize '500>' output.jpg
    
    # Resize shortest side to 500px
    gm convert image.jpg -resize '500^>' output.jpg
    
  • Change resolution without keeping ratio
    # Create an 120x120 image
    gm convert image.jpg -resize 120x120! output.jpg
    
  • Change width (keeping ratio)
    gm convert image.jpg -resize 120x output.jpg
    
  • Change height (and keeping ratio)
    gm convert image.jpg -resize x120 output.jpg
    
  • Resize and crop (create a thumbnail)
    # Resize shortest side to 500px and crop (from the center) a 500x500 image
    gm convert image.jpg -resize 500^ -gravity center -crop 500x500+0+0 thumbnail.jpg
    
  • Change quality (compression)
    gm convert image.jpg -quality 50 output.jpg
    

You can do these operations in batch using gm mogrify instead of gm convert:

gm mogrify -format jpg -resize 1000 -output-directory output/ *.png

Filters

  • Blur: -blur <radius>
  • Gaussian: -gaussian <radius>
  • Border: -bordercolor <color> -border <width>x<height>
  • Charcoal: -charcoal <factor>

Borders

# Set border color to red and add a 10px border
gm convert image.jpg -bordercolor '#FF0000' -border 20 output.jpg

Brightness, saturation, hue

  • Modulate: -modulate <brightness>,[<saturation>],[<hue>]
    gm convert image.jpg -modulate 100,0 grayscale.jpg
    
  • Gamma: -gamma <value>

GIFs

# Create a GIF with a delay of 2 seconds before every image
gm convert -delay 200 output/img02.jpg -delay 200 output/img03.jpg anim.gif
# Simplest way to create a GIF, resizing the output
gm convert IMG* -resize 500 test.gif

Composing

  • Use gm composite and -compose <operator>.
    gm composite img1.jpg -compose Multiply img2.jpg img-comp.jpg
    
    • Check man gm for a list of operators.

Useful parameters

  • -size 200x200 (before operations) allows to make the process faster because it gives a hint to the JPEG decoder that the image is going to be downscaled to 200x200.
  • +profile "*" removes any ICM, EXIF, IPTC, or other profiles that might be present in the input.
Test with this online terminal:

If you have any suggestion, feel free to contact me via social media or email.