SHARE
TWEET

Convert binary file to png image

a guest Sep 12th, 2018 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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'])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top