Search code examples
bashimagemagick-convert

How to automate conversion of images


I can convert an image like this:

convert -resize 50% foo.jpg foo_50.jpg

How can I automate such a command to convert all the images in a folder?

You can assume every image has .jpg extension.

A solution easily adaptable to automate the conversion of all the images inside the subdirectories of the working directory is preferable.


Solution

  • You can use a for loop with pattern expansion:

    for img in */*.jpg ; do
        convert -resize 50% "$img" "${img%.jpg}"_50.jpg
    done
    

    ${variable%pattern} removes the pattern from the right side of the $variable.