Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/ios/generate_icons.py
3185 views
1
import os
2
from PIL import Image
3
import json
4
5
# Input files
6
icons = {
7
"light": "icon_gold_backfill_1024.png",
8
"dark": "dark.png",
9
"tinted": "tinted.png"
10
}
11
12
output_dir = "IconGold.appiconset"
13
os.makedirs(output_dir, exist_ok=True)
14
15
# Sizes and scales required by iOS
16
icon_sizes = [
17
(20, [1, 2, 3]),
18
(29, [1, 2, 3]),
19
(40, [1, 2, 3]),
20
(60, [2, 3]),
21
(76, [1, 2]),
22
(83.5, [2]),
23
(1024, [1]),
24
]
25
26
# Appearance tags for Contents.json, none for light
27
appearance_tags = {
28
"light": None,
29
"dark": [{"appearance": "luminosity", "value": "dark"}],
30
"tinted": [{"appearance": "luminosity", "value": "tinted"}]
31
}
32
33
def save_icon(image, size_pt, scale, appearance):
34
px = int(size_pt * scale)
35
# file name like: icon_20x20@2x_light.png
36
suffix = appearance
37
filename = f"icon_{int(size_pt)}x{int(size_pt)}@{scale}x_{suffix}.png"
38
filepath = os.path.join(output_dir, filename)
39
resized = image.resize((px, px), Image.LANCZOS)
40
resized.save(filepath)
41
return filename
42
43
def generate_images_for_appearance(img_path, appearance):
44
image = Image.open(img_path)
45
images = []
46
for size, scales in icon_sizes:
47
for scale in scales:
48
filename = save_icon(image, size, scale, appearance)
49
entry = {
50
"idiom": "universal",
51
"size": f"{size}x{size}",
52
"scale": f"{scale}x",
53
"filename": filename,
54
"platform": "ios",
55
}
56
if appearance_tags[appearance]:
57
entry["appearances"] = appearance_tags[appearance]
58
images.append(entry)
59
return images
60
61
# Generate all images and JSON entries
62
all_images = []
63
for appearance, filepath in icons.items():
64
all_images.extend(generate_images_for_appearance(filepath, appearance))
65
66
contents = {
67
"images": all_images,
68
"info": {
69
"version": 1,
70
"author": "xcode"
71
}
72
}
73
74
with open(os.path.join(output_dir, "Contents.json"), "w") as f:
75
json.dump(contents, f, indent=4)
76
77
print("✅ IconGold asset catalog with light, dark and tinted appearances generated.")
78
79