commit 2e472a6f8606d1599f014be6477d8ee3dea64caf Author: E. Almqvist Date: Mon Jan 25 18:23:51 2021 +0100 Stuff diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b06aaa1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.py[cod] +documentation/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..29887e0 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# Image Encoder +Script to create images from bytes diff --git a/ascii_bin.py b/ascii_bin.py new file mode 100644 index 0000000..0bcebfd --- /dev/null +++ b/ascii_bin.py @@ -0,0 +1,23 @@ +import numpy as np +import scipy.misc as smp + +bit_on = [255, 255, 255] # RGB value if bit is 1 +bit_off = [0, 0, 0] # RGB value if bit is 0 + +def translate_bit_color(bit:str): + if( bit == "1" ): + return bit_on + else: + return bit_off + +def data_to_matrix(data:list) -> np.array: + newdat = [ [translate_bit_color(bit) for bit in byte] for byte in data ] + col_array = np.array(newdat, dtype=np.uint8) + + return col_array + +def main(inp:list) -> None: + col_array = data_to_matrix(inp) + + img = smp.toimage(col_array) + img.show() diff --git a/generator.py b/generator.py new file mode 100755 index 0000000..8cec4d7 --- /dev/null +++ b/generator.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python + +import sys +from ascii_bin import main as ascii_bin + +method_pointers = { + "ascii_bin": ascii_bin +} + + +def get_input(file:str, bytesize:int = 8) -> list: + f = open(file, "r") + + f_str = f.read() + f_str = f_str.strip() + f_str = f_str.replace(" ", "") + + return [ f_str[i:i+bytesize] for i in range(0, len(f_str), bytesize) ] + +def generate_image(file:str, method:str = "ascii_bin", bytesize:int = 8) -> None: + inp = get_input(file, bytesize) + + method_pointers[method](inp) + + +if __name__ == "__main__": + generate_image(sys.argv[3], sys.argv[1], int(sys.argv[2])) diff --git a/testbytes.txt b/testbytes.txt new file mode 100644 index 0000000..8b289d5 --- /dev/null +++ b/testbytes.txt @@ -0,0 +1 @@ +01100100 01010111 01100100 01101110 01011001 01111010 01101111 01110110 01001100 00110010 00110101 00110101 01100101 01101101 01100100 01111001 01100011 01001000 01010101 01110101 01011010 01101110 01001001 01001011