The syntax for the command convert in ImageMagick is

convert source_filename.ext1  destination_filename.ext2

I would like to use zsh to feed it with files in a folder path_to_source and output the result (files with same name, but with a different extension) under the folder path_to_destination.

I believe I can use find and xargs for this, but I didn't come up with anything that worked. Also I presume that ZSH may have some builtins that are useful for this. Any pointers would be greatly appreciated.

NOTE: As far as I know, convert does not have a native way of handling a batch of files, so I need to feed the files one by one. In either case, I'm interested in solving the problem of feeding a command file by file with these arguments.

3 Answers 3

for x in /path/to/source/**/*.ext1; do convert $x ${x:r}.ext2 done

The r in ${x:r} is a history modifier. There's a short form of for that saves a few characters.

for x (/path/to/source/**/*.ext1) convert $x ${x:r}.ext2

I would do this with:

cd path_to_source
find . | sed 's/.ext1$//' | xargs convert {}.ext1 ../path_to_destination/{}.ext2

You can batch process your files with mogrify and convert from one format to another using the -path option (as of imagemagick v6.2.0) to specify a different directory in which to output the processed images:

mogrify -path /path/to/dest_filename -format ext2 /path/to/source_filename/*.ext1

so e.g.

mogrify -path /home/myjpgs -format jpg /media/images/*.png

will convert all .png images in the /media/images directory to jpg keeping the same filename (only the extension changes) and saving the resulting images to /home/myjpgs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.