import cv2
import numpy as np
cat = cv2.imread(‘3.jpg’)
rows,colmns,channel = cat.shape
img_key = np.random.randint(0,256,(rows,colmns,channel),np.uint8)
def encode(img,img_key):
result = img = cv2.bitwise_xor(img,img_key)
return result
#加密
result = encode(cat,img_key)
cv2.imwrite(‘1.jpg’,result)
#解密
result = encode(result,img_key)
cv2.imwrite(‘2.jpg’,result)
该代码示例展示了如何利用OpenCV库和numpy进行图像的加密和解密操作。首先读取图像3.jpg,然后生成一个随机的密钥。通过位异或操作实现加密,将加密后的图像保存为1.jpg。接着,使用相同的密钥对加密图像进行解密,并将结果保存为2.jpg。
178

被折叠的 条评论
为什么被折叠?



