Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Convert PBM to Bytearray
# Convert image to bytes
#!/usr/bin/env python
import sys
import os
def main():
if len(sys.argv) != 2:
return 2
with open(sys.argv[1], 'rb') as fd:
pbm_format = fd.readline().strip()
if pbm_format != b'P4':
print("ERROR: input file must be binary PBM (type P4)",
file = sys.stderr)
return 1
pbm_dims = [int(d) for d in fd.readline().strip().split()]
pbm_data =
fbbase = "buf_{0}".format(os.path.basename(sys.argv[1]))
fbname = os.path.splitext(fbbase)[0]
with sys.stdout as fd:
f = "{0} = bytearray({1})\n"
fd.write(f.format(fbname, str(pbm_data), pbm_dims[0], pbm_dims[1]))
def usage():
print("""usage: {0} PBM_FILE""".format(os.path.basename(sys.argv[0])),
file = sys.stderr)
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment