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:

Resize single image
sips -Z 36 orig.png --out static/favicon36x36.png

  • -Z - maintain image aspect ratio
  • 36 - maximum height and width

It can be also used for batch image processing:


Beware, without –out param, it will overwrite images in place!

Batch image resizing
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:

Install imagemagick
brew install imagemagick

Imagemagick provides additional abilities to resize and then crop images. Let use cat image below as a demo.

Source: www.pexels.comexternal link

Just scale keeping whole image

Scale down
convert demo.webp \
	-resize 300x100 \

The resulting images is:

Check the result
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

Scale down keeping width
convert demo.webp \
	-resize 300x100^ \

The resulting image is:

Check the result
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

Resize and crop from top
convert demo.webp \
	-resize 300x100^ \
	-extent 300x100 \

The resulting image is:

Check the result
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

Resize, then crop
convert demo.webp \
	-resize 300x100^ \
	-gravity Center \
	-crop 300x100+0+0 +repage \

The resulting image is:

Check the result
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.