-
Notifications
You must be signed in to change notification settings - Fork 1
/
rgb2png
executable file
·93 lines (73 loc) · 2.54 KB
/
rgb2png
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
#!/usr/bin/env python3
import os, re
import argparse
import PIL.Image
def main():
args = parse_args()
if not args.all:
# check if file exists
if not os.path.isfile(args.file):
print("File not found: {}".format(args.file))
exit(1)
parseFile(args)
else:
# find all files to loop over
files = [
f
for f in os.listdir(".")
if os.path.isfile(f) and re.match(r"^RGB_(.*)\.bin$", f)
]
for f in files:
args.file = f
parseFile(args)
def parseFile(args):
with open(args.file, "rb") as f:
data = list(f.read())
data = data[args.offset :]
num_bytes = len(data)
width = args.width
height = num_bytes // (width * 4)
print("File: {}".format(args.file))
print("Computed height: {}".format(height))
# pad the end so we always have at least w * h bytes
data.extend([0] * (width * 4))
img = PIL.Image.new("RGBA", (width, height), "white")
pixels = img.load() # Create the pixel map
for y in range(height):
for x in range(0, width):
B = data[(y * width * 4) + x * 4 + 0]
G = data[(y * width * 4) + x * 4 + 1]
R = data[(y * width * 4) + x * 4 + 2]
A = data[(y * width * 4) + x * 4 + 3]
pixels[x, y] = (R, G, B, A)
if args.crop:
off_x = (width - args.c_width) / 2
off_y = (height - args.c_height) / 2
img = img.crop((off_x, off_y, off_x + args.c_width, off_y + args.c_height))
img.save("{}.png".format(os.path.basename(args.file)))
def parse_args():
description = """ Util for displaying binary data as bitmap.
"""
parser = argparse.ArgumentParser(description=description)
parser.add_argument("--file", "-f", default="RGB_0000.bin", help="Input file path")
parser.add_argument(
"--all",
action="store_true",
help="Convert all files named RGB_*.bin in current directory",
)
parser.add_argument(
"-w",
"--width",
default=960,
type=int,
help="Width of output, default: %(default)s",
)
parser.add_argument(
"-o", "--offset", default=0, type=int, help="Offset in input file."
)
parser.add_argument("--crop", action="store_true", help="Crop center 720x480")
parser.add_argument("--c_width", default=720, type=int, help="Crop width.")
parser.add_argument("--c_height", default=480, type=int, help="Crop height.")
return parser.parse_args()
if __name__ == "__main__":
main()