binimg.py (view raw)
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 png
import sys
import getopt
def main(args):
width = 128
infile = ""
outfile = ""
opts, args = getopt.getopt(args, "i:w:o:")
for opt, arg in opts:
if opt == '-i':
infile = arg
elif opt == '-o':
outfile = arg
elif opt == '-w':
width = int(arg)
else:
print("ERROR: wrong argument format")
return
print(width)
print(infile)
print(outfile)
with open(infile, mode="rb") as f:
bin_data = f.read()
rows = []
while len(bin_data) > 0:
rows = rows + [ bin_data[0:width-1] ]
bin_data = bin_data[width:]
png.from_array(rows, 'L').save(outfile)
if __name__ == "__main__":
main(sys.argv[1:])