-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
199 lines (152 loc) · 4.79 KB
/
main.py
File metadata and controls
199 lines (152 loc) · 4.79 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from collections import namedtuple
from enum import Enum, unique
from io import BytesIO
from PIL import Image
from zipfile import ZipFile
import argparse
# from gifmaker import makedelta
@unique
class DEVICES(Enum):
MAKO = (768, 1270)
def zero_padded_number(padding, num):
num = str(num)
while len(num) < padding:
num = "0" + num
return num
def _count(gif):
count = 0
time = 0
while True:
try:
gif.seek(gif.tell() + 1)
time += gif.info["duration"]
count += 1
except EOFError:
gif.seek(0)
break
time = time / count
return count, time
def count_frames(gif):
return _count(gif)[0]
def count_time(gif):
return _count(gif)[1]
def extract_frames(gif):
frame = Image.open(gif)
nframes = count_frames(frame)
imglist = []
for frames in range(nframes + 1):
frame.seek(frames)
imglist.append(frame.copy())
return imglist
Part = namedtuple("Part", "loop delay folder")
def make_desc_file(width, height, fps, parts):
desc = ""
desc += "{w} {h} {s}\n".format(w=width, h=height, s=fps)
for p in parts:
desc += "p {l} {d} {f}\n".format(l=p.loop, d=p.delay, f=p.folder)
return desc
def scale_img(width, img):
wpercent = (width / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
return img.resize((width, hsize), Image.ANTIALIAS)
def make_bootanimation(dim, gif, outfn=None, fps=None, fit=None):
width, height = dim
if outfn is None:
outfn = "bootanimation.zip"
z = ZipFile(outfn, mode="w")
if fps is None:
fps = 1000 / count_time(Image.open(gif))
desc = make_desc_file(width, height, int(fps), [Part(0, 0, "part0")])
z.writestr("desc.txt", desc)
images = extract_frames(gif)
padding = len(str(len(images)))
for index, image in enumerate(images):
# scale it
if fit is not None:
x = int(width * (fit / 100))
image = scale_img(x, image)
# background it
background = Image.new("RGBA", (width, height))
x = int((width / 2) - (image.size[0] / 2))
y = int((height / 2) - (image.size[1] / 2))
background.paste(image, (x, y))
# save it
img = BytesIO()
background.save(img, format="png")
z.writestr(
'part0/{}.png'.format(zero_padded_number(padding, index)),
img.getvalue()
)
# Technologic
z.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Builds boot animations for android"
)
subparsers = parser.add_subparsers(dest="command")
list_parser = subparsers.add_parser("list")
preview_parser = subparsers.add_parser("preview")
build_parser = subparsers.add_parser("build")
build_parser.add_argument(
"dimensions",
metavar="DIMENSIONS",
help=(
"name of a device listed by `list_devices` "
"or a tuple containing width and height"
)
)
build_parser.add_argument(
"fname",
metavar="FROM",
help="the input GIF"
)
build_parser.add_argument(
"-o",
dest="outfilename",
default="bootanimation",
metavar="OUTPUT",
help="name of the output zip file"
)
build_parser.add_argument(
"--fps",
default=None,
type=int,
help="speed of the animation"
)
build_parser.add_argument(
"--fit",
default=None,
type=int,
help="if set will upscale the animation to fit the dimensions"
)
args = parser.parse_args()
if args.command == "list":
for i in DEVICES:
print(i)
if args.command == "preview":
print("[Insert preview here]") # TODO
if args.command == "build":
args.dimensions = args.dimensions.upper()
dimensions = device = None
try:
device = args.dimensions
dimensions = DEVICES[args.dimensions].value
except KeyError:
device = "Unknown"
dimensions = "".join(
i for i in args.dimensions if i not in "() "
).split(",")
try:
dimensions = [int(i) for i in dimensions]
except ValueError:
err = "Invalid dimensions or device name ({})"
raise Exception(err.format(args.dimensions))
outfilename = args.outfilename
if not outfilename.endswith(".zip"):
outfilename += ".zip"
print("Building animation for {device}{dimensions}".format(
device=device, dimensions=dimensions))
make_bootanimation(
dimensions, args.fname, outfilename, args.fps, args.fit
)
print("Output in {}".format(outfilename))