Nono.MA

Batch-transforming images with the command-line and ImageMagick

FEBRUARY 16, 2022

I often look for ways to batch-transform images and videos with command-line tools instead of heavy image-editing tools, such as Photoshop or Gimp.

Today, I needed to resize and crop a series of images to ensure they had a height of one thousand pixels.

convert \
  input.jpg \
  -resize 1500x1000^ \
  -gravity center \
  -extent 1500x1000 \
  -quality 90 \
  converted.jpg

Here's the same command, with comments.

convert \
  # Input image path/name
  input.jpg \
  # Desired resize size
  -resize 1500x1000^ \
  # Where to position the cropping frame
  -gravity center \
  # Cropping frame size
  -extent 1500x1000 \
  # JPEG compression quality
  -quality 90 \
  # Output file path/name
  converted.jpg

To batch convert images, really, you'd have to repeat this command manually. But there has to be a way to parametrize the input and output names. The one way I found, not using the original input names though, is to use a formatted numbered string.

convert input.jpg -resize 50% output_%03d.jpg

Blog