Pour cela nous allons utiliser deux commandes fournis dans le paquet imagemagick, "identify" et  "mogrify" et la commande "awk" qui comme la commande "sed" est une commande aussi très puissante et permet de faire énormément de choses car elles intègrent beaucoup de fonctions mais on ne rentra pas dans le détail de awk ici.

Imagemagick est un logiciel très puissant qui permet de nombreuses manipulations d’images en ligne de commande tel que rotation, conversion et bien plus.

Il faut donc avant tout installe imagemagick

sudo apt install imagemagick

Puis pour redimensionner par lot :

$ identify *.jpg | awk '{split($3, TAB, "x"); W = TAB[1]; H = TAB[2]; if(W > 1200){system("mogrify -resize 1200x "$1)} if (H > 1200){system("mogrify -resize x1200 "$1)}}'

identify *.jpg → identifie les fichiers se terminant par jpg et passe à la commande axk.
awk → pour faire simple, divise la chaîne en un tableau qui va permettre la comparaison.
mogrify → permet ici de redimensionner, elle a plein d’autres possibilités.

Bref : je redimensionne à 1200 maxi les fichiers jpg, pour la hauteur et la largeur.

Pour info : Extrait du man awk concernant "split"
split(s, a [, r [, seps] ])
Split the string s into the array a and the separators array seps on the regular expression r, and return the number of fields. If r is omitted, FS is used instead. The arrays a and seps are cleared first. seps[i] is the field separator matched by r between a[i] and a[i+1]. If r is a single space, then leading whitespace in s goes into the extra array element seps[0] and trailing whitespace goes into the extra array element seps[n], where n is the return value of split(s, a, r, seps). Splitting behaves identically to field splitting, described above. In particular, if r is a single-character string, that string acts as the separator, even if it happens to be a regular expression metacharacter.

Le billet Tuto - Diminuer la dimension des images par lot en ligne de commande est apparu en premier sur le blog de Sima78.