| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 89 |
|
| 90 |
|
| 91 |
subprocess.call( |
| 92 |
[ |
| 93 |
"iconutil", |
| 94 |
"-c", |
| 95 |
"icns", |
| 96 |
iconsetDir, |
| 97 |
"-o", |
| 98 |
destDir / f"{fname}.icns" |
| 99 |
] |
| 100 |
) |