Showing the changes to src/scripts (2 of 2 files). Show all
Unified · Split
| @@ -0,0 +1,22 @@ | |||
| 1 | example: | ||
| 2 | python3 generate-iconset.py ~/Desktop/chuck.png | ||
| 3 | |||
| 4 | this will: | ||
| 5 | 1. create a folder ~/Desktop/chuck.iconset | ||
| 6 | 2. generate 10 files with different dimensions and save those into that folder | ||
| 7 | |||
| 8 | next (from Desktop): | ||
| 9 | iconutil -c icns chuck.iconset -o chuck.icns | ||
| 10 | |||
| 11 | this will: | ||
| 12 | create a single chuck.icns file | ||
| 13 | |||
| 14 | |||
| 15 | --- author and reference --- | ||
| 16 | Old version (we are using this one) | ||
| 17 | https://github.com/retifrav/python-scripts/tree/master/generate-iconset | ||
| 18 | |||
| 19 | New version (python package): | ||
| 20 | https://retifrav.github.io/blog/2018/10/09/macos-convert-png-to-icns/ | ||
| 21 | https://github.com/retifrav/generate-iconset/ | ||
| 22 | |||
| @@ -0,0 +1,100 @@ | |||
| 1 | import sys | ||
| 2 | import pathlib | ||
| 3 | import subprocess | ||
| 4 | |||
| 5 | if len(sys.argv) < 2: | ||
| 6 | print("No path to original / hi-res icon provided") | ||
| 7 | raise SystemExit | ||
| 8 | |||
| 9 | if len(sys.argv) > 2: | ||
| 10 | print("Too many arguments") | ||
| 11 | raise SystemExit | ||
| 12 | |||
| 13 | originalPicture = pathlib.Path(sys.argv[1]) | ||
| 14 | if not (originalPicture.is_file()): | ||
| 15 | print(f"There is no such file: {sys.argv[1]}") | ||
| 16 | raise SystemExit | ||
| 17 | |||
| 18 | # if False, then sips will be used instead of ImageMagick | ||
| 19 | useMagick = False | ||
| 20 | |||
| 21 | fname = pathlib.Path(originalPicture).stem | ||
| 22 | ext = pathlib.Path(originalPicture).suffix | ||
| 23 | destDir = pathlib.Path(originalPicture).parent | ||
| 24 | |||
| 25 | iconsetDir = pathlib.Path(destDir / f"{fname}.iconset") | ||
| 26 | if not (iconsetDir.is_dir()): | ||
| 27 | pathlib.Path(iconsetDir).mkdir(parents=False, exist_ok=True) | ||
| 28 | |||
| 29 | |||
| 30 | class IconParameters(): | ||
| 31 | width = 0 | ||
| 32 | scale = 1 | ||
| 33 | |||
| 34 | def __init__(self, width, scale): | ||
| 35 | self.width = width | ||
| 36 | self.scale = scale | ||
| 37 | |||
| 38 | def getIconName(self): | ||
| 39 | if self.scale != 1: | ||
| 40 | return f"icon_{self.width}x{self.width}{ext}" | ||
| 41 | else: | ||
| 42 | return f"icon_{self.width//2}x{self.width//2}@2x{ext}" | ||
| 43 | |||
| 44 | |||
| 45 | # https://developer.apple.com/design/human-interface-guidelines/macos/icons-and-images/app-icon#app-icon-sizes | ||
| 46 | ListOfIconParameters = [ | ||
| 47 | IconParameters(16, 1), | ||
| 48 | IconParameters(16, 2), | ||
| 49 | IconParameters(32, 1), | ||
| 50 | IconParameters(32, 2), | ||
| 51 | IconParameters(64, 1), | ||
| 52 | IconParameters(64, 2), | ||
| 53 | IconParameters(128, 1), | ||
| 54 | IconParameters(128, 2), | ||
| 55 | IconParameters(256, 1), | ||
| 56 | IconParameters(256, 2), | ||
| 57 | IconParameters(512, 1), | ||
| 58 | IconParameters(512, 2), | ||
| 59 | IconParameters(1024, 1), | ||
| 60 | IconParameters(1024, 2) | ||
| 61 | ] | ||
| 62 | |||
| 63 | # generate iconset | ||
| 64 | for ip in ListOfIconParameters: | ||
| 65 | if useMagick: | ||
| 66 | subprocess.call( | ||
| 67 | [ | ||
| 68 | "magick", | ||
| 69 | "convert", | ||
| 70 | originalPicture, | ||
| 71 | "-resize", | ||
| 72 | str(ip.width), | ||
| 73 | iconsetDir / ip.getIconName() | ||
| 74 | ] | ||
| 75 | ) | ||
| 76 | else: | ||
| 77 | subprocess.call( | ||
| 78 | [ | ||
| 79 | "sips", | ||
| 80 | "-z", | ||
| 81 | str(ip.width), | ||
| 82 | str(ip.width), | ||
| 83 | originalPicture, | ||
| 84 | "--out", | ||
| 85 | iconsetDir / ip.getIconName() | ||
| 86 | ] | ||
| 87 | ) | ||
| 88 | #print(f"Generated {ip.getIconName()}") | ||
| 89 | |||
| 90 | # convert iconset to icns file | ||
| 91 | subprocess.call( | ||
| 92 | [ | ||
| 93 | "iconutil", | ||
| 94 | "-c", | ||
| 95 | "icns", | ||
| 96 | iconsetDir, | ||
| 97 | "-o", | ||
| 98 | destDir / f"{fname}.icns" | ||
| 99 | ] | ||
| 100 | ) | ||