import cv2 from argparse import ArgumentParser parser = ArgumentParser() parser.add_argument('input', help='Input image') parser.add_argument('output', help='Output bin') args = parser.parse_args() file_path = args.output img = cv2.imread(args.input, cv2.IMREAD_GRAYSCALE) height, width = img.shape vals = [] for i in range(height): for j in range(width): k = img[i, j] vals.append(k) print(f'{len(vals)} bytes') with open(file_path, "wb") as file: file.write(bytes(vals))