How do I embed multiple sizes in an .ico file?

17

14

I have seen .ico icons that have different images for multiple sizes, eg. 16x16, 32x32, 48x48, 128x128, 256x256. On Windows, how can I create an icon file that supports multiple sizes in this way, given I have existing .ico files for each size? Please note that I'm hoping to do this to 200+ files, so doing it from cmd would be ideal.

Suchipi

Posted 2012-10-22T16:24:46.870

Reputation: 854

Answers

29

ImageMagick (Windows/Mac/Linux) contains a command-line tool called convert that can be used for many things, including packing multiple images in one icon:

convert 16.png 32.png 48.png 128.png 256.png -colors 256 icon.ico

The previous command takes 5 PNG images, and combines them into a single .ico file.

Unlike the other answers, this method can easily be used in batch scripts to automatically generate several icon files. In one of my projects, I have a single vector image (SVG), and use Inkscape to generate png's of various sizes, followed by convert to create a icon container. This is a reduced example (in a bash script):

#!/bin/bash
for size in 16 32 48 128 256; do
    inkscape -z -e $size.png -w $size -h $size icon.svg >/dev/null 2>/dev/null
done
convert 16.png 32.png 48.png 128.png 256.png -colors 256 icon.ico

Rob W

Posted 2012-10-22T16:24:46.870

Reputation: 1 473

How do you do the opposite? Convert a .ico into multiple PNGs, based on the sizes it contains? – Flimm – 2014-07-09T09:38:19.500

2@Flimm convert favicon.ico favicon.png generates favicon-0.png, favicon-1.png, etc. for every icon in the frame. If you want to know the dimensions of the image, use the identify command on the png file. – Rob W – 2014-07-09T09:41:29.407

You can use imagemagicks own convert <icon.png> -resize 64x64 instead of inkscape – kumarharsh – 2016-02-17T06:03:58.730

I added -dither None to the convert command because the default conversion to 256 colors added some noise to my icon – James Roth – 2017-11-19T00:07:45.483

@RobW I tried this and have been having problems with setting the small icon. When I check it still displays the default icon. – Kennichi Nitta – 2020-01-02T02:06:15.373

1Man, I feel like I learn a new way to use ImageMagick's convert every time I look into it. Thanks for the answer! – Suchipi – 2013-12-02T23:33:18.030

7

Better command for ImageMagick:

convert in.jpg -define icon:auto-resize=16,48,256 -compress zip out.ico

user400747

Posted 2012-10-22T16:24:46.870

Reputation:

1This is a great answer actually. Use this command to test that the file has actually converted: identify out.ico – kumarharsh – 2016-02-17T06:04:52.850

The bitmapped pngs exported from Inkscape individually will have better clarity than the resized initial png used in this example. – UuDdLrLrSs – 2020-01-31T17:53:34.493

5

You can do this for free in GIMP. There are brief instructions for doing this here.

To quote:

  1. Open your image in Gimp
  2. Make your canvas square
  3. Resize your layer to the image
  4. Scale the layer to the largest size in your .ico file like 64 pixels
  5. Duplicate the layer
  6. Scale the duplicate layer to the next size
  7. Keep duplicating / scaling for all the sizes you want in your .ico file
  8. Save as .ico

In your case, you could either start with the largest image and scale down for each duplicated image, or you could just add new layers and import the specific icon images you wanted into that layer.

techturtle

Posted 2012-10-22T16:24:46.870

Reputation: 8 059

You have to choose to export in Gimp 2.8.16 but years later, I can confim there is a dialog for color settings when you export the ico. – Broots Waymb – 2016-07-07T18:01:42.397

How do you support multiple color depths by this method? – Random832 – 2012-10-22T17:43:56.443

Can't say that I've had to do that personally, but this post says GIMP should pop up a color settings dialog for each layer upon saving.

– techturtle – 2012-10-22T17:50:35.937

1

Here’s the accepted answer by Rob W, with a trivial adaptation to avoid having to type the sizes (16, 32, etc.) more than once:

#!/bin/bash
files=()
for size in 16 32 48 128 256; do
    inkscape -z -e "$size.png" -w "$size" -h "$size" logo.svg > /dev/null 2> /dev/null
    files+=("$size.png")
done
convert "${files[@]}" -colors 256 favicon.ico
unlink  "${files[@]}"

Here logo.svg represents the input (source) image, from which we create smaller files of the desired sizes (16.png, 32.png, etc.) which we then combine into the output (result) icon file, favicon.ico.  You can change the list of sizes in line 3, e.g., to "16 24 32 48 64 72 128", and the convert command will automatically adapt accordingly, because this script uses the technique described by G-Man in his answer here to build an array of filenames.  And finally we unlink (remove) the PNG files created in line 4, using the array of filenames again.


