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
| import random from PIL import Image import numpy as np
def text_to_binary(text): binary_data = ''.join(format(ord(c), '08b') for c in text) return binary_data
def binary_to_text(binary): text = ''.join(chr(int(binary[i:i+8], 2)) for i in range(0, len(binary), 8)) return text
image_path = 'input.png' output_image_path = 'modified_image.png' data_to_hide = "han1"
def png_rgb_lsb(image_path, data_to_hide, output_image_path): image = Image.open(image_path)
pixels = list(image.getdata())
binary_data = text_to_binary(data_to_hide)
if len(binary_data) > len(pixels) * 3: raise ValueError("隐藏数据过大,无法完全嵌入到图像中")
key = [134304, 24906, 1165134, 840434, 1034067, 256626, 960109, 677039, 604987, 1138403, 1252149, 1328829, 693323, 100995, 281079, 808381, 448302, 1262668, 622356, 1360412, 321087, 1424695, 399419, 1260153, 1180374, 1115301, 1420382, 860763, 799633, 764414, 179750, 856208]
index = 0 for k in key: pixel = list(pixels[k]) if index % 2 == 0: if pixel[0] & 1 != int(binary_data[index]): if random.randint(0, 1) == 0: if pixel[0] & 1 == 0: pixel[0] = (pixel[0] & 254) | (pixel[0] + 1) else: pixel[0] = (pixel[0] & 254) | (pixel[0] - 1) else: if pixel[0] & 255 == 255: pixel[0] = (pixel[0] & 254) | (pixel[0] - 1) else: pixel[0] = (pixel[0] & 254) | (pixel[0] + 1) index = index + 1 else: if pixel[2] & 1 != int(binary_data[index]): if random.randint(0, 1) == 0: if pixel[2] & 1 == 0: pixel[2] = (pixel[2] & 254) | (pixel[2] + 1) else: pixel[2] = (pixel[2] & 254) | (pixel[2] - 1) else: if pixel[2] & 255 == 255: pixel[2] = (pixel[2] & 254) | (pixel[2] - 1) else: pixel[2] = (pixel[2] & 254) | (pixel[2] + 1) index = index + 1 pixels[k] = tuple(pixel)
new_image = Image.new(image.mode, image.size) new_image.putdata(pixels) new_image.save(output_image_path)
def png_rgb_ilsb(image_path): key = [134304, 24906, 1165134, 840434, 1034067, 256626, 960109, 677039, 604987, 1138403, 1252149, 1328829, 693323, 100995, 281079, 808381, 448302, 1262668, 622356, 1360412, 321087, 1424695, 399419, 1260153, 1180374, 1115301, 1420382, 860763, 799633, 764414, 179750, 856208] extracted_data = "" img = Image.open(image_path) pixels = list(img.getdata())
extracted_binary = "" index = 0 for k in key: pixel = pixels[k] if index % 2 == 0: extracted_binary += str(pixel[0] & 1) if len(extracted_binary) % 8 == 0: if binary_to_text(extracted_binary[-8:]) == '\x00': break extracted_data += binary_to_text(extracted_binary[-8:]) index = index + 1 else: extracted_binary += str(pixel[2] & 1) if len(extracted_binary) % 8 == 0: if binary_to_text(extracted_binary[-8:]) == '\x00': break extracted_data += binary_to_text(extracted_binary[-8:]) index = index + 1
return extracted_data
png_rgb_lsb(image_path, data_to_hide, output_image_path) data1 = png_rgb_ilsb(output_image_path) print(data1)
|