How to find & auto resize images?

I need a command or mini scripts that can find certain type of images file recursively from the current dir with files of minimum 736 width and resize them by "736 max width, relative height" and replace the source files. I currently have GD installed but can also install Imagemagick if needed, I'm not sure which is better. Is this possible? Help is appreciated. Thanks in advance.

use imagemagick

use -resize parameter

This is what I get from reading several guides but not 100% sure if it's correct. If can confirms:

find . -type f -name '*.jpg' -o -name '*.jpeg' -o -name '*.gif' -o -name '*.png' -exec convert -resize '>736' \;

No, you don't have it correctly. There are a couple problems with your line:

  1. convert takes 2 filenames:
convert <inputfile> <outputfile>

If you want to perform the operation on the same file, use mogrify instead.
Note that this will overwrite the original file!

  1. You are not passing the filenames that find prints to convert . This is done using {} . E.g.
 find . -type f -name "*.jpg" -exec convert -resize '>736' {} {}.resized \;

But that will keep the suffix, so file image.jpg will become image.jpg.resized.
When renaming with find, it is safer and more convenient to create a script and then do -exec myscript.sh {} \; on a whole set of files.
At first your script can echo the command you are going to run, then you verify the terminal output that it looks right, test it on one or few files and then run it on the whole set.

If you are sure you want to overwrite your inputs, just use mogrify:

find . -type f -name ..... -exec mogrify -resize '>736' {} \;

I don't know what '>736' specifier means. I assume you got that correct.

1 Like

thanks they work but for some reason this part of the code doesn't work, the script only work when I remove it, but I need to add more file extension.

-o -name '*.jpeg' -o -name '*.gif' -o -name '*.png'

what is the correct way to add more file extension to my options? thanks

Change:

-name '*.jpg' -o -name '*.jpeg' -o -name '*.gif' -o -name '*.png'

to:

\( -name '*.jpg' -o -name '*.jpeg' -o -name '*.gif' -o -name '*.png' \)
2 Likes