-
Notifications
You must be signed in to change notification settings - Fork 0
/
frames-to-bitmaps.py
38 lines (30 loc) · 1.13 KB
/
frames-to-bitmaps.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
import argparse
import os
import array
from PIL import Image
def to_84x48_binary_image(filename):
with Image.open(filename).resize((84, 48)).convert('1') as img:
data = img.getdata()
converted = bytearray()
for i in range(6):
for j in range(84):
v = 0
for sh in range(8):
b = int(bool(data[84 * 8 * i + 84 * sh + j]))
v = v | (b << sh)
v = ~v & 255
converted.append(v)
return converted
def imgs_to_binary_file(files, outfile='bitmaps.bin'):
outf = open(outfile, "wb")
for file in files:
arr = array.array('L', to_84x48_binary_image(file))
outf.write(arr)
parser = argparse.ArgumentParser()
parser.add_argument('--input', help='input directory')
parser.add_argument('--out', help='output file', default='bitmaps.bin')
args = parser.parse_args()
filenames = sorted(os.path.join(args.input, f) for f in os.listdir(args.input) if os.path.isfile(os.path.join(args.input, f)))
name = os.path.basename(args.out)
name = name[:name.find('.')]
imgs_to_binary_file(filenames, args.out)