TIL: Convert a PNG to a JPG with sips

Armno's avatar
Published on November 5th, 2019
By Armno P.

Today I learn how to use sips to convert a PNG image to a JPG image from the command line from The Robservatory blog.

sips ā€“ The Scriptable Image Processing System ā€“ is a command line tool that comes pre-installed with macOS (Iā€™m using 10.15 Catalina). It can convert a PNG to a JPG like this:

$ sips --setProperty format jpeg input.png --out output.jpg

With JPG as an output file type, we can add formatOptions property to the command to set JPG quality level of the output file to get smaller file size.

# set quality to 'normal`
$ sips --setProperty format jpeg \
  --setProperty formatOptions normal \
  input.png \
  --out output.jpg

# or set quality to a percentage value
$ sips --setProperty format jpeg \
  --setProperty formatOptions 42.195 \
  input.png \
  --out output.jpg

Convert the last captured screenshot to a JPG

When I captured a screenshot to send via Slack or to attach in a ticket, I often found the captured PNG file is way too big in terms of the file size, especially when the screenshot has photos in it.

Captured screenshot in PNG format
Captured Screenshot in PNG format: 4.5MB

I wanted to quickly convert it to a JPG. I created a bash function in my .functions dotfile to do that.

function peg() {
  filename=$(ls -1t $HOME/Desktop/Screen\ Shot* | head -n 1)
  sips --oneLine --setProperty format jpeg "$filename" --out "$HOME/Desktop/screenshot.jpg"
}

So when I captured the screenshot, I go to the terminal and run peg command. I get the JPG version of the file named screenshot.jpg.

Captured screenshot after converted to JPG format: 666KB
Captured Screenshot after converted to JPG format: 666KB

More info about sips on the sips man-page.

Tags:

Related posts