I’ve noticed that the command:

convert logo.svg -define icon:auto-resize=16,48,256 -compress zip favicon.ico

(equivalent to the one presented in user400747’s answer) actually scaled bitmap image (lost quality) and layers background lost transparency.

Andris Petrams

Posted 2012-10-22T16:24:46.870

Reputation: 11

1(1) You have essentially copied somebody else’s answer.   If you did that and nothing more (yes, people sometimes do that), I would be flagging your answer for deletion.   I’m not doing that, because you made a tiny improvement.   But, whenever you copy somebody else’s work, you should acknowledge the original author by name and by linking to the information you’re quoting.  (2) Please don’t say things like “this is more flexible” without explaining what you mean.  … (Cont’d) – Scott – 2018-10-12T20:35:00.417

(Cont’d) …  (3) You should always quote shell variables unless you have a good reason not to, and you’re sure you know what you’re doing. Actually, of all the examples I’ve seen of scripts that use shell variables without quoting them, yours (i.e., Rob W’s) may be among the least bad. But still, it’s better to quote the variables.  (4) Why do you use unlink instead of the more common rm?

– Scott – 2018-10-12T20:35:02.763

1

Windows batch file, which creates multiple sized .PNGs and merge them to one .ICO file:

@echo off

set inkScape="C:\SOFTWARE\GRAPHIC\INKSCAPE\inkscape.exe"
set imageMagick="C:\SOFTWARE\DEVELOPER\IMAGEMAGICK\magick.exe"
set fileName=favicon
set importType=svg
set exportType=png
set exportDpi=300
set imageSizes=(16 24 32 48 57 60 64 70 72 76 96 114 120 128 144 150 152 180 192 196 256 300 320 400 450 460 480 512 600)

for %%s in %imageSizes% do (
 %inkScape% -z -f %~dp0%fileName%.%importType% -w %%s -h %%s -e %~dp0%fileName%-%%sx%%s.%exportType% -d %exportDpi%
 echo CREATED: %fileName%-%%sx%%s.%exportType%
 set e=%fileName%-%%sx%%s.%exportType%
 call :concat (e)
)

%imageMagick% %exportFileNames%"%~dp0%fileName%.ico"
echo MERGED IN: %fileName%.ico

pause goto :eof


:concat (e) (
 set exportFileNames=%exportFileNames%"%~dp0%e%" 
)

If you don't need the .PNG files, you can delete (or remove) them by del FILE or you save all PNGs inside a directory you can remove (after %imageMagick% %exportFileNames%"%~dp0%fileName%.ico").

Hope it helps somebody. :)

Froschkoenig84

Posted 2012-10-22T16:24:46.870

Reputation: 11

0

It is my opinion that Axialis IconMaker is the best solution to icon problems. There's a 30 day trial that will probably solve the problem for you.

I have used Axialis for so many years and on so many projects, I can attest that it is a really worthwhile product. You won't need 30 days! Ha!

Jerry Nixon

Posted 2012-10-22T16:24:46.870

Reputation: 1 228

0

You must use a third-party icon-editing program because MSPaint only supports a single icon per file. There are a couple of threads here with recommendations for icon editors, some free, some commercial.

Once you settle on an icon editor, the method of adding icon formats will vary but generally be similar (you click a button or select a menu item to add a new format). Most programs will let you import an icon file when adding an icon format/size, but most also let you create a new format/size from the existing one by re-sizing it.

If you choose to use the create-from-existing option when adding a new format/size, make sure to create them from the largest icon format you have already available since it will have the most data for the re-sizing algorithm to work with. Also, make sure to use a version with transparency when creating an XP/Vista icon since most programs are not great at creating the alpha channel from scratch.

Synetech

Posted 2012-10-22T16:24:46.870

Reputation: 63 242

I need to do this to over 200 icons. Can any of those icon editors run things in batch? – Suchipi – 2012-10-22T20:08:48.543

1I don’t recall ever seening an icon editor with batch mode. You could try using an image editor in batch mode, but I’m not sure if any image-editors have multi-format icon support (at least not from the command-line/batch-mode). If any do, it would be ImageMagick. Also, you should add the volume and batch-mode requirement to the question. – Synetech – 2012-10-22T20:14:35.043

0

You might try Matthias Benkmann's png2ico. It is free and can pack multi size png's into a single ico file.

Dimitri C.

Posted 2012-10-22T16:24:46.870

Reputation: 2 232

It has some interesting restrictions, though: Width must be multiple of 8 and <256. Height must be <256. – Francois Botha – 2018-02-27T15:14:54.237