Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Friday, January 31, 2014

Use 'sips' to manipulate images in CLI.

sips (scriptable image processing system) is a prebundled command tool which can used to manipulate images. There are two advantages:

- it is prebundled, you don't need to download and install it.
- it uses CoreImage so it is very fast.

Here are some examples:

1. Get Image size
$sips --getProperty pixelWidth  xxx.jpg
  pixelWidth: 3264

$sips --getProperty pixelHeight xxx.jpg
  pixelHeight: 2448

2. Resize an image
$sips -z 100 100 xxx.jpg
  resize xxx.jpg to 100x100

$sips --resampleWidth 100 xxx.jpg
  resize xxx.jpg 's width to 100, and resize the height to keep its ratio.

$sips --resampleHeight 100 xxx.jpg
  resize xxx.jpg's height to 100, and resize its width to keep the original ratio.

You can check its man page here: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/sips.1.html

src

Monday, August 27, 2012

rm -R with wildcard

Unfortunately, it seems the shell of Mac OSX doesn't support wildcard in 'rm -R' command. So if you want to delete, for example, some "*.swp" files which locates on many sub-directories, it will be very difficult.

However, there is a workaround by using the command "xargs". Here is the method:

$ find . | grep *.swp | xargs rm

What's the meaning?

First, "find ." will list all the files / directories in the current directory (.).
Then, the "|" will send the results to "grep", and grep will filter all the files/directories which don't meet "*.swp", that is, all the *.swp will be listed by "grep".
At last,  "xargs" will use the results returned by "grep" as argument for "rm", so that the file will be removed.