How can I increase the canvas size (add white borders) to various images to make them square?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
dracho
Posts: 3
Joined: 2016-07-24T07:01:59-07:00
Authentication code: 1151

How can I increase the canvas size (add white borders) to various images to make them square?

Post by dracho »

I have a huge number of images of all different sizes and dimensions. I want to keep the quality the same, and add white padding to either the left and right or top and bottom, to make them all square. There should be no cropping performed at all.

If necessary, I could manually organize the images into two folders: tall images and wide images... but I'd rather not, if possible.

I'm running Windows, if that matters.

Again, the images are various sizes... for example, some might be 100x400, some 400x100, some 200x300, etc. Every program I've looked into so far requires me to specify the output dimensions. This is not possible in my case.

Thanks very much.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How can I increase the canvas size (add white borders) to various images to make them square?

Post by snibgo »

I would do it in a "for" loop that loops through the images. For each one, find the maximum of width and height, then "-extent" to that number in both directions.

In v6, each image need two commands: first to get the number, then to extend the image. In v7, this can probably be done in a single command.
snibgo's IM pages: im.snibgo.com
dracho
Posts: 3
Joined: 2016-07-24T07:01:59-07:00
Authentication code: 1151

Re: How can I increase the canvas size (add white borders) to various images to make them square?

Post by dracho »

Thanks. Unfortunately, I've never used ImageMagick before. It sounds like it's fairly simple... Is there any way you (or someone) could come up with the command? I'd appreciate it very much.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How can I increase the canvas size (add white borders) to various images to make them square?

Post by snibgo »

This processes files named "x*.png". It pads with blue instead of white because it's easier to see. It writes files to a different directory. It assumes %IM% is blank or points to the directory that contains convert.exe. Input images that contain transparency will be flattened against blue.

Windows BAT syntax. Written for IM v6, but should also work with v7.

Code: Select all

setlocal enabledelayedexpansion

md outdir

for %%F in (x*.png) do (

  for /F "usebackq" %%L in (`%IM%identify ^
  -format "DIM=%%[fx:max(w,h)]" ^
  %%F`) do set %%L

  echo %%F !DIM!

  %IM%convert %%F +repage -background Blue -gravity center -extent !DIM!x!DIM! outdir\%%F

)
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: How can I increase the canvas size (add white borders) to various images to make them square?

Post by GeeMack »

dracho wrote:Thanks. Unfortunately, I've never used ImageMagick before. It sounds like it's fairly simple... Is there any way you (or someone) could come up with the command? I'd appreciate it very much.
Using ImageMagick 7 (I'm running it on Windows 10 64) you can do something like this from the command line...

Code: Select all

magick *.jpg -set filename:f "%[t]_sq" -background white -gravity center ^
   -extent %[fx:max(w,h)]x%[fx:max(w,h)] -quality 100 "%[filename:f].jpg"
That would bring in all the JPGs in the directory and create output images padded with white as necessary to make squares.

The results would be in the same directory but with their original filenames changed to add a "_sq" just before the extension, like "myimage1_sq.jpg" and "thisdog_sq.jpg" and so on.

The "-quality 100" would maintain the best possible quality images, but of course there is always some amount of loss with JPGs due to compression.

Using the same command in a BAT file would require changing every percent sign "%" into a double "%%".
dracho
Posts: 3
Joined: 2016-07-24T07:01:59-07:00
Authentication code: 1151

Re: How can I increase the canvas size (add white borders) to various images to make them square?

Post by dracho »

GeeMack wrote:
dracho wrote:Thanks. Unfortunately, I've never used ImageMagick before. It sounds like it's fairly simple... Is there any way you (or someone) could come up with the command? I'd appreciate it very much.
Using ImageMagick 7 (I'm running it on Windows 10 64) you can do something like this from the command line...

Code: Select all

magick *.jpg -set filename:f "%[t]_sq" -background white -gravity center ^
   -extent %[fx:max(w,h)]x%[fx:max(w,h)] -quality 100 "%[filename:f].jpg"
That would bring in all the JPGs in the directory and create output images padded with white as necessary to make squares.

The results would be in the same directory but with their original filenames changed to add a "_sq" just before the extension, like "myimage1_sq.jpg" and "thisdog_sq.jpg" and so on.

The "-quality 100" would maintain the best possible quality images, but of course there is always some amount of loss with JPGs due to compression.

Using the same command in a BAT file would require changing every percent sign "%" into a double "%%".
Thanks. This does work, however I'm trying to modify it so the original images are automatically deleted / overwritten. I'm currently just resizing my Explorer window to have only two thumbnails per row and then deleting a whole column (the originals.)

I copied all the files to new directories, so I don't need to preserve the originals. I've played around with the code, but I keep getting errors, usually "magick: MissingArgument `-quality' at CLI arg 8 @ fatal/magick-cli.c/ProcessCommandOptions/444."

How can I fix this?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How can I increase the canvas size (add white borders) to various images to make them square?

Post by snibgo »

dracho wrote:I've played around with the code, but I keep getting errors, usually "magick: MissingArgument `-quality' at CLI arg 8 @ fatal/magick-cli.c/ProcessCommandOptions/444."
If you showed us the code that produced that error, perhaps we could help.
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: How can I increase the canvas size (add white borders) to various images to make them square?

Post by GeeMack »

dracho wrote:This does work, however I'm trying to modify it so the original images are automatically deleted / overwritten.
The command I posted sets the filename for each output file with this...

Code: Select all

... -set filename:f "%[t]_sq" ...
Leave the "_sq" out of there and it will make each output file the same name as its input file. That will of course overwrite the originals, so use caution.
Post Reply