使用 ImageMagick crop images to specific aspect ratio

1.安裝 ImageMagick

yum install ImageMagick

檢查版本:

convert --version

 Version: ImageMagick 6.7.8-9 2019-08-08 Q16 http://www.imagemagick.org
 Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
 Features: OpenMP

2.下載: ASPECTCROP
http://www.fmwconcepts.com/imagemagick/downloadcounter.php?scriptname=aspectcrop&dirname=aspectcrop

3.安裝 aspectcrop

sudo mv aspectcrop /usr/local/bin
sudo chmod +x /usr/local/bin/aspectcrop

4. 使用 aspectcrop

EX1:
aspectcrop -a 16:9 test.JPG test_cropped.JPG

The other option to be manipulated is -g gravity which defines which part of the image is used for cropping. The default is center, all options are:

center (c)
north (n)
south (s)
east (e)
west (w)
northwest (nw)
northeast (ne)
southwest, (sw)
southeast (se)
So to give an example of this using north:

EX2:
aspectcrop -a 16:9 -g n test.JPG test_cropped.JPG
整個目錄下轉檔:

for file in *.JPG; do aspectcrop -a 16:9 -g c $file ${file/%.JPG/_cropped.JPG}; done

參考說明:
1.https://askubuntu.com/questions/762692/imagemagick-crop-images-to-specific-aspect-ratio

2. http://www.fmwconcepts.com/imagemagick/aspectcrop/index.php

5. Resizing Images

for file in *.jpg; do convert $file -resize 1920X1080 ${file/%.jpg/.jpeg}; done