Created
June 8, 2017 01:53
-
-
Save joe-sullivan/2a62b90702603cfb9c1a33706f266d87 to your computer and use it in GitHub Desktop.
Digram based binary visualization
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
#!/usr/bin/env python3 | |
from collections import Counter | |
from PIL import Image | |
import math, os, sys | |
def create_image(filename): | |
# build digram | |
digram = Counter() | |
with open(filename, 'rb') as f: | |
y = ord(f.read(1)) | |
for x in f.read(): | |
digram.update([(x,y)]) | |
y = x | |
# log normal data | |
for key in digram: | |
digram[key] = math.log(digram[key], 256) | |
# make image | |
img = Image.new('F', (256, 256), 0) | |
for point in digram: | |
color = digram[point] * 255 | |
img.putpixel(point, color) | |
img.convert('RGB').save('%s.png' % os.path.basename(filename)) | |
def main(): | |
try: | |
filename = sys.argv[1] | |
except Exception as e: | |
print('Error: missing filename') | |
return | |
else: | |
if os.path.isfile(filename): | |
create_image(filename) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment