Created
January 18, 2016 10:24
-
-
Save Inndy/54891f6297458fd8e83f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| import sys | |
| import binascii | |
| def main(argv): | |
| if len(argv) <= 2: | |
| print("Usage: %s input.txt output.html [size-in-px]" % sys.argv[0]) | |
| print(" default size is 2px") | |
| print() | |
| print(" sample input:") | |
| print(" 0aff4699ffeeaa") | |
| print(" 9ff46fefea0a9a") | |
| print(" 0ed9437bcae315") | |
| exit() | |
| def hex2bits(line): | |
| _=line | |
| line = line.replace(' ', '').replace('\n', '') | |
| line = binascii.unhexlify(line) | |
| result = ''.join( bin(i)[2:].replace('0', ' ').rjust(8) for i in line ) | |
| print(_, result) | |
| return result | |
| def process_line(l): | |
| out = "" | |
| for ch in l: | |
| out += '<td></td>' if ch == ' ' else '<td class="b"></td>' | |
| return "<tr>{line}</tr>".format(line = out) | |
| input_data = open(argv[1], "r").read().split('\n') | |
| input_data = map(hex2bits, input_data) | |
| input_data = list(map(process_line, input_data)) | |
| size = argv[3] if len(argv) > 3 else '2'; | |
| if not size.endswith('px'): | |
| size = '{}px'.format(size) | |
| html_template = """ | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>Image</title> | |
| <style type="text/css"> | |
| * {{ padding: 0; margin: 0; }} | |
| body {{ padding: 12px; }} | |
| table {{ border-collapse: collapse; }} | |
| td {{ | |
| min-width: {size}; | |
| min-height: {size}; | |
| width: {size}; | |
| height: {size}; | |
| }} | |
| td.b {{ background-color: #000; }} | |
| </style> | |
| </head> | |
| <body> | |
| <table>{content}</table> | |
| </body> | |
| </html> | |
| """ | |
| content = html_template.format( | |
| content = "\n".join(input_data), | |
| size = size | |
| ) | |
| open(argv[2], "w").write(content) | |
| if __name__ == '__main__': | |
| main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment