Image Manipulation from the Command Line


I wanted to do something simple, really simple. I wanted to scale graphics – the ones you see in the stories on this site and the book covers in the right-hand column of the main page.

Now, there are many ways to do this – image manipulation programs from Photoshop to Graphic Converter, through tons of freeware and shareware programs, can scale images. But I wanted to do it as easily as possible. My ideal solution would be a drag-and-drop program, where I would simply drag a file and use the resulting scaled file to upload to my web server.I wasn’t able to find a single program that did what I want. So, as I often do, I looked in Terminal to see what commands could do this. The sips (scriptable image processing system) command can do all this and more. Using Apple’s Core Grahpics, sips manipulates images and displays information about them. It doesn’t do much – you can’t change an image from one format to another, but you can rotate, flip, crop and resize images with simple commands.

I’ve arbitrarily decided to present images at a width of 120 pixels in my articles, though I’ll make exceptions for graphics in landscape format. So to scale an image to that size, sips is perfect. I just run the following command (without typing the $; that’s the prompt you see in Terminal):


$ sips --resampleWidth 120

And the job is done. Type that in Terminal, and drag your image into the Terminal window (or type its file path) then press Return. Your original image file is replaced by the new one (so make sure to keep a copy of it somewhere). That’s all there is to it.

If you plan to do this often, create an alias. I did this, naming the alias “scale”: for bash, add the following to your .bash_profile file:


alias scale='sips --resampleWidth 120'

Check out the sips help (sips -h) to find out the other ways this command can save you time.

Note: I was chatting with Rob Griffiths of Mac OS X Hints about this, and he suggested an AppleScript droplet. He has since published the result of his search in this hint.


Read more articles in this category: Mac OS X Command Line

Posted: 9/24/2004 by kirk | Filed under: Apple & Mac OS X | 12 Comments »

12 Responses to “Image Manipulation from the Command Line”

  1. Anonymous says:

    Dear Kirk,
    i tried your solution but i receive this error message:

    -bash: $: command not found

    Why ?
    Thanks, Luigi

    • Kirk says:

      Sorry, you don’t type the $ – that’s the prompt you see on the command line.
      I tend to present commands like that, in my writing, since it makes it more
      clear what is the command and what is the output, if I show any.

  2. alec says:

    why not just make an image ready droplet?

    i’ve got a couple of dozen lurking around to do each of the manipulations i want.

    it is a bit top-heavy for a simple function but i find that adobe does the best job of jpeg and gif compression. byte for byte your graphics will look better.

  3. Anonymous says:

    You are missing another dash (-) before ‘resampleWidth.’ I couldn’t get it to
    work unless I used "sips –resampleWidth 120".

    • Kirk says:

      You’re right; sorry, I’ve fixed it in the text.

      One of those rare commands that uses double-dashes.

      • juandesant@mac.c says:

        Most "verbose" commands use — instead of -, that tends to be reserved for
        single letter abbreviations.

        And by the way, the text still uses a single dash…

  4. jhaffner says:

    Hi Kirk,

    In the article above you mention looking in Terminal for commands that
    might do what you want.

    How exactly did you go about doing so? I’m not afraid of the command line,
    but I often have trouble knowing about the available tools.

    Thanks.

    John


    Don’t worry, it’s out of control.

    • Kirk says:

      Well, "looking in Terminal" is not exactly correct. In this case, I looked for
      available tools, partly by using the whatis command, partly by searching on
      Mac OS X Hints.

      The whatis command is useful if you know what you want to do. For example,
      typing whatis copy returns a list of commands whose descriptions contain the
      word copy.

      The other way is to get a good book (hint, hint; see my forthcoming book on
      the command line, in the right-hand column? Up there, a bit higher) and
      learn the basic commands and what they do. Then you’ll find it easier to
      connect them to do more complex tasks.

  5. Anonymous says:

    You can use ImageWell to do more than just resize your images with just drag and drop. Btw, i had an article about sips some time ago as well. :)

  6. Anonymous says:

    For tasks like this I really enjoy HBImageProcessor
    [hogbatsoftware.com]. I also enjoy HogBay’s timer app, because like their
    image-massager, it seems to provide the most essential functionality in the
    smallest and simplest way.

  7. Ben Lavery says:

    Thanks for this Kirk, really interesting and useful article.

    For those wanting to find out more about sips, open up a Terminal window and type:
    man sips
    The `man` command displays the manual to a given command :)

Leave a Reply