gpt4 book ai didi

python - 使用乘法和减法加密和解密图像

转载 作者:行者123 更新时间:2023-12-04 07:22:45 29 4
gpt4 key购买 nike

我使用加法和减法在 Python 中对图像进行编码和解码,但我无法通过乘法和除法来完成。任何人都可以吗?
加密代码部分:

for  i in range(image.shape[0]):
for j in range(image.shape[1]):
temp=image[i, j] + key[i,j]
if temp > 255:
temp = image[i, j] + key[i,j] - 255
result[i,j]=temp
解密代码部分:
for i in range(image.shape[0]):
for j in range(image.shape[1]):
temp = image[i, j]-key[i,j]
if temp < 0:
temp = 255 + temp
result[i, j] = temp

最佳答案

你可以试试这种方式:

from cv2 import *
import numpy as np

image_input = imread('image_input.jpeg', IMREAD_GRAYSCALE)
(x, y) = image_input.shape
image_input = image_input.astype(float) / 255.0
加密
mu, sigma = 0, 0.1 # mean and standard deviation
key = np.random.normal(mu, sigma, (x, y)) + np.finfo(float).eps
image_encrypted = image_input / key
imwrite('image_encrypted.jpg', image_encrypted * 255)
解密
image_output = image_encrypted * key
image_output *= 255.0
imwrite('image_output.jpg', image_output)

关于python - 使用乘法和减法加密和解密图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68394044/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com