I was updating my blog and needed to generate few variants of images, in different resolution.
Option 1 - sips
There’s simple, builtin tool sips
, that can be used for simple resizing 1:
sips -Z 36 orig.png --out static/favicon36x36.png
-Z
- maintain image aspect ratio36
- maximum height and width
It can be also used for batch image processing:
Warning
Beware, without –out param, it will overwrite images in place!
sips -Z 1024 *.jpg
Option 2 - imagemagick
For more complicated use cases, imagemagick
have no competition. It’s not available out of the box, you have to install it:
brew install imagemagick
Imagemagick provides additional abilities to resize and then crop images. Let use cat image below as a demo.
Just scale keeping whole image
convert demo.webp \
-resize 300x100 \
demo-just-resize.webp
The resulting images is:
identify demo-just-resize.webp
demo-just-resize.webp WEBP 67x100 67x100+0+0 8-bit sRGB 4076B 0.000u 0:00.000
Scale down but keeping width
convert demo.webp \
-resize 300x100^ \
demo-just-resize2.webp
The resulting image is:
identify demo-just-resize2.webp
demo-just-resize2.webp WEBP 300x450 300x450+0+0 8-bit sRGB 8078B 0.000u 0:00.002
Resize and crop from top
convert demo.webp \
-resize 300x100^ \
-extent 300x100 \
demo-resize-crop.webp
The resulting image is:
identify demo-resize-crop.webp
demo-resize-crop.webp WEBP 300x100 300x100+0+0 8-bit sRGB 3400B 0.000u 0:00.001
Resize and crop center
convert demo.webp \
-resize 300x100^ \
-gravity Center \
-crop 300x100+0+0 +repage \
demo-resize-crop2.webp
The resulting image is:
identify demo-resize-crop2.webp
demo-resize-crop2.webp WEBP 300x100 300x100+0+0 8-bit sRGB 5202B 0.000u 0:00.001
As you might guess, there are many cases and far more options, than described in this post. Personally, I was pretty satisfied with the last one. I was able to cut most of images properly, being able to generate nice cover images with just one command.