1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
"""Encode png image via command-line.
Usage: main (-e|encode) <originImage> [<text>] [<encoded_image>] main (-d|decode) <encoded_image>
Options: -h,--help 显示帮助菜单 -e 加密 -d 解密
Example: main -e coffee.png hello textOrFileToEncode encoded_image.png main -d encoded_image.png """
from PIL import Image from docopt import docopt
def rgba_make_image_even(image): pixels = list(image.getdata()) even_pixels = [(r >> 1 << 1, g >> 1 << 1, b >> 1 << 1, t >> 1 << 1) for [r, g, b, t] in pixels] even_image = Image.new(image.mode, image.size) even_image.putdata(even_pixels) return even_image
def rgb_make_image_even(image): pixels = list(image.getdata()) even_pixels = [(r >> 1 << 1, g >> 1 << 1, b >> 1 << 1) for [r, g, b] in pixels] even_image = Image.new(image.mode, image.size) even_image.putdata(even_pixels) return even_image
def const_len_bin(arg): binary = "0" * (8 - (len(bin(arg)) - 2)) + bin(arg).replace('0b', '') return binary
def rgba_encode_data_in_image(image, data): even_image = rgba_make_image_even(image) binary = ''.join(map(const_len_bin, bytearray(data, 'utf-8'))) if len(binary) > len(image.getdata()) * 4: raise Exception(f"Error: Can't encode more than {len(even_image.getdata()) * 4} bits in this image. ") encoded_pixels = [(r + int(binary[index * 4 + 0]), g + int(binary[index * 4 + 1]), b + int(binary[index * 4 + 2]), t + int(binary[index * 4 + 3])) if index * 4 < len(binary) else (r, g, b, t) for index, (r, g, b, t) in enumerate(list(even_image.getdata()))] encoded_image = Image.new(even_image.mode, even_image.size) encoded_image.putdata(encoded_pixels) return encoded_image
def rgb_encode_data_in_image(image, data): even_image = rgb_make_image_even(image) binary = ''.join(map(const_len_bin, bytearray(data, 'utf-8'))) if len(binary) % 3 != 0: rema = len(binary) % 3 binary = binary + ('0' * (3 - rema)) if len(binary) > len(image.getdata()) * 3: raise Exception(f"Error: Can't encode more than {len(even_image.getdata()) * 3} bits in this image. ") encoded_pixels = [(r + int(binary[index * 3 + 0]), g + int(binary[index * 3 + 1]), b + int(binary[index * 3 + 2])) if index * 3 < len(binary) else (r, g, b) for index, (r, g, b) in enumerate(list(even_image.getdata()))] encoded_image = Image.new(even_image.mode, even_image.size) encoded_image.putdata(encoded_pixels) return encoded_image
def binary_to_string(binary): index = 0 string = [] rec = lambda x, i: x[2:8] + (rec(x[8:], i - 1) if i > 1 else '') if x else '' fun = lambda x, i: x[i + 1:8] + rec(x[8:], i - 1) while index + 1 < len(binary): char_type = binary[index:].index('0') length = char_type * 8 if char_type else 8 string.append(chr(int(fun(binary[index:index + length], char_type), 2))) index += length return ''.join(string)
def rgba_decode_image(image): pixels = list(image.getdata()) binary = ''.join([str(int(r >> 1 << 1 != r)) + str(int(g >> 1 << 1 != g)) + str(int(b >> 1 << 1 != b)) + str(int(t >> 1 << 1 != t)) for (r, g, b, t) in pixels]) location_double_null = binary.find('0000000000000000') end_index = location_double_null + ( 8 - (location_double_null % 8)) if location_double_null % 8 != 0 else location_double_null data = binary_to_string(binary[0:end_index]) return data
def rgb_decode_image(image): pixels = list(image.getdata()) binary = ''.join([str(int(r >> 1 << 1 != r)) + str(int(g >> 1 << 1 != g)) + str(int(b >> 1 << 1 != b)) for (r, g, b) in pixels]) location_double_null = binary.find('0000000000000000') end_index = location_double_null + (8 - (location_double_null % 8)) if location_double_null % 8 != 0 else location_double_null data = binary_to_string(binary[0:end_index]) return data
def is_text_file(path): if path.endswith(".txt"): return True elif path.endswith(".m"): return True elif path.endswith(".h"): return True elif path.endswith(".c"): return True elif path.endswith(".py"): return True else: return False
if __name__ == '__main__': """command-line interface""" arguments = docopt(__doc__)
if arguments['-e'] or arguments['encode']: if arguments['<text>'] is None: arguments['<text>'] = "待加密的文本" if arguments['<encoded_image>'] is None: arguments['<encoded_image>'] = "encoded_image.png"
if is_text_file(arguments['<text>']): with open(arguments['<text>'], 'rt') as f: arguments['<text>'] = f.read()
print("载体图片:") print(arguments['<originImage>'] + "\n") print("待加密密文:") print(arguments['<text>'] + "\n") print("加密后图片:") print(arguments['<encoded_image>'] + "\n") print("加密中……\n") im = Image.open(arguments['<originImage>']) if im.mode == 'RGBA': rgba_encode_data_in_image(im, arguments['<text>']).save(arguments['<encoded_image>']) else: print("暂不支持此图片格式……")
print("加密完成,密文为:\n" + arguments['<text>'] + "\n") elif arguments['-d'] or arguments['decode']: print("解密中……\n") im = Image.open(arguments['<encoded_image>']) if im.mode == 'RGBA': print("解秘完成,密文为:\n" + rgba_decode_image(im) + "\n") else: print("非法的图片格式……")
|