Convert PNG to ICO with Pillow in Python

Convert PNG to ICO with Pillow in Python

Converting a PNG image to an ICO (icon) format using the Python library Pillow is a straightforward task. Here's a step-by-step guide on how to do it:

  1. Install Pillow: If you haven't already installed Pillow, you can do so using pip:

    pip install Pillow
    
  2. Python Script for Conversion: Use the following Python script to convert a PNG file to an ICO file:

    from PIL import Image
    
    # Load the PNG file
    png_file = 'path/to/your/image.png'  # Replace with your PNG file path
    img = Image.open(png_file)
    
    # Convert the image to ICO format
    # You can add different sizes as a list, e.g., [16, 32, 48, 64, 128, 256]
    ico_sizes = [(16, 16), (32, 32), (48, 48), (64, 64)]
    img.save('path/to/save/icon.ico', format='ICO', sizes=ico_sizes)
    

    In this script, replace 'path/to/your/image.png' with the path to your PNG file and 'path/to/save/icon.ico' with the path where you want to save the ICO file. The sizes parameter in the save method is optional, and it allows you to specify multiple sizes for the icon.

  3. Run the Script: Execute this script in your Python environment. It will read the specified PNG file and convert it into an ICO file with the specified sizes.

Remember, the ICO format is commonly used for favicons on websites and desktop icons on Windows systems. It can contain multiple images at different sizes, which is why the sizes parameter is useful for specifying the dimensions you need.


More Tags

android-styles counter android-facebook typescript picturebox logical-operators laravel-3 datetimeoffset cumulative-sum jsonparser

More Programming Guides

Other Guides

More Programming Examples