giwook
giwook

Reputation: 580

Using ImageMagick to add whitespace to images in bulk

I have a bunch of images that are rectangular and I need them to be square, i.e. 1000x1000, so I've decided to use ImageMagick to pad the sides of the image with the amount of whitespace necessary to make the image square.

I've found the code from the following link useful (see next paragraph). But this requires me to process each image individually, to set the filename and then the -thumbnail dimensions. How can I use this same process to process an entire folder?

Here's the code and the link to it:

http://imagemagick.org/Usage/thumbnails/#pad

convert -define jpeg:size=200x200 hatching_orig.jpg -thumbnail '100x100>' \
-background white -gravity center -extent 100x100 pad_extent.gif

Upvotes: 5

Views: 3614

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207465

It is not very clear from your question what you actually have (i.e. how big your images are? what format are they?), nor what you actually want (you only provide an example that doesn't do what you want. But I'll take your question at the face-vale of the title and hope you can work out anything missing from there.

If you want to do ImageMagick in bulk, you either need to use a loop in bash, like this:

#!/bin/bash
for f in *.jpg; do
    convert "%f" ...
done

or use IM's mogrify command. That is quite a powerful command, so make a backup of your images before you even think of trying to use it.

Let's assume you want to place all JPEG images in the current directory onto a white 1000x1000 pixel background. You would do this:

mogrify -background white -gravity center -extent 1000x1000 *.jpg

Updated Answer

It should be possible to do the resizing and the white padding in a single ImageMagick command without needing to use SIPS as well, like this if you now want 1200x1200 square images padded with white:

mogrify -background white -gravity center -resize 1200x1200\> -extent 1200x1200 *.jpg

Upvotes: 8

Related Questions