Under Windows, using Batch-Script or Powershell, with ImageMagick, I just would like to create a proper icon file like the icons that you can create with ToyCon or QuickAnyToIco programs, that is:
That is a simple task that any conventional icon converter software for Windows will do perfectly.
Now, using ImageMagick, the icon:auto-resize
argument it is not of any help, because it stretches the image to fit width and height.
I've read ImageMagick's documentation and it has these two modifiers:
I noticed they can be specified in both the width and height values specified in both -resize
and -extent
arguments.
And this:
...for -extent
argument.
But I can't find the way to use or combine them properly to center the image by shrinking and enlarging - preserving aspect ratio - automatically to fit width or height. Just like QuickAnyToIco program does, I insist.
This is a sample command that I try to create a 256x256 png layer for the icon:
Input image size: 513x458
Command is this:
Magick.exe convert "image.jpg" -resize "256>x256" -background white -gravity center -extent "256^x256^" -quality 100 "output.png"
And the resulting output image is this:
OUTPUT IS OK !!, that is the same result as produced by any good icon converter software.
Now, using the same command with an image size of 208x242, I get this resulting output image:
OUTPUT IS WRONG !!. Image is centered but top and bottom edges does not fit.
I expect this resulting image (produced by any good icon converter software):
And I can achieve it using this command:
Magick.exe convert "cover.jpg" -resize "256<x256" -background white -gravity center -extent "256^x256^" -quality 100 "output.png"
But it only works for smaller images like that one. That same command will produce this other image:
NOTES:
The white background is just a convenience color that I chose for better visual presentation of the problem.
Original input images used in the examples above:
https://i.imgur.com/wiXntuD.jpg
https://i.imgur.com/QaWV1Oe.jpg
With ImageMagick v7 on Windows 11, I use this command to make icons from nearly any input image...
magick image.jpg -gravity center -extent 1:1# ^
-define icon:auto-resize="256,128,64,48,32,24,16" result.ico
It starts with "-extent 1:1#" to extend the input image's canvas to a 1:1 ratio. The "#" flag tells it to add canvas as needed to achieve the 1:1 ratio.
The command continues with that square image, and using IM's icon sizing define, it will output an icon containing the sizes specified.