Resize images with find and ImageMagick

$ find pictures/ -type f -iname '*.jpg' | xargs -I '{}' convert '{}' -resize 150x150 -quality 100 '{}'.png 

Command break down

  1. find is a command that searches for files and directories, optionally matching certain criteria.
  2. pictures/ tells find where to start its search.
  3. -type f is the first search criterion: it tells find to only look for files
  4. -iname ‘*.jpg’ tells find the second search criterion: do a case insensitive search for all files ending with`.jpg`. The case insensitivity causes this criterion to also match `.JPG` or `.jPg`
  5. The |, called the pipe symbol, causes the shell to pipe the output of the command on its left hand side to be used as the input of the command of the right hand side.
  6. xargs executes the command given as its arguments to be executed for each line of input.
  7. -I ‘{}’ instructs xargs to replace every occurrence of ‘{}’ in the arguments that follow with the content of the current line of the input.
  8. convert is part of the ImageMagick image manipulation package.
  9. ‘{}’ will be replaced by the content of the current line of the input. Say, the first file `find` found is `pictures/aaa_first.jpg` the effective command for this iteration of xargs would read convert pictures/aaa_first.jpg ...
  10. -resize 150x150 instructs convert to change the size of the image at hand to a maximum size pf 150×150, while maintaining the aspect ratio. So, for example if the original picture was 300 x 200, the resulting picture would be 150×100. If the original size was 150×300, the dimensions of the converted image would be 75×150.
  11. -quality 100 instructs convert to generate with the best quality it can achieve. Sometimes, you do not need (or even want) full quality images, for example for previews. In this case, you could set -quality 80.
  12. ‘{}’.png tells convert where to save the output to. In this example, to pictures/aaa_first.jpg.png As convert is rather smart and detects the image type of the original JPG, it knows that it is supposed to write the output file in PNG format.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.