Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash convert resize recursively preserving filenames

Have images in subfolders that need to be limited in size (720 max width or 1100 max height). Their filenames must be preserved. Started with:

for img in *.jpg; do filename=${img%.*}; convert -resize 720x1100\> "$filename.jpg" "$filename.jpg"; done 

which works within each directory, but have a lot of subfolders with these images. Tried find . -iname "*.jpg" -exec cat {} but it did not create a list as expected.

This also didn't work:

grep *.jpg | while read line ; do `for img in *.jpg; do filename=${img%.jpg}; convert -resize 720x1100\> "$filename.jpg" "$filename.jpg"`; done 

Neither did this:

find . -iname '*jpg' -print0 | while IFS= read -r -d $'\0' line; do convert -resize 720x1100\> $line; done

which gives me error message "convert: no images defined." And then:

find . -iname '*.jpg' -print0 | xargs -0 -I{} convert -resize 720x1100\> {}

gives me the same error message.

like image 532
motorbaby Avatar asked Nov 11 '16 17:11

motorbaby


1 Answers

It seems you're looking for simply this:

find /path/to/dir -name '*.jpg' -exec mogrify -resize 720x1100\> {} \;

In your examples, you strip the .jpg extension, and then you add it back. No need to strip at all, and that simplifies things a lot. Also, convert filename filename is really the same as mogrify filename. mogrify is part of ImageMagick, it's useful for modifying files in-place, overwriting the original file. convert is useful for creating new files, preserving originals.

like image 199
janos Avatar answered Sep 23 '22 01:09

janos