Jpeg, png and heic to webp

 · 1 min · torgeir

WebP offers a compelling combination of features for images on the web; small image sizes, high image quality with both transparency and animation support. Convert to webp on macos.

Terminal Images Macos Image-Optim Jpeg Png Heic Webp

Here’s a few lines to convert jpeg and png images to webp on macos, using

  • the macos “Scriptable Image Processing System” sips, to convert an image to jpeg
  • the docker image toy/image_optim to compress and strip images of EXIF data
  • the Google binary cwebp to convert the result to webp
# converts image file given as $1 to webp
# optionally specify the quality as $2, defaults to 25/100
function image-to-webp () {
  file=$1
  basename="${file%.*}"
  quality=${2:-25}
  sips -s format jpeg $file --out $basename.jpeg
  docker run --rm -v "$PWD":/here -w /here ghcr.io/toy/image_optim $basename.jpeg
  cwebp -q $quality $basename.jpeg -o $basename.webp
  rm $basename.jpeg
}

You can run sips -g all to check that the EXIF information is really gone.

Before

du -h asdf.HEIC
sips -g all asdf.HEIC
1,4M	asdf.HEIC
/tmp/asdf.HEIC
  pixelWidth: 4032
  pixelHeight: 3024
  typeIdentifier: public.heic
  format: heic
  formatOptions: default
  dpiWidth: 72.000
  dpiHeight: 72.000
  samplesPerPixel: 3
  bitsPerSample: 8
  hasAlpha: no
  space: RGB
  profile: Display P3
  creation: 2023:07:07 22:47:23
  make: Apple
  model: iPhone 12 Pro
  software: 16.5.1

After

du -h asdf.webp
sips -g all asdf.webp
64K	asdf.webp
/tmp/asdf.webp
pixelWidth: 3024
pixelHeight: 4032
typeIdentifier: org.webmproject.webp
format: webp
formatOptions: default
dpiWidth: 72.000
dpiHeight: 72.000
samplesPerPixel: 3
bitsPerSample: 8
hasAlpha: no
space: RGB