[Python] How can I save the 32x32 icon from a .ico file that has the highest color depth...

Discussão em 'Python' iniciado por Stack, Fevereiro 16, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to extract and save the 32x32 icon from a .ico file that contains multiple icons with multiple sizes and color depths using Pillow. The 32x32 icon is available in the following color depths: 32-bit, 8-bit and 4-bit.

    I tried opening the icon file using Image.open(), then set its size to 32x32, and then save it as a .png file. I expect to get the icon with the highest color depth possible, but I'm getting the one with the lowest color depth possible.

    Here's a minimal, reproducible example:

    from PIL import Image

    icon = Image.open("icon.ico")
    icon.size = (32, 32)
    icon.save("icon.png")


    I'm expecting to get this: [​IMG]

    But, I'm getting this: [​IMG]

    I also tried looking for similar questions on the internet, but I didn't find anything.

    Is there a way I can tell Pillow to extract the 32x32 icon from a .ico file at the highest color depth possible without modifying my .ico file to exclude icons with lower color depths?

    Continue reading...

Compartilhe esta Página