Search code examples
imagemagickcropimage-resizing

Use Imagemagick to crop large images to a specific aspect ratio and then resize them


I have a bunch of high-resolution images from Unsplash and,

  1. First need to crop to a 16:9 aspect ratio (from the center), and then,
  2. Resize them to 2560x1440px.

How do I do this using Imagemagick on a Mac?


Solution

  • I think this will do what you need for a single image:

    magick INPUT.JPG -gravity center -extent 16:9 -resize 2560x1440 RESULT.JPG 
    

    If you want to do lots, you could use a bash loop:

    for f in *.jpg *.png ; do
       magick "$f" -gravity center -extent 16:9 -resize 2560x1440 "$f"
    done
    

    If you have thousands, you could use homebrew to install GNU Parallel with:

    brew install parallel
    

    Then get them done in parallel using all your CPU cores with:

    parallel --bar magick {} -gravity center -extent 16:9 -resize 2560x1440 {} ::: *.jpg *.png
    

    If you have hundreds of thousands, or millions, that will overflow the ARG_MAX, so you will need to feed the filenames on stdin like this:

    find . -iname \*.png -print0 | parallel -0 magick {} -gravity center -extent 16:9 -resize 2560x1440 {}