def test_filepath_wrong_extension(self): filepath = "C:\\Users\\User\\Documents\\example.png" self.assertNotEqual( filepath, check_filename("C:\\Users\\User\\Documents\\example.png", extension=".txt")) filepath = "/home/user/example.png" self.assertNotEqual( filepath, check_filename("/home/user/example.png", extension=".txt"))
def test_filepath_without_extension_trailing_slash(self): filepath = "C:\\Users\\User\\Documents\\example\\" self.assertEqual( "C:\\Users\\User\\Documents\\example" + ".txt", check_filename("C:\\Users\\User\\Documents\\example\\", extension=".txt")) filepath = "/home/user/example/" self.assertEqual( "/home/user/example" + ".txt", check_filename("/home/user/example", extension=".txt"))
def encode(text, image_path, limit=256): """ Take a string of text and encode it into an 8-bit grayscale image. :param str text: Text may be ASCII or UTF-8 but limit value must be changed accordingly. :param str image_path: Path to image file. Should have a '.png' extension or no extension :param int limit: The value limit for each pixel. 256 = 8 bit meaning all character encoding schemes using 8 or fewer bit can be encoded. If limit is 65536 then character encoding schemes using 16 bits or less can be applied e.g. UTF-8. When a character is used from a character set greater than the limit, the character value will be divided by the limit value. e.g. limit=256 character=Ĭ (value=300), resulting value will be 44. For values equal to the limit, the resulting value will be 1 to avoid NULL within the encoded data. Limit is the number of possible values in decimal from 1 to a max value. (default=256 i.e. 8 bit pixels/ 1- 256 means 255 possible values) :return str: The path to the image produced. """ if type(text) is not str: raise TypeError("Parameter 'text' must be a string.") text_length = len(text) print(text_length) size = get_image_size(text_length) print(size) result_path = check_filename(image_path, extension=".png") img = Image.new("L", size) # grayscale, blank black image ind = 0 for row in range(0, size[0]): for col in range(0, size[1]): if ind < text_length: # only change pixel value for length of text pixel_value = convert_char_to_int(text[ind], limit=limit) img.putpixel((row, col), pixel_value) ind += 1 else: # end of text, leave remaining pixel(s) black to indicate null break img.save(result_path) return result_path
def decode_to_file(image_path, file_path): """ Take an encoded png image and produce a text file with the decoded text. :param str image_path: Path to a png image with encoded text to be extracted. :param str file_path: Path to file where decoded text will be stored. Should be a plain text file '.txt'. :return str: Path to output text file. """ if not path.isfile(image_path): raise FileExistsError("Image file {0} does not exist. Cannot decode a nonexistent image".format(image_path)) if image_path[-4:].lower() != ".png": raise TypeError("Image {0} must be a png image file with a '.png' file extension".format(image_path)) file_path = check_filename(file_path) decoded_text = decode(image_path) with open(file_path, "w") as f: f.write(decoded_text) return file_path
def test_simple_filename_with_extension(self): filename = "example.txt" self.assertEqual(filename, check_filename("example.txt", extension=".txt"))
def test_linux_filepath_without_extension(self): filepath = "/home/user/example" self.assertEqual( "/home/user/example" + ".txt", check_filename("/home/user/example", extension=".txt"))
def test_windows_filepath_without_extension(self): filepath = r"C:\Users\User\Documents\example" self.assertEqual( filepath + ".txt", check_filename(r"C:\Users\User\Documents\example", extension=".txt"))