52 lines · 2.3 KB · PythonRaw
#!/usr/bin/env python3"""Import Cotton's closed set of transparent act objects at 3x.Usage: python3 scripts/import-act-objects.py [~/lane-briefs/act-objects]Requires Pillow. Originals stay outside the repository; existing Cottonpictures are reused for the stamp, bin, handset and clip."""import jsonfrom pathlib import Pathimport shutilimport sysfrom PIL import Image, ImageDrawNAMES = ('stamp', 'bin', 'handset', 'slip', 'envelope', 'clip', 'loop', 'folder', 'star', 'bell', 'tag')CATALOGUE = Path(__file__).resolve().parent.parent / 'apps/loom/Sources/CottonPaper/Media.xcassets'REUSE = {'stamp': 'stamp-done/stamp-done.png', 'bin': 'object-bin/object-bin@3x.png', 'handset': 'object-phone/object-phone@3x.png', 'clip': 'note-clip/note-clip.png'}def main(source): source = Path(source).expanduser() source.mkdir(parents=True, exist_ok=True) for name, picture in REUSE.items(): folder, filename = picture.split('/') shutil.copyfile(CATALOGUE / (folder + '.imageset') / filename, source / (name + '.png')) sheet = Image.new('RGB', (1000, 600), (223, 211, 188)) draw = ImageDraw.Draw(sheet) for i, name in enumerate(NAMES): with Image.open(source / (name + '.png')) as original: image = original.convert('RGBA') if image.getchannel('A').getextrema()[0] != 0: raise SystemExit(f'{name}: requires a transparent background') image.thumbnail((360, 360), Image.Resampling.LANCZOS) folder = CATALOGUE / f'act-{name}.imageset' folder.mkdir(exist_ok=True) image.save(folder / f'act-{name}.png') (folder / 'Contents.json').write_text(json.dumps({ 'images': [ {'filename': f'act-{name}.png', 'idiom': 'universal', 'scale': '3x'}, {'filename': f'act-{name}.png', 'idiom': 'mac', 'scale': '2x'}, ], 'info': {'author': 'xcode', 'version': 1} }, indent=2) + '\n') image.thumbnail((180, 160), Image.Resampling.LANCZOS) x, y = (i % 5) * 200, (i // 5) * 200 sheet.paste(image, (x + (200-image.width)//2, y), image) draw.text((x+10, y+170), name, fill='black') sheet.save(source / 'contact-sheet.png')if __name__ == '__main__': main(sys.argv[1] if len(sys.argv) > 1 else '~/lane-briefs/act-objects')