How-To

Mass convert JPEGs, PNGs, GIFs and more, with a single Terminal command

Converting a file to a different format isn’t exactly a complicated process, but if you’re dealing with a large number of files then it can quickly become one of those tedious, time-consuming tasks that no one enjoys doing.

Maybe you want to post some images to a website that has strict rules about the formats it accepts, or perhaps you want to share your holiday snaps with a friend who can only open certain files on their computer. Alternatively, maybe you’re aware that not all images are created equally, and want to free up precious disc space by converting high-resolution images to a less memory-hungry format.

Whatever your motivation, if you have a collection of images that you need to convert from one format to another, then the last thing you’ll want to do is tackle each file individually.

While the App Store is full of software that promises to take the pain out of file conversions, why go to the trouble of installing a third party app, when your Mac already has everything it needs to quickly and easily convert hundreds of files?

In this article, I’ll show you how to convert images en masse, using your Mac’s built-in Sips (Simple Image Conversion) tool.

Converting a single file

Sips is a command line tool, so you can only access it via your Mac’s Terminal:

  • Open a new Finder window.
  • Navigate to ‘Applications > Utilities.’
  • Launch the Terminal.

Although Sips’ real power lies in its ability to convert multiple images, you may want to practice on a single image first, especially if you’re unfamiliar with command line tools.

You can convert a single image, using the following Terminal command:

sips -s format [output file type] [file name] –out [output file location, name and format]

For example, imagine you had a ‘cat.png’ image on your Desktop. If you wanted to convert this file to ‘cat.jpeg,’ then you’d type the following command into the Terminal window:

sips -s format jpeg /Users/jessicathornsby/Desktop/cat.png –out /Users/myusername/Desktop/cat.jpeg

Then, press the ‘Enter’ key on your keyboard, and Sips will create a duplicate ‘cat’ image, in JPEG format, and store it on your Desktop, without deleting the original PNG.

If you don’t specify a location where ‘cat.jpeg’ should be stored, then Sips will place the JPEG in your Mac’s ‘Home’ directory. For example, the following command would create a ‘dog.png’ file inside the ‘Home’ directory:

sips -s format png /Users/myusername/Desktop/dog.jpeg –out dog.png

You don’t have to type the whole command!

When typing Terminal commands, you’ll often have to include file paths, which can be long and complex, and generally frustrating to type.

Whenever you need to include a file path, you can save yourself some time by finding the file in question, dragging it to the Terminal, and then dropping it onto the Terminal window. This will automatically add the entire path, file name and file extension to your command.

Using the previous ‘dog.png’ command as an example, you’d perform the following steps:

  • Type the first part of the command (sips -s format png).
  • Drop the ‘dog.jpeg’ file into the Terminal window, which will automatically add /Users/myusername/Desktop/dog.jpeg to the command.
  • Type the final part of the command (–out dog.png).
  • Press the ‘Enter’ key on your keyboard, and the conversion will go ahead as normal.

Change the file’s quality

When converting your images, you may want to specify the quality of the newly-converted image, especially if you’re converting your files to try and free up some disc space!

To control the quality, add “formatOptions” to your command, and then either specify a percentage, or use the words “low,” “normal,” “high” or “best.” For example, if you wanted to create a JPEG file at 70% the original file’s quality, then you’d use the following:

sips -s format jpeg -s formatOptions 70 /Users/myusername/Desktop/cat.png –out cat.jpg

So, how do I convert multiple files?

In reality, you’d rarely use Sips to convert a single file, so let’s look at how you can convert multiple images with a single command.

The easiest way to convert a large number of images, is to place all these files in the same folder, then “change directory” (cd) so the Terminal is pointing at said folder. For example, if you had an ‘images’ folder on your desktop, then you’d run the following command:

cd /Users/myusername/Desktop/images

Once the Terminal is pointing at the correct folder, you can convert every file inside this folder, with the following command:

for i in *.[current file format]; do sips -s format [desired file format] “${i}” –out “${i%current file format}desired file format”; done

This may look complicated, but you don’t need to know what the above command means, you simply need to copy/paste it into the Terminal, and then tweak it to specify the file input and output. For example, if you wanted Sip to convert a bunch of PNG files to JPEG, then you’d run the following:

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

If you really wanted to perform the maximum amount of work in the minimum amount of time, then you could change the quality of all these files, as you’re converting them. For example, the following command would convert every PNG to a JPEG while also reducing their quality to 80% of the original files:

for i in *.png; do sips -s format jpeg -s formatOptions 80 “${i}” –out “${i%png}jpg”; done

About the author

Jessica Thornsby

Jessica Thornsby is a technical writer based in Sheffield. She writes about Android, Java, Kotlin and all things Apple. She is the co-author of O'Reilly's "iWork: The Missing Manual," and the author of "Android UI Design," from Packt Publishing.

Add Comment

Click here to post a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.