Convert image extension in Terminal

Convert image extension in Terminal

Do you have images in a folder that you actually want to convert to another image extension? Let's say you want to convert all images from PNG to JPG. If you are using OSX you can start your terminal and run the following command:

# from *.png to *.jpg
sips -s format jpeg *.png --out .

What do the above commands mean:

  • -s format jpeg This will be the new image format. You can use any of jpeg | tiff | png | gif | jp2 | pict | bmp | qtif | psd | sgi | tga
  • *.png This part secifies which file to process, in our case we tell sips to grab all files ending in .png
  • --out . In this last part we tell sips to output processed images in the same (.) folder as the source files. If you want to output all files in a jpg-images you could type: --out jpg-images. Note: the specified folder should already exist.

Bonus: compress image quality

But wait there is more: what if you want to optimise images in one go? If your chosen format accepts quality settings, like jpeg does, you can specifify a quality percentage. This will reduce your image even more, and can save you enough disk space. Let's try it out:

sips -s format jpeg *.png -s formatOptions 50 --out compressed
  • -s formatOptions 50 This means: set the quality setting to be 50% of the original.

Bonus resize image

Now let's see how to resize an image but keep the aspect ration of the image. Thus an image that is currently 600 x 600 will become 800 x 800 when we run the following command:

# sips --resampleHeight <pixels> <image.ext> Resample image to specified height.
# sips --resampleWidth <pixels> <image.ext> Resample image to specified width.
# sips --resampleHeightWidthMax <pixels> <image.ext>  Resample image so height and width aren't greater than specified size.
sips --resampleHeight 800 <image-name>.jpg

If you want to read more about SIPS, you can start here.