Converting Image File Formats with the Command Line & sips

Jan 11, 2013 - 16 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

.

Related articles:

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

16 Comments

» Comments RSS Feed

  1. Veronica McC says:

    I tried the batch convert of RAW images with preview in my MacBookPro El Capitan, but it only woorked with the panoramic photos, not the rest. What other method could I try? I’m a novice at this. Thanks.

  2. Nyimbi Odero says:

    # Sort out the issue of jpeg.png to just png
    for i in *.jpeg;
    do
    sips -s format png $i –out Converted/”${i%jpg}png”;
    done

  3. Robertjm says:

    Does sips have a command line switch to maintain exif/metadata?

  4. Darryl says:

    Make sure you create the “Converted” folder first before running the command.

  5. xeno says:

    No need for “for” loop:

    sips -s format png *.jpeg –out Converted

    instead of

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

  6. anon says:

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

  7. Link says:

    for i in *.jpg; do sips -s format png $i –out converted/$(basename $i .jpg).png; done

  8. Liz Mannering says:

    Another way to prevent the double-filetype problem is to strip the filetype out of the variable i before you name the png:

    for i in *.jpg; do sips -s format png $i –out pngs/${i/.*/}.png; done

  9. 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

  10. Bern says:

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

  11. 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

  12. Steebz says:

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

  13. adam says:

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

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

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

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site