Advertisement
Guest User

Convert binary file to png image

a guest
Sep 12th, 2018
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | None | 0 0
  1. import numpy,scipy.misc, os, array, argparse
  2.  
  3.  
  4. ap = argparse.ArgumentParser()
  5. ap.add_argument("-p", "--path", required=True,
  6.     help="path to the binaries")
  7. args = vars(ap.parse_args())
  8. count = 0
  9. root = args["path"]
  10.  
  11. def convert(root):
  12.  
  13.     for _, _, files in os.walk(root):
  14.         for file in files:
  15.             filepath = os.path.join(root, file)
  16.             f = open(filepath, 'rb')
  17.             ln = os.path.getsize(filepath);  # length of file in bytes
  18.             width = 256; # depends on file size
  19.             rem = ln % width;
  20.  
  21.             a = array.array("B");  # uint8 array
  22.             a.fromfile(f, ln - rem);
  23.             f.close();
  24.  
  25.             img = numpy.reshape(a, (len(a) / width, width));
  26.             img = numpy.uint8(img);
  27.  
  28.             scipy.misc.imsave(filepath + '.png', img);
  29.  
  30. if __name__ == '__main__':
  31.     convert(args['path'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement