Convert PNG to ICO with Pillow for Python Beginners – Python Tutorial

By | August 30, 2020

There exist many online png to ico convertors, however, this functionality is very easy in python. In this tutorial, we will introduce how to conver a png file to a ico file with python pillow library, you can learn and do step by step.

python pillow logo

Install Pillow

pip install Pillow

Load pillow library

from PIL import Image

Open a png image file

filename = r'logo.png'
img = Image.open(filename)

Convert png to ico file with pillow

img.save('logo16.ico',format = 'ICO', sizes=[(32,32)])

To use Image.save() to convert png to ico, you must notice:

Image.save(fname, format=None, **params)

where:

fname: it is the name of new image file

format: such as JPEG, ICO. You can know more formats in here.

https://pillow.readthedocs.io/en/3.1.x/handbook/image-file-formats.html

sizes: if the image is ico, the (width, height) size may be [(16, 16), (24, 24), (32, 32), (48, 48), (64, 64), (128, 128), (255, 255)]

Leave a Reply

Your email address will not be published. Required fields are marked *