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将被忽略更大.
是的,它位于文档的只读部分,但它在某种程度上有效.
也许以下方法可行:
我没有尝试过这种方法.ImageMagick转换命令行程序能够将.png文件转换为.ico格式,因此至少ImageMagick支持.ico格式.
虽然这个问题相当老了,但它是使用 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\nfrom 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 归档时间: |
|
查看次数: |
10604 次 |
最近记录: |