If you’ve longed for a quick way to convert a handful of images (or more) from one format to another, I’ve got a treat for you. I use this quick and dirty bash script mainly to convert from PNG to JPG files, but it can be easily modified and tweaked to do other things. We’re going to use it to batch convert image files from PNG to JPG. Note that this should work on just about any version of OS X or macOS; the utility it depends upon has been around for quite some time.
How I Found a Way to Batch Convert Image Files
It’s a pretty common thing for me, needing to batch convert image files from PNG to JPG. When I drag pictures out of the Photos app on my Mac, they’re almost inevitably exported as PNG. We prefer to use JPG at TMO, so I’d been converting them one at a time using Preview’s Export feature. After finding this tool, I found that you can actually use Automator to batch convert and resize images. I’m a Terminal junkie, though, and prefer my method.
Then I found out (through an amazing feat of Google-Fu) about a funny-named little command in the Terminal called sips. The name stands for “scriptable image processing system,” and it’s extremely handy. The utility is able to resize images constrained to a particular size, resample your photos, and convert your pictures from one format to another.
Scripting sips in the Terminal
If you just have one image to process, this Terminal command will convert it from PNG to JPG. Just make sure you’re in the right directory first. You do this by typing cd <directory name> in Terminal. You can also drag the folder icon of your directory into Terminal, which is usually much quicker.
sips -s format jpeg -s formatOptions 80 IMG_1354.png –out IMG_1354.jpg
That would convert IMG_1354.png to a JPG file with the image quality set to 80 percent. That’s all well and good, but what if you have multiple images you want converted?
Batch Scripting sips
That’s where the beauty of bash scripting comes into play. This short script, which I have as a snippet in TextExpander, makes short work of a host of PNG files.
for i in *.png; do sips -s format jpeg -s formatOptions 80 "${i}" –out "${i%png}jpg"; done
What that does is set up a loop for each PNG file you have in that directory. The script runs sips on each one, converting them as described above.
One important thing to note – when you put this script into TextExpander, you have to escape the percentage sign. So I actually typed it as “{i%%png}jpg” into TextExpander. That’s because TextExpander uses the percentage sign for other purposes.
Enjoy!
Learned something new with the Terminal today… thanks for that. But using an Automator script tucked under the “Services” menu works well enough for me to convert just about any image file (PSD, PNG, GIF, etc) to a JPG.
Neato, but I’ll stick to Automator. I just don’t have the memory cells for complex Terminal commands….
For those looking for an inexpensive GUI tool, look at PhotoBulk. It’s available in the App store.