import numpy as np
from PIL import Image
from matplotlib import pyplot as plt
def henon_map(a, b, size):
""" 生成基于 Henon 映射的序列 """
x, y = 0.1, 0.1
sequence = []
for _ in range(size):
x, y = 1 - a * x * x + y, b * x
sequence.append(x)
return sequence
def process_image(image_array, a, b):
""" 使用 Henon 映射处理图像 """
sequence = henon_map(a, b, image_array.size)
flat_img = image_array.flatten()
for i in range(len(flat_img)):
flat_img[i] ^= int((sequence[i] % 1) * 256)
return flat_img.reshape(image_array.shape)
def load_image(image_path):
""" 加载并转换图像为 numpy 数组 """
try:
img = Image.open(image_path).convert('L')
return np.array(img)
except IOError:
print(f"无法加载图像:{image_path}")
return None
# 参数和路径
a, b = 1.4, 0.3
input_path = r'c:\picture\grid.png'
output_path = r'c:\picture\Henon.png'
# 加载图像
original = load_image(input_path)
if original is not None:
# 处理图像
encrypted = process_image(original, a, b)
decrypted = process_image(encrypted, a, b)
# 保存加密图像
Image.fromarray(encrypted.astype(np.uint8)).save(output_path)
# 显示图像
fig, ax = plt.subplots(1, 3, figsize=(12, 4))
for i, img in enumerate([original, encrypted, decrypted]):
ax[i].imshow(img, cmap='gray')
ax[i].set_title(['Original', 'Encrypted', 'Decrypted'][i])
ax[i].axis('off')
plt.tight_layout()
plt.show()
Henon映射进行图像加密解密,并展示过程图像
于 2024-01-21 14:09:28 首次发布