I have recently come across sips
which is MacOS terminal built-in image processing tool. It is very useful to crop or resize images on the fly without having to use, God forbid, tools like Photoshop, or any UI.
How Cool is that!
Syntax
sips [query/modification] file
Nice usages
Here’s our test subject. The website’s avatar.
In order to get some data about the image, we use:
sips -g all image
This will give all the available info about the image. Here’s the output:
pixelWidth: 709
pixelHeight: 709
typeIdentifier: public.png
format: png
formatOptions: default
dpiWidth: 72.000
dpiHeight: 72.000
samplesPerPixel: 3
bitsPerSample: 8
hasAlpha: no
space: RGB
Now let’s resize the image:
sips -o name -z [H] [W] image
- Here, I use
-o
in order to output to a different file. - This will resize the image to the given width and height. Make sure to use the correct aspect ratio though.
- The below is the output of using [H]=320 and [W]=320.
sips -c [H] [W] image
This will crop the image to the required width and height. The offset of the crop here is 0.
sips -c [H] [W] --cropOffset [Y] [X] image
This will crop the image to the set [H]eight and [W]idth but with an offset of [Y] vertically and [X] horizontally.
Here’s the output of using sips -o output -c 500 500 --cropOffset 104.5 104.5 image
P.S. I am using 104.5 in order to keep the avatar in the middle of the image. Since the image is 709*709px
, this means that I need to offset my crop by (709-500)/2
in order to keep the same composition.