使用ImageMagick创建Windows ICO文件的配方?

时间:2012-07-10 23:59:09

标签: icons imagemagick

我想使用ImageMagick动态地(从SVG文件)为我的Windows应用程序创建.ico图标。我该怎么做?

Microsoft lists various color depth and size requirements用于图标。 ImageMagick有-depth-colors选项,但在这种情况下我不确定如何正确使用它们。

另外,它看起来像Vista+ supports 256x256 hi-res icon嵌入到同一个.ico中,它可以(应该?必须?)是一个压缩的PNG。如何将Windows XP图标和此新Vista图标“加入”单个.ico文件?

9 个答案:

答案 0 :(得分:27)

ImageMagick在其文档中有相关的配方,请参阅FavIcon Web Page Link Thumbnail

基本上你运行以下内容:

convert image.png  -bordercolor white -border 0 \
          \( -clone 0 -resize 16x16 \) \
          \( -clone 0 -resize 32x32 \) \
          \( -clone 0 -resize 48x48 \) \
          \( -clone 0 -resize 64x64 \) \
          -delete 0 -alpha off -colors 256 favicon.ico

您可以根据需要修改此选项以包含更大的分辨率,并更改边框,透明度设置等内容。

答案 1 :(得分:8)

看起来ImageMagick似乎不能单独执行此操作,因为它不能以理智的方式处理SVG大小调整(而是仅在栅格化之后调整SVG大小会产生可怕的结果)

通过使用inkscape进行转换,它似乎是可能的,例如以下一个班轮应该为您提供一个包含所有图标大小的可用图标:

mkdir temp; declare -a res=(16 24 32 48 64 128 256); for f in *.svg; do for r in "${res[@]}"; do inkscape -z -e temp/${f}${r}.png -w $r -h $r $f; done; resm=( "${res[@]/#/temp/$f}" ); resm=( "${resm[@]/%/.png}" ); convert "${resm[@]}" ${f%%.*}.ico; done; rm -rf temp;

上面不会给你文件中的8位和4位图标(我认为这些仅适用于不再支持的旧版Windows) 如果你需要的话,应该可以做更多的工作。

答案 2 :(得分:6)

我清理了Malcolm的解决方案,修复了一个错误,并使脚本输出tiffs,这样你就可以在osx中​​运行tiff2icns了。

#! /bin/bash
# converts the passed-in svgs to tiff and ico

if [[ $# -eq 0 ]]; then
    echo "Usage: $0 svg1 [svg2 [...]]"
    exit 0
fi

temp=$(mktemp -d)
declare -a res=(16 24 32 48 64 128 256 512)
for f in $*; do
    mkdir -p $temp/$(dirname $f)
    for r in "${res[@]}"; do
        inkscape -z -e $temp/${f}${r}.png -w $r -h $r $f
    done
    resm=( "${res[@]/#/$temp/$f}" )
    resm=( "${resm[@]/%/.png}" )
    for filetype in ico tiff; do
        convert "${resm[@]}" ${f%%.*}.$filetype
    done
done
rm -rf $temp

答案 3 :(得分:6)

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

http://imagemagick.org/script/command-line-options.php#define

答案 4 :(得分:4)

以下是来自FAQ的标准配方,已修改为具有msdn link中提到的所有分辨率(“附加尺寸...”下的那些除外)(另一个答案并非全部希望的决议)

 convert input.png -bordercolor white -border 0 ( -clone 0 -resize 16x16 ) ( -clone 0 -resize 24x24 ) ( -clone 0 -resize 32x32 ) ( -clone 0 -resize 40x40 ) ( -clone 0 -resize 48x48 ) ( -clone 0 -resize 64x64 ) ( -clone 0 -resize 256x256 ) -delete 0 -alpha off -colors 256 output.ico

答案 5 :(得分:2)

Bash one-liner将logo.svg转换为logo.ico,使用Inkscape导出各种尺寸的png图片:

eval convert \
  '<(inkscape -e /dev/stderr logo.svg -w '{16,24,32,48,64,128,256}' 2>&1 > /dev/null)' \
  logo.ico

Malcolm MacLeod's answer的启发,但避免使用显式循环和临时文件。

stderr和重定向是为了避免Inkscape在stdout上的成功消息(“Bitmap保存为:/ dev / stdout”)以图像数据结束。

答案 6 :(得分:2)

以先前的所有答案为基础,并纠正以下错误:

  • 请勿使用 func itemsDownloaded(items: NSArray) { feedTrees = items self.treeTableView.reloadData() } override func viewDidLoad() { super.viewDidLoad() //table view delegats and datasource declaration self.treeTableView.delegate = self self.treeTableView.dataSource = self let treeModel = TreeModel() treeModel.delegate = self treeModel.downloadItems() } ,因为您需要使用现代Windows版本(Vista +)的所有尺寸的32位彩色版本
  • Windows中必需的大小为16, 24 ,32, 48 ,64、128、256。大多数脚本都忘记了这些大小。我不确定是否真的需要96,但这并没有伤害。
  • 您需要包括大小为16、24、32和48的4位和8位调色板版本(显然是为了特别支持远程桌面应用程序)

一站式bash脚本(从-color=256开始并生成logo.svg):

logo.ico

答案 7 :(得分:1)

为Windows用户修改hnasarat的答案。最简单的方法是使用Chocolatey安装InkScape和ImageMagick,然后在批处理文件中运行以下命令。 (它不像你在一个svg中传递的其他答案那样灵活,但是它会推出Favicon Cheat Sheet中建议的所有favicons。

@ECHO off

IF "%1"=="" (
    ECHO You must provide an svg file
    EXIT /b
) 

IF NOT EXIST favicons MD favicons

SET "sizes=16 24 32 48 57 64 72 96 120 128 144 152 195 228 256 512"

FOR %%s IN (%sizes%) DO (
    inkscape -z -e favicons/favicon-%%s.png -w %%s -h %%s %1
)

convert favicons/favicon-16.png favicons/favicon-24.png favicons/favicon-32.png favicons/favicon-48.png favicons/favicon-64.png favicons/favicon.ico

答案 8 :(得分:0)

要通过SVG创建ICO文件,同时保持宽高比:

  1. 寻找SVG比例(例如1920x1080)
  2. 对于最大256像素宽的图标,请执行以下比例:[1920:1080 = 256:x]-> x =(1080 * 256)/ 1920 = 144
  3. 最后,使用ImageMagick convert命令:

    转换-background none-调整大小256x144-重心-范围256x144 image.svg image.ico

相关问题