# Tool used to generate assets like bullets from sys import argv from os import listdir, remove from os.path import isfile, join from PIL import Image import numpy as np import colorsys ############################################################################### # Snippet from https://stackoverflow.com/questions/7274221/changing-image-hue-with-python-pil def shift_hue(arr, hout): rgb_to_hsv = np.vectorize(colorsys.rgb_to_hsv) hsv_to_rgb = np.vectorize(colorsys.hsv_to_rgb) r, g, b, a = np.rollaxis(arr, axis=-1) h, s, v = rgb_to_hsv(r, g, b) h = hout r, g, b = hsv_to_rgb(h, s, v) arr = np.dstack((r, g, b, a)) return arr def colorize(image, hue): """ Colorize PIL image `original` with the given `hue` (hue within 0-360); returns another PIL image. """ img = image.convert('RGBA') arr = np.array(np.asarray(img).astype('float')) new_img = Image.fromarray(shift_hue(arr, hue/360.).astype('uint8'), 'RGBA') return new_img ############################################################################### # Make images if "make" in argv: dir = "./assets-cg/img/bullets/" sprites = [join(dir, f) for f in listdir(dir) if isfile(join(dir, f))] colors = 8 for f in sprites: img = Image.open(f) for i in range(colors): img2 = colorize(img, 360 * i / colors) img2.save(f[:-4] + f'_{i+1}.png', 'PNG') # Clean images if "clean" in argv: dir = "./assets-cg/img/bullets/" sprites = [join(dir, f) for f in listdir(dir) if f[-8:] == "-tmp.png"] for s in sprites: print(s) remove(s)