"""The app icon from a picture: an .ico for the builder and a .res the game's exe links in (nx2/build.py). python nx2/make_icon.py [out folder, default work] The picture is the user's and stays out of the repository, so these go to work/. Needs Pillow. """ import io import os import struct import sys from PIL import Image SIZES = (16, 24, 32, 48, 64, 128, 256) def square(im, size): """fitted in a transparent square, not stretched""" im = im.convert("RGBA") k = size / max(im.size) fit = im.resize((max(1, round(im.width * k)), max(1, round(im.height * k))), Image.LANCZOS) out = Image.new("RGBA", (size, size), (0, 0, 0, 0)) out.paste(fit, ((size - fit.width) // 2, (size - fit.height) // 2), fit) return out def png_of(im): b = io.BytesIO() im.save(b, "PNG", optimize=True) return b.getvalue() def res_entry(kind, name, data, flags=0x1010): header = struct.pack(" 2 else os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "work") im = Image.open(src) images = [(s, png_of(square(im, s))) for s in SIZES] head = struct.pack("> 10)) if __name__ == "__main__": main()