Page 1 of 1
How can I increase the canvas size (add white borders) to various images to make them square?
Posted: 2016-07-24T07:09:11-07:00
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.
Re: How can I increase the canvas size (add white borders) to various images to make them square?
Posted: 2016-07-24T08:27:46-07:00
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.
Re: How can I increase the canvas size (add white borders) to various images to make them square?
Posted: 2016-07-24T09:55:17-07:00
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.
Re: How can I increase the canvas size (add white borders) to various images to make them square?
Posted: 2016-07-24T10:17:01-07:00
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
)
Re: How can I increase the canvas size (add white borders) to various images to make them square?
Posted: 2016-07-25T17:30:34-07:00
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 "%%".
Re: How can I increase the canvas size (add white borders) to various images to make them square?
Posted: 2016-07-31T15:10:13-07:00
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?
Re: How can I increase the canvas size (add white borders) to various images to make them square?
Posted: 2016-07-31T15:47:25-07:00
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.
Re: How can I increase the canvas size (add white borders) to various images to make them square?
Posted: 2016-07-31T17:04:07-07:00
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...
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.