MacOS sips Image Processing Tutorial
What is sips
macOS has a image processing command line tool named sips
sips
stand for Scriptable Image Processing System.
it can convert image formats, rotate, flip, resize, etc.
Get Image Width Height
# get width sips -g pixelWidth xx.jpg # get height sips -g pixelHeight xx.jpg # get both width and height sips -g pixelWidth -g pixelHeight xx.jpg # sample output # /Users/xah/Desktop/xx.jpg # pixelWidth: 1024 # pixelHeight: 768
convert
# convert one png to jpg sips -s format jpeg x.png --out x.jpg
resize
# resize to 500 by 500 sips -z 500 500 x.png --out x2.png
# Resize the largest side, keep aspect ratio sips -z 600 old.jpg --out new.jpg
Overwrite Original
to overwrite original, just don't use the parameter --out
do all files in a dir
# convert all png in a dir to jpg # file name such as x.png becomes x.png.jpg for ff in ~/Desktop/sshot/*.png; do sips -s format jpeg $ff --out $ff.jpg; done
# convert all png in a dir to jpg # also delete original for ff in ~/Desktop/sshot/*.png; do sips -s format jpeg $ff --out $ff.jpg && rm $ff; done
# rename any .png.jpg to .jpg for ff in *.png.jpg; do mv $ff "${ff%.png.jpg}.jpg"; done