Simple image modifications using Imagemagick in the shell

During the We Are Developers Conference in Vienna this year, I heard an interesting talk about “Habits of Efficient Developers”, held by Daniel Lebrero (the slides from this talk can be found on his website).

One of the key take-aways for me was that efficient developers automate all the things… well, at least the tasks they do over and over again.
Of course, I also have tasks that come up in nearly every project, so I have been thinking about a way to automate those tasks.

One of the first tasks that I chose to enhance was simple image modifications. I often need to do minor adjustments to images and don’t want to fire up Photoshop for that. If it’s just resizing, I’ll simply use the Preview app on Mac OS X, but placing a rectangular image on a square background is something that the Preview app can’t do.

So I came up with the idea to use a command line tool for this task – Imagemagick.

I installed Imagemagick with the help of Homebrew:
brew install imagemagick

Next, I needed a command for placing my image onto a square background, StackOverflow to the rescue!

magick convert -background white -gravity center /Users/stjagl/Desktop/originalfile.png -extent 500x500 /Users/stjagl/Desktop/squarefile.png

 

That’s it. In the timespan that would be needed to start up Photoshop, my image convertion will already be done.