Last active
May 10, 2019 17:09
-
-
Save zaneGittins/39a67af524d46fb4047cc0a81cbd3132 to your computer and use it in GitHub Desktop.
BinImage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# BinImage | |
# Author: Zane Gittins | |
# Date: 5/8/2019 | |
import os | |
import sys | |
import numpy | |
import argparse | |
import bitarray | |
from PIL import Image | |
def create_image(binary, image_path): | |
infile = open(binary, "rb") | |
int_data = numpy.array([]) | |
data = infile.read() | |
ba = bitarray.bitarray() | |
ba.frombytes(data) | |
int_data = numpy.append(int_data, ba) | |
int_data.resize((256,256)) | |
mat = numpy.reshape(int_data,(256,256)) | |
img = Image.fromarray(numpy.uint8(mat * 255) , 'L') | |
img.save(image_path, "JPEG") | |
def create_all_dir(top_directory): | |
for root, directories, filenames in os.walk(top_directory): | |
for filename in filenames: | |
filepath = os.path.join(root,filename) | |
imgpath = filepath + ".jpg" | |
create_image(filepath, imgpath) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--binary", "-b", help="Binary to create image from.") | |
parser.add_argument("--save", "-s", help="Path to save image.") | |
parser.add_argument("--auto", "-a", help="Auto enumerate through directory and create images.") | |
args = parser.parse_args() | |
if(args.binary and args.save): | |
create_image(args.binary, args.save) | |
elif(args.auto): | |
create_all_dir(args.auto) | |
else: | |
print("Improper arguments.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment