Converting Image File Formats with the Command Line & sips
Converting images to new file formats is very easy thanks to a variety of tools built directly into OS X (and most Linux distributions). Though the easiest method uses Preview for converting images, there’s a command line option that uses the same sips tool we’ve discussed before to perform batch resizing from the command line. Using sips, you can convert single images to new image formats, or even perform batch image conversions.
Simple Image Conversion from the Command Line
To convert a single image with sips, use the following command string syntax:
sips -s format [image type] [file name] --out [output file]
For example, on a file named “test.jpg” that you want converted to PNG, the sips syntax would be:
sips -s format png test.jpg --out test.png
Batch Image Conversion with sips
Converting a group of images is a little trickier, and using simple wildcards like when resizing with sips doesn’t work quite the same. You’ll find that using a generic wildcard like * doesn’t rename the file as well, so we’ll use very simple shell scripting instead with the following command syntax:
for i in [filename]; do sips -s format [image type] $i --out [destination]/$i.[extension];done
Putting that to use, we’ll convert a folder of .jpeg files to png files in a new subfolder of the current directory, called “Converted”:
for i in *.jpeg; do sips -s format png $i --out Converted/$i.png;done
Running that command may result in all JPEG images converted into PNG format in the new directory.
A potentially annoying catch is the resulting filenames will include the original filetype in them as well, meaning you’ll end up with files titled “test.jpeg.png”. The file extension stays correct, it’s only a naming issue. You could get around that by renaming them to begin with then adding the proper file extension afterwards with a similar bash script, using regex, or renamed manually with mv.
When running sips with some file formats you may encounter ‘lingpng warning’ errors regarding exif data, those errors can be ignored for the most part and the image conversion will still take place.
Thanks go to Thom for the batch conversion idea