Created
October 25, 2019 00:07
-
-
Save erikvanzijst/2b0f3af407bf2ebe88b88f0c7d03acc1 to your computer and use it in GitHub Desktop.
Scramble video game level compiler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import sys | |
from PIL import Image | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser( | |
sys.argv[0], description='Convert image to binary.') | |
parser.add_argument('file', type=argparse.FileType('rb')) | |
parser.add_argument('-o', '--output', | |
help='write to file (stdout when omitted)', | |
type=argparse.FileType('wb'), default=sys.stdout) | |
args = parser.parse_args(sys.argv[1:]) | |
img = Image.open(args.file) | |
width, height = img.size | |
for x in range(min(0x8000, width)): | |
b = 0 | |
for y in range(min(8, height)): | |
b |= (1 if img.getpixel((x, y)) else 0) << y | |
args.output.buffer.write( | |
int.to_bytes(b, length=1, byteorder='big', signed=False)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment