Henon映射进行图像加密解密,并展示过程图像

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()

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值