是否有用于生成.ico文件的Python库?

Han*_*Gay 20 python favicon

我想用favicon.icoPython编程创建文件,但PIL只支持读取ico文件.

Ron*_*xão 25

你可以使用枕头:

from PIL import Image
filename = r'logo.png'
img = Image.open(filename)
img.save('logo.ico')
Run Code Online (Sandbox Code Playgroud)

(可选)您可以指定所需的图标大小:

icon_sizes = [(16,16), (32, 32), (48, 48), (64,64)]
img.save('logo.ico', sizes=icon_sizes)
Run Code Online (Sandbox Code Playgroud)

枕头文档说,在默认情况下它会产生大小 [(16, 16), (24, 24), (32, 32), (48, 48), (64, 64), (128, 128), (255, 255)]和任何规模大小比原来的大小或255将被忽略更大.

是的,它位于文档的只读部分,但它在某种程度上有效.

  • 这确实有效,前提是原始图像是正方形的。如果它不是正方形,您可以通过在此处应用接受的答案将其变为正方形:/sf/ask/3096184661/带-黑色/44231784 (2认同)

cod*_*ape 7

也许以下方法可行:

  • 使用PIL生成图标图像
  • 使用python接口将图像转换为.ico格式到ImageMagick,PythonMagick

我没有尝试过这种方法.ImageMagick转换命令行程序能够将.png文件转换为.ico格式,因此至少ImageMagick支持.ico格式.


Dou*_*der 7

根据维基百科,现代浏览器可以处理PNG格式的favicon,所以也许你可以生成它?

另外,ICO文章描述的格式......

  • 这是如何被接受的答案?下面是Ronan Paixao的更好的awser。 (2认同)

Jan*_*Jan 7

虽然这个问题相当老了,但它是使用 Python 将 PNG 文件转换为 ICO 的一个突出搜索结果,所以我想我会添加我的两分钱。

\n\n

如果您想要的只是一个网站图标,Douglas Leeder 的答案对我来说似乎完全没问题。如果您有一个高分辨率 PNG 文件的徽标并希望将其转换为 ICO 文件,Ronan Paix\xc3\xa3o 的答案可能是最简单的方法。

\n\n

但是一个 ICO 文件可以包含多个图像,用于不同的分辨率,我经常发现自己想要对这些不同的分辨率进行细粒度的控制,以避免不幸的抗锯齿效果,这意味着我想提供每个图像分辨率单独。这意味着我想将多个 PNG 文件而不是单个文件转换为单个 ICO 文件。据我所知,Pillow 包不提供此功能。幸运的是,现代 ICO 文件内部可以包含多个 PNG 文件,因此任务归结为编写一些标头条目的简单挑战。根据情况,我写了两个函数,第一个基本上是Ronan Paix\xc3\xa3o的解决方案,而第二个则提供了将多个PNG文件合并为一个ICO文件的功能:

\n\n
from pathlib import Path\nfrom PIL import Image\n\n\ndef bake_one_big_png_to_ico(sourcefile, targetfile, sizes=None):\n    """Converts one big PNG into one ICO file.\n\n    args:\n        sourcefile (str): Pathname of a PNG file.\n        targetfile (str): Pathname of the resulting ICO file.\n        sizes (list of int): Requested sizes of the resulting\n            icon file, defaults to [16, 32, 48].\n\n    Use this function if you have one big, square PNG file\n    and don\xe2\x80\x99t care about fine-tuning individual icon sizes.\n\n    Example::\n\n        sourcefile = "Path/to/high_resolution_logo_512x512.png"\n        targetfile = "Path/to/logo.ico"\n        sizes = [16, 24, 32, 48, 256]\n        bake_one_big_png_to_ico(sourcefile, targetfile, sizes)\n    """\n    if sizes is None:\n        sizes = [16, 32, 48]\n    icon_sizes = [(x, x) for x in sizes]\n    Image.open(sourcefile).save(targetfile, icon_sizes=icon_sizes)\n\n\ndef bake_several_pngs_to_ico(sourcefiles, targetfile):\n    """Converts several PNG files into one ICO file.\n\n    args:\n        sourcefiles (list of str): A list of pathnames of PNG files.\n        targetfile (str): Pathname of the resulting ICO file.\n\n    Use this function if you want to have fine-grained control over\n    the resulting icon file, providing each possible icon resolution\n    individually.\n\n    Example::\n\n        sourcefiles = [\n            "Path/to/logo_16x16.png",\n            "Path/to/logo_32x32.png",\n            "Path/to/logo_48x48.png"\n        ]\n        targetfile = "Path/to/logo.ico"\n        bake_several_pngs_to_ico(sourcefiles, targetfile)\n    """\n\n    # Write the global header\n    number_of_sources = len(sourcefiles)\n    data = bytes((0, 0, 1, 0, number_of_sources, 0))\n    offset = 6 + number_of_sources * 16\n\n    # Write the header entries for each individual image\n    for sourcefile in sourcefiles:\n        img = Image.open(sourcefile)\n        data += bytes((img.width, img.height, 0, 0, 1, 0, 32, 0, ))\n        bytesize = Path(sourcefile).stat().st_size\n        data += bytesize.to_bytes(4, byteorder="little")\n        data += offset.to_bytes(4, byteorder="little")\n        offset += bytesize\n\n    # Write the individual image data\n    for sourcefile in sourcefiles:\n        data += Path(sourcefile).read_bytes()\n\n    # Save the icon file\n    Path(targetfile).write_bytes(data)\n
Run Code Online (Sandbox Code Playgroud)\n\n

该代码假定您的 PNG 文件是每像素 32 位 RGBA 图像。否则,上面代码中的数字 32 就必须更改,并且应该用一些 Pillow-image-sniffing 来替换。

\n