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.
How can I increase the canvas size (add white borders) to various images to make them square?
-
- 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?
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.
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
Re: How can I increase the canvas size (add white borders) to various images to make them square?
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.
-
- 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?
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.
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
- 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?
Using ImageMagick 7 (I'm running it on Windows 10 64) you can do something like this from the command line...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.
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"
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?
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.)GeeMack wrote:Using ImageMagick 7 (I'm running it on Windows 10 64) you can do something like this from the command line...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.
That would bring in all the JPGs in the directory and create output images padded with white as necessary to make squares.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"
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 "%%".
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?
-
- 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?
If you showed us the code that produced that error, perhaps we could help.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."
snibgo's IM pages: im.snibgo.com
- 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?
The command I posted sets the filename for each output file with this...dracho wrote:This does work, however I'm trying to modify it so the original images are automatically deleted / overwritten.
Code: Select all
... -set filename:f "%[t]_sq" ...