-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresize.py
97 lines (81 loc) · 3.29 KB
/
resize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from PIL import Image
from pathlib import Path
import colors
import PIL
import json
import sys
def fail(message):
sys.exit(colors.red("ERROR: " + message))
def warn(message, confirm=False, confirmation_message=". Do you wish to continue?"):
def abort():
print(colors.red("Aborted"))
sys.exit()
print(colors.yellow("WARNING: " + message), end="")
if confirm:
print(colors.yellow(" " + confirmation_message + " ") + colors.green("Y") + colors.yellow("/") + colors.red("n"))
answer = ""
prompt = colors.yellow(">>> ")
while answer != "y" and answer != "n" and answer != "yes" and answer != "no":
try:
answer = input(prompt).lower()
except KeyboardInterrupt:
print()
abort()
prompt = colors.yellow("I don't recognize that answer. Please answer with (") + colors.green("y") + colors.yellow(")es or (") + colors.red("n") + colors.yellow(")o")
if answer == "n":
abort()
else:
print()
if len(sys.argv) < 2:
fail("You must specify a file")
file_path = Path(sys.argv[1]).resolve()
if not file_path.is_file():
fail(f"The file \"{file_path}\" was not found")
if len(sys.argv) >= 3:
output_directory = Path(sys.argv[2]).resolve()
if not output_directory.is_dir():
fail(f"The directory \"{output_directory}\" was not found")
else:
output_directory = file_path.parent
should_warn_for_asset_bundle = True
for part in output_directory.parts:
if part.endswith(".xcassets"):
should_warn_for_asset_bundle = False
break
if should_warn_for_asset_bundle:
warn("The output directory is not an asset bundle")
set_name = file_path.stem
set_name_flag = "--setname"
if set_name_flag in sys.argv:
flag_index = sys.argv.index(set_name_flag)
if len(sys.argv) >= flag_index:
set_name = sys.argv[flag_index + 1]
else:
fail(f"{set_name_flag} flag was specified, but no iconset name was provided")
target_directory = output_directory / f"{set_name}.imageset"
try:
target_directory.mkdir()
except FileExistsError:
warn(f"The file \"{target_directory}\" already exists.", confirm=True, confirmation_message="Overwrite?")
sizes_flag = "--sizes"
if len(sys.argv) >= 5 and sizes_flag in sys.argv:
try:
sizes = int(sys.argv[sys.argv.index(sizes_flag) + 1])
except ValueError:
fail("The number of sizes must be an integer")
except IndexError:
fail(f"{sizes_flag} flag was specified, but no size count was provided")
else:
sizes = 3
try:
base = Image.open(str(file_path))
except OSError:
fail(f"\"{sys.argv[1]}\" is not an image")
contents_index = {"images": [], "info": {"version": 1, "author": "xcresize"}}
for factor in reversed(range(1, sizes + 1)):
size = tuple(int(dimension * (factor/sizes)) for dimension in base.size)
new_file = Path(target_directory) / f"{file_path.stem}@{factor}x{file_path.suffix}"
base.resize(size, Image.ANTIALIAS).save(str(new_file))
contents_index["images"].append({"size": f"{size[0]}x{size[1]}", "idiom": "universal", "filename": new_file.name, "scale": f"{factor}x"})
with open(str(target_directory / "Contents.json"), "w") as contents_file:
json.dump(contents_index, contents_file, indent=2, separators=(",", " : "))