Search code examples
windowspowershellbatch-fileiconsimagemagick

How to properly center an image with ImageMagick to make an icon?


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:

  • An icon whose image layer is centered in the canvas
  • It fits vertical or horizontal edges automatically, that is, the image gets shrinked or enlarged automatically.
  • Preserving aspect ratio
  • With transparent background
  • The icon real size is 256x256px (containing inside the icon other various arbitrary layers of typical sizes: 128x128, 64x64, 32x32, 16x16 etc)

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:

enter image description here

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:

enter image description here

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):

enter image description here

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:

enter image description here


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
    

Solution

  • 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.