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
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.
# 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
Does sips have a command line switch to maintain exif/metadata?
Make sure you create the “Converted” folder first before running the command.
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
Very cool, thanks for that shorter method to convert the images xeno!
This method works best. Thanks for your input.
for i in *.gif; do sips -s format png $i –out ${i%.*}.png; done
for i in *.jpg; do sips -s format png $i –out converted/$(basename $i .jpg).png; done
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
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
Picasa, all graphic and from anywhere in the pictures, preview or thumbnails just hit export and choose your size
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
Very smart, love this thanks!
I do this with Preview, no offense to the unix gods but I think it’s a lot easier.
coreimage is more powerful,
but need to add the binaries ;)