I'm looking to create favicon.ico
files programatically from Python, but PIL only has support for reading ico
files.
If you are designing an application for Windows, a website, or just want to personalize your Windows desktop, you can create ICO files using an image converter website, MS Paint, Mac Preview, or a Photoshop plugin.
Right-click on the image and select File->Open as Layers... and select all the other icon images. This will create a single image with a layer containing each of the other icon images. Save the image as a Microsoft Windows Icon (.
A favicon can actually be either a PNG, GIF, or ICO file. However, ICO files are typically used more than others as the file size is smaller and it is supported in all major browsers.
You can use Pillow:
from PIL import Image filename = r'logo.png' img = Image.open(filename) img.save('logo.ico')
Optionally, you may specify the icon sizes you want:
icon_sizes = [(16,16), (32, 32), (48, 48), (64,64)] img.save('logo.ico', sizes=icon_sizes)
The Pillow docs say that by default it will generate sizes [(16, 16), (24, 24), (32, 32), (48, 48), (64, 64), (128, 128), (255, 255)]
and any size bigger than the original size or 255 will be ignored.
Yes, it is in the Read-only section of the docs, but it works to some extent.
Perhaps the following would work:
I have not tried this approach. The ImageMagick convert command line program was able to convert a .png file to .ico format, so at least ImageMagick supports the .ico format.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With