2 min read

film + code: using imagemagick to skip the photoshop step

When I post to Instagram on my @r.ch3n account, I typically post an image that looks like this:

As you may or may not be able to see, there's a white border around the photo to make it fit within the constraints of Instagram. While I really like how Instagram has introduced support for other photo formats, I somehow still like the square.

So for a long time I've been using Adobe Photoshop CC to center the image into a blank square, then exporting it. This is fine, but it does take some time. Photoshop startup, as some of you may know, is no young colt and is more like Simba when I tell him to go up the stairs.

But screw Photoshop! I want something as fast as Simba coming to dinner! So I decided to wrangle some imagemagick to finangle my commands to create this mat.

Step 1. Install imagemagick

For the unaware: imagemagick is a command line utility that is able to do a lot of things to images. You can use it to crop, superimpose two images, and much more! But first we have to install it. These instructions are for Mac only, but you can find them for Windows if you do a quick google search (e.g. "imagemagick install Windows").

Install imagemagick using brew in the command line. If you don't have brew, that's not on me anymore.

brew install imagemagick

Step 2. Run the command!

When imagemagick installs, we can call it using the convert command. What we're going to do is run the following command that will create a white background for our image, resize our image to fit in this image, then export it to our folder.

convert -define jpeg:size=2000x2000 07302022_MOVING-5.jpg -thumbnail '2000x2000>' -gravity center  -crop 2200x2200+0+0\! -background white  -flatten square.jpg

Ok let's break this down. If you don't care about this just replace the first .jpg with your file and the last .jpg with the desired output filename.

  • convert is the imagemagick library we just installed.
  • -define jpeg:size=2000x2000 is what our resolution will end up being.
  • 07302022_MOVING-5.jpg is the image we want to convert into a square.
  • -thumbnail '2000x2000>' means we are creating a copy of the image that is size 2000 x 2000 only if the size of the image is larger than that.
  • -gravity center means we want this in the middle.
  • -crop 2200x2200+0+0!: adjusting the first two numbers in this gives us the size of the final white border. So if we want the border to be about 100px wide at the smallest point, we would but 2200 here.
  • -background white fills in the background of the image.
  • -flatten puts everything into one flat image
  • square.jpg is the final output file.