-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpng_to_oiff.py
More file actions
executable file
·46 lines (35 loc) · 1.19 KB
/
png_to_oiff.py
File metadata and controls
executable file
·46 lines (35 loc) · 1.19 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
#!/usr/bin/env python
from PIL import Image
from sys import argv
VERSION_MAJOR = 0
VERSION_MINOR = 1
def image_to_oiff(image_path, output_path):
img = Image.open(image_path)
with open(output_path, "wb") as f:
# Magic number; Oreneta Image File Format
f.write(b"OIFF")
# Version number major
f.write(VERSION_MAJOR.to_bytes(2, byteorder="little"))
# Version number minor
f.write(VERSION_MINOR.to_bytes(2, byteorder="little"))
# Image width
f.write(img.width.to_bytes(4, byteorder="little"))
# Image height
f.write(img.height.to_bytes(4, byteorder="little"))
# Pad with zeroes
padding_needed = 128 - f.tell()
if padding_needed > 0:
# pass
f.write(b'\0' * padding_needed)
for y in range(img.height):
for x in range(img.width):
r, g, b, a = img.getpixel((x, y))
pixel = bytes(bytearray([b, g, r, a]))
f.write(pixel)
def main():
if len(argv) < 3:
print("Usage: png_to_oiff.py <input_image> <output_image>")
else:
image_to_oiff(argv[1], argv[2])
if __name__ == "__main__":
main()