Thursday, July 19, 2012

Command line script to batch convert images

Ever had a folder full of images that you needed to convert? Well, today I had a couple hundred 20Mb RAW images that I wanted to convert to JPG, and I really didn't want to open them on an image editor.

So I went hunting for a way to convert the images using the command line on the Mac. Here's what I found:

1. Converting an image via command line: There's a tool called "sips" that does this (and much more) on the mac! To convert a file from RAW to JPG, you just need to call something like:

sips -s format jpeg IMAGE.NEF --out IMAGE.jpg

2. Getting the right name: Iterating a list of files via command line is easy, getting just the filename in order to change the extension requires google! Here's how to do it:

for i in *.NEF; do echo "${i%.*}.jpg"; done

(curious on how it works? Check this doc)

3. All together now: To convert all the files in the current directory, do this:

for i in *.NEF; do sips -s format jpeg $i --out "${i%.*}.jpg"; done

Let me know how the script worked out for you!
Post a Comment