简体   繁体   English

如何在 imagemagick 或其他上压缩 ico 文件

[英]How to compress ico file on imagemagick or other

I created a 32x32 png in photoshop exported as 500bytes.我在 Photoshop 中创建了一个 32x32 png,导出为 500 字节。

网站图标

Converted to ico using使用转换为 ico

magick convert .\favicon.png favicon.ico

And it became 5kb.它变成了5kb。
Question?问题? Is there there a compression flag in imagemagick or anoter way to compress favicon.ico? imagemagick 中是否有压缩标志或另一种压缩 favicon.ico 的方法?

I just tried @dan-mašek's suggestion and it definitely works better than ImageMagick.我刚刚尝试了@dan-mašek 的建议,它肯定比 ImageMagick 更好。

With the PNGs I was working with, ImageMagick gave me a 5.4K .ico file despite asking for it to use .ico 's support for embedded PNGs for the compression (apparently it ignores you for sizes under 256x256) while Pillow got it down to 1.8K.对于我正在使用的 PNG,ImageMagick 给了我一个 5.4K 的.ico文件,尽管它要求它使用.ico对嵌入式 PNG 的支持进行压缩(显然它忽略了你的尺寸低于 256x256),而 Pillow 把它归结为1.8K。

Here's how I went about crunching down my favicons based on an existing PNG-optimizing shell script I cooked up years ago:以下是我如何根据我多年前编写的现有 PNG 优化 shell 脚本来处理我的网站图标:

#!/bin/sh

optimize_png() {
    for X in "$@"; do
        echo "---- Using pngcrush to strip irrelevant chunks from $X ----"
        # Because I don't know if OptiPNG considers them all "metadata"
        pngcrush -ow -q -rem alla -rem cHRM -rem gAMA -rem iCCP -rem sRGB \
            -rem time "$X" | egrep -v '^[ \|]\|'
    done

    echo "---- Using OptiPNG to optimize delta filters ----"
    # ...and strip all "metadata"
    optipng -clobber -o7 -zm1-9 -strip all -- "$@" 2>&1 | grep -v "IDAT size ="

    echo "---- Using AdvanceCOMP to zopfli-optimize DEFLATE ----"
    advpng -z4 "$@"
}

optimize_png 16.png 32.png

python3 << EOF
from PIL import Image

i16 = Image.open('16.png')
i32 = Image.open('32.png')
i32.save('src/favicon.ico', sizes=[(16, 16), (32, 32)], append_images=[i16])
EOF

Just be aware that:请注意:

  1. pngcrush and advpng don't take -- as arguments, so you have to prefix ./ onto relative paths which might start with - . pngcrushadvpng不采用--作为 arguments,因此您必须将./前缀到可能以-开头的相对路径上。
  2. .save in PIL must be called on the largest image so, if you have a dynamic list of images, you probably want something like this: PIL 中的.save必须在最大的图像上调用,所以,如果你有一个动态的图像列表,你可能想要这样的东西:
images.sort(key=lambda x: x.size)
images[-1].save('favicon.ico', sizes=[x.size for x in images], append_images=images)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM