Skip to content

Instantly share code, notes, and snippets.

@gsw945
Created January 19, 2023 02:45
Show Gist options
  • Save gsw945/e4f43d0da735458e09ea5da110b7dde4 to your computer and use it in GitHub Desktop.
Save gsw945/e4f43d0da735458e09ea5da110b7dde4 to your computer and use it in GitHub Desktop.
convert bitmat to ico
# -*- coding: utf-8 -*-
'''
convert bitmat to ico
ref: https://stackoverflow.com/questions/45507/is-there-a-python-library-for-generating-ico-files/62374154#62374154
'''
import pathlib
from PIL import Image
SIZE_16 = (16, 16)
SIZE_24 = (24, 24)
SIZE_32 = (32, 32)
SIZE_48 = (48, 48)
SIZE_64 = (64, 64)
SIZE_128 = (128, 128)
SIZE_256 = (256, 256)
def bitmap2ico(image_path: str, size: tuple[int, int]) -> str:
source = pathlib.Path(image_path).resolve()
print('source: {0}'.format(source))
target = source.parent.joinpath(source.stem).with_suffix('.ico')
print('target: {0}'.format(target))
with Image.open(str(source)) as image:
with image.resize(size) as new_image:
new_image.save(str(target), format='ICO', quality=90)
if __name__ == '__main__':
bitmap2ico(r'./favicon.jpg', SIZE_128)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment