Saturday, December 28, 2024
Google search engine
HomeLanguagesConvert PNG to ICO with Pillow in Python

Convert PNG to ICO with Pillow in Python

In this article, we will convert PNG to ICO using pillow in Python.

Convert PNG to ICO with Pillow in Python

Before moving further first let us understand what is PNG and ICO. The PNG stands for Portable Network Graphic. It is often used to store web graphics. The png image can be used with transparent background. Whereas, the ICO file format is an image file format used for the representation of a computer application icons in Microsoft Windows.

To Convert PNG to ICO we will use the Image module from the pillow library. The image module has an Image class, which is used to represent the PIL images, The image class has multiple functions for loading an image, saving an image, and processing the images.

Syntax: PIL.Image.open(fp, mode=’r’, formats=None)

Parameters:

  • fp = Name or path of the image file to be loaded.
  • mode = the mode in which the file is opened for read mode = r.
  • formats: It specifies the format of the file being loaded.

Returns: An Image object

PNG image used in this article.

 

Stepwise Implementations:

1. Install the pillow library using the following cmd.

pip install pillow2.

2. Load the png file which needs to be converted into an ICO file, using the open() method.

logo = Image.open(“C:\”File Path”\gfgLogo.png”)

3. Use the save method and set the format as ICO this will convert the image and save it in the given path,

ogo.save(“C:\”File Path”\gfgLogoIco.ico”,format=’ICO’)

Example 1: 

Converting PNG to ICO.

Python3




from PIL import Image
 
logo = Image.open("C:\\Users\\sai mohan \
pulamolu\\Desktop\\Lazyroar_dir\\gfgLogo.png")
 
logo.save("C:\\Users\\sai mohan pulamolu\\D\
esktop\\Lazyroar_dir\\gfgLogoIco.ico",format='ICO')


Output:

 

Example 2:

We can also specify the height and width of the ICO file using the sizes parameter ( sizes=[(40,40)] ).

Python3




from PIL import Image
 
logo = Image.open("C:\\Users\\sai mohan pula\
molu\\Desktop\\Lazyroar_dir\\gfgLogo.png")
 
logo.save("C:\\Users\\sai mohan pulamolu\\Des\
ktop\\Lazyroar_dir\\gfgLogoIco_40.ico", format='ICO',
          sizes=[(40, 40)])


Output:

 

RELATED ARTICLES

Most Popular

Recent Comments