Resizing images using Mac command line
Recently while writing on my blog, I realized that some of my images were too big to upload on the online space. Problem was slow upload and then slow download for blog pages. It is quite obvious to happen for multiple images sized more than mega byte.
I am quite annoyed why Ghost do not automatically compress images while uploading them on server? Facebook does it. It will ask users if they are ok with resizing and cutting down on photo size as it may result in faster download. Problem was when blog writing was in progress, uploading multiple images would grind the page to halt. It would either scroll so slowly or just sit their refusing to respond to user actions.
Enough of digression, back to the point now. In order to avoid above mentioned problems, I started using online image resizing service to scale down image to desired dimensions thereby reducing their total size. However, it presented another problem. Large images, especially more than 2 mega bytes would take forever to upload, conversion and downloading back. It was too much of an overkill. Again, no online image resizing service offered batch uploading of multiple images and scaling all of them to specific ratio. (50%, 25% etc.).
Luckily this afternoon I found a great solution which would solve all the above mentioned problems. I came to know about Mac sips utility. sips is an acronym for Scriptable image processing system. As mentioned in the documentation, it allows you to do so many operations on image such as file conversion, resizing, rotation, cropping and metadata analysis.
Thanks to LifeHacker post, I came to know about simple one line command for offline resizing of images. For eg. to resize all jpg
images in the current folder to size of x
pixels,
sips -Z x *.jpg
Where,
sips
is a command for our image processing utility
-Z
tells the utility to maintain image aspect ratio
x
is the dimension value in pixels which would be maintained by either height or width whichever is maximum and then minimum dimension would then be scaled by the same ratio so as to maintain aspect ratio.
To make things more clear, say you have image of size 3000(W) X 2000(H) pixels and you specify
x
to be 1000 pixels, width will be resized to 1000 pixels since it is maximum dimension and then height will be resized to2000/3 = 666.67 pixels
so as to maintain aspect ratio.
*.jpg
tells utility to include all jpg
files in the current folder. It can also be used in other ways. For example I had image names as IMG_0031.jpg, IMG_0032.jpg, IMG_0033.jpg etc. in my current folder. I had no reason to resize all jpg files. So I just typed in the following command to convert all the files matching pattern to the size of 1200 pixels,
sips -Z 1200 IMG_003*
You can find official Apple documentation about sips
Here. If you have trouble following documentation, you may visit OS X Daily or LifeHacker Blog for more examples and to get acquainted with the utility.