Converting Image File Formats with the Command Line & sips

Jan 11, 2013 - 6 Comments

Convert image files from the command line

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

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS X, Tips & Tricks

6 Comments

» Comments RSS Feed

  1. adam says:

    coreimage is more powerful,
    but need to add the binaries ;)

  2. Steebz says:

    I do this with Preview, no offense to the unix gods but I think it’s a lot easier.

  3. Matt says:

    If you change the batch command to use bash variable pattern substitution, then you can prevent the resulting file name from having two extensions.
    Example 1:

    for i in *.jpeg; do sips -s format png $i –out Converted/${i%jpeg}png;done

    Example 2:
    for i in *.jpeg; do sips -s format png $i –out Converted/${i/%\.jpeg/\.png};done

  4. Bern says:

    Picasa, all graphic and from anywhere in the pictures, preview or thumbnails just hit export and choose your size

  5. Lauri Ranta says:

    One shortcoming with sips (and Automator and Preview) is that they always use a resizing method like bilinear in CS applications, so images that are made smaller can look blurry unless you add sharpening to them manually. And sips doesn't have many commonly needed resizing or cropping options.

    It's probably better to learn to use ImageMagick. This would make images smaller if they are 501px or wider and convert them to jpg:

    for f in *.png; do convert -filter lanczos2 -format jpg -quality 90 -resize '500x>' "$f" "${f%png}jpg"; done

    mogrify modifies images in place:

    mogrify -filter lanczos2 -resize 70% *.png

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates