Multifile favicon.ico much bigger than sum of sizes of component files
Asked Answered
S

4

6

I want to create multifile favicon.ico according to great favicon cheat sheet.
I created 3 .png files, optimized them with OptiPNG and received files with 1, 2 and 3kb size.
Now I'm trying to create favicon.ico from them using Imagemagick but final file is around 15kb big (sum of component files is around 6kB).
What causes this effect and how i can avoid it ?

Signorelli answered 21/3, 2014 at 14:32 Comment(5)
I observe the same thing with ImageMagick 6.8.8-8 and icotool (icoutils) 0.29.1Joinder
I found another program that creates "good enough" results and posted is as answer but still don't know whats causes this effect.Signorelli
I have this question too. three 0.5k files become oe 15k file. Huh?Tarrsus
I have tried the same procedure with a third-party tool, icoFx, and I get the same increase, so I suppose it's just a question of format compressing or not.Tarrsus
This is a very old thread, but you can use the "-colors 256" parameter. Regards!.Ealasaid
J
2

A workaround is to rely on the HTTP gzip compression.

For example, I packed 3 PNG pictures (3580 bytes in total) in a 15086 bytes favicon.ico file. When I download it with gzip compression enabled on the server side, I get 2551 bytes of data. This is even more efficient than downloading the PNG pictures one by one, as gzip is usually not enabled for this kind of file.

However, gzip is often not configured for .ico files (this is more for text files). On Apache, this can be fixed by adding:

<IfModule mod_deflate.c>
    (... other rules here...)
    AddOutputFilterByType DEFLATE image/x-icon
</IfModule>

in an Apache configuration file, such as /etc/apache2/mods-available/deflate.conf.

This is not the answer you expected and I hope someone will come with a magic command line to produce the lightweight favicon.ico we deserve!

Joinder answered 23/3, 2014 at 9:54 Comment(0)
S
1

Another workaround is using other another tool to create favicon file.
I found (on stack ofc ;)) png2ico tool. It gave me 8kb file (sum of components is around 6) which is size I can live with.

Please read comments to this answer - philippe_b observed that sometimes png2ico gives him poor results.

Signorelli answered 23/3, 2014 at 17:30 Comment(2)
Although I observed the same gain in size, png2ico is not conservative. The embedded pictures are not identical to the original ones. I didn't review the code to find out what it does, but my feeling is that it removes the transparency. Which will often produce poor results.Joinder
Could you please edit your answer to indicate that png2ico sometimes produce poor results? People might consider it as a surefire tool whereas I clearly had issues with it.Joinder
T
1

You can combine PNGs into an ICO through a simple Python script:

png2ico.py 1.png 2.png -o out.ico

import argparse
import struct
import io
import PIL.Image


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument('inputs', nargs='+')
    ap.add_argument('-o', '--output', required=True)
    args = ap.parse_args()

    pngs = []
    for path in args.inputs:
        with open(path, 'rb') as fp:
            pngs.append(fp.read())

    headers = []
    headers.append(struct.pack('<HHH', 0, 1, len(pngs)))
    offset = 6 + 16 * len(pngs)
    for data in pngs:
        with PIL.Image.open(io.BytesIO(data)) as im:
            w, h = im.size
        headers.append(struct.pack('<BBBBHHII', w, h, 0, 0, 1, 0, len(data), offset))
        offset += len(data)

    with open(args.output, 'wb') as fp:
        fp.truncate()
        fp.write(b''.join(headers + pngs))


main()
Trencherman answered 2/9, 2023 at 0:14 Comment(1)
Thanks! The ICO format is indeed this simple - see en.wikipedia.org/wiki/ICO_(file_format) - and fully PNG-based ICO files are supported by any browser or any modern Windows (since Vista).Audreyaudri
S
0

Only the 256x256 32bit icon is PNG compressed, others are stored as classic icons in .ICO file. So, there's a logical increase in filesize since it will decompress the other smaller size icons.

Sycamine answered 12/4, 2016 at 22:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.