0

I'm trying to resize all images in a directory and sub directories. I have this script which I got from here: Use mogrify to resize large files while ignoring small ones

and adjusted to:

identify -format '%w %h %i\n' ./*.jpg |
awk '$1 > 1200 || $2 > 1200 {sub(/^[^ ]* [^ ]* /, ""); print}' |
tr '\n' '\0' |
xargs -0 mogrify -resize '1200x1200'

but it only does the current directory and only .jpg extensions - ignores the upper case - .JPG

I've tried making adjustments but not making much progress.

mmc501
  • 103
  • 3

1 Answers1

3

You could combine identify with find, for example:

find . -type f -iname "*.jpg" -exec identify -format '%w %h %i\n' {} \;

which will run your identify command for every file recursively found with name ending in .jpg (case-insensitive).

Using your full example:

find . -type f -iname "*.jpg" -exec identify -format '%w %h %i\n' {} \; |
awk '$1 > 1200 || $2 > 1200 {sub(/^[^ ]* [^ ]* /, ""); print}' |
tr '\n' '\0' |
xargs -0 mogrify -resize '1200x1200'
sebasth
  • 14,332
  • 4
  • 50
  • 68
  • Awesome - works perfectly! In second code block just update the file extension to .jpg – mmc501 Aug 15 '17 at 08:29
  • I have just tried this same script on another server but just get the error: find: `identify': No such file or directory – mmc501 Aug 15 '17 at 10:53
  • Is imagemagick installed? – sebasth Aug 15 '17 at 11:01
  • Yes it is installed Trying again and get loads of the above error message and this at the bottom: xargs: mogrify: No such file or directory – mmc501 Aug 15 '17 at 11:14
  • How do I do that? – mmc501 Aug 15 '17 at 11:20
  • Try running just `identify` and `mogrify` to see if you get `command not found`, in which case there is a problem with your imagemagick install. – sebasth Aug 15 '17 at 11:24
  • Might be something like that alright ran: `mogrify -resize 50% 1test.jpg` and `magick mogrify -resize 50% 1test.jpg` First one said mogrify can be found on one of following packages and other one said no command 'magick' found. It was installed via [https://serverpilot.io/community/articles/how-to-install-the-php-imagemagick-extension.html](https://serverpilot.io/community/articles/how-to-install-the-php-imagemagick-extension.html) – mmc501 Aug 15 '17 at 11:34
  • Ah - sorted now - installed via [https://serverpilot.io/community/articles/how-to-install-the-imagemagick-executable.html](https://serverpilot.io/community/articles/how-to-install-the-imagemagick-executable.html) – mmc501 Aug 15 '17 at 11:36