import random
import numpy as np
after={}
solve_code={}
data=[]
for i in range(0,256):
data.append(i)
for i in range(0,256):
k=random.sample(data, 1)
solve_code[k[0]]=i
after[i]=k[0]
data.remove(k[0])
print(solve_code)
np.save("C:/Users/xpp/Desktop/code.npy",after)
np.save("C:/Users/xpp/Desktop/solve_code.npy",solve_code)
import numpy as np
from PIL import Image
im=Image.open('C:/Users/xpp/Desktop/Lena.png')
im2=np.array(im)
size1=im2.shape
print(size1)
#图像字典加密
code=np.load('C:/Users/xpp/Desktop/code.npy',allow_pickle=True).item()
slove_code=np.load('C:/Users/xpp/Desktop/solve_code.npy',allow_pickle=True).item()
print(code)
new_array=np.zeros(size1)
k1=0
print("进入循环")
for i in range(0,size1[0]):
for j in range(0,size1[1]):
for k in range(0,size1[2]):
new_array[i, j, k] =code[im2[i,j,k]]
im3=Image.fromarray(np.uint8(new_array))
im3.show()
im3.save('C:/Users/xpp/Desktop/result.png')
{154: 0, 46: 1, 13: 2, 161: 3, 100: 4, 197: 5, 24: 6, 255: 7, 201: 8, 202: 9, 177: 10, 182: 11, 103: 12, 236: 13, 32: 14, 204: 15, 119: 16, 165: 17, 228: 18, 152: 19, 214: 20, 125: 21, 105: 22, 70: 23, 74: 24, 242: 25, 162: 26, 245: 27, 241: 28, 134: 29, 66: 30, 223: 31, 188: 32, 203: 33, 254: 34, 151: 35, 167: 36, 3: 37, 186: 38, 145: 39, 65: 40, 31: 41, 56: 42, 153: 43, 143: 44, 221: 45, 27: 46, 142: 47, 76: 48, 18: 49, 77: 50, 96: 51, 149: 52, 115: 53, 122: 54, 98: 55, 99: 56, 218: 57, 159: 58, 50: 59, 183: 60, 1: 61, 86: 62, 235: 63, 194: 64, 157: 65, 12: 66, 212: 67, 53: 68, 113: 69, 19: 70, 30: 71, 137: 72, 95: 73, 84: 74, 59: 75, 184: 76, 240: 77, 208: 78, 248: 79, 51: 80, 158: 81, 195: 82, 58: 83, 78: 84, 215: 85, 243: 86, 217: 87, 61: 88, 106: 89, 37: 90, 198: 91, 144: 92, 168: 93, 199: 94, 181: 95, 166: 96, 21: 97, 110: 98, 222: 99, 227: 100, 225: 101, 244: 102, 114: 103, 111: 104, 91: 105, 75: 106, 207: 107, 253: 108, 128: 109, 25: 110, 55: 111, 57: 112, 141: 113, 133: 114, 126: 115, 135: 116, 63: 117, 48: 118, 20: 119, 60: 120, 180: 121, 4: 122, 174: 123, 7: 124, 230: 125, 6: 126, 233: 127, 35: 128, 226: 129, 42: 130, 250: 131, 88: 132, 120: 133, 132: 134, 193: 135, 173: 136, 36: 137, 252: 138, 34: 139, 49: 140, 164: 141, 150: 142, 210: 143, 23: 144, 67: 145, 116: 146, 9: 147, 29: 148, 123: 149, 109: 150, 79: 151, 129: 152, 219: 153, 43: 154, 117: 155, 178: 156, 97: 157, 0: 158, 90: 159, 54: 160, 39: 161, 220: 162, 64: 163, 213: 164, 71: 165, 102: 166, 45: 167, 160: 168, 189: 169, 163: 170, 52: 171, 26: 172, 191: 173, 251: 174, 41: 175, 238: 176, 108: 177, 209: 178, 249: 179, 247: 180, 234: 181, 80: 182, 33: 183, 216: 184, 138: 185, 224: 186, 16: 187, 171: 188, 94: 189, 172: 190, 232: 191, 175: 192, 121: 193, 89: 194, 196: 195, 11: 196, 190: 197, 17: 198, 87: 199, 5: 200, 246: 201, 156: 202, 81: 203, 104: 204, 155: 205, 139: 206, 148: 207, 239: 208, 146: 209, 192: 210, 73: 211, 107: 212, 136: 213, 124: 214, 176: 215, 206: 216, 205: 217, 14: 218, 44: 219, 112: 220, 85: 221, 92: 222, 62: 223, 10: 224, 200: 225, 82: 226, 170: 227, 8: 228, 28: 229, 15: 230, 40: 231, 72: 232, 187: 233, 22: 234, 147: 235, 237: 236, 2: 237, 231: 238, 47: 239, 140: 240, 179: 241, 185: 242, 118: 243, 131: 244, 38: 245, 83: 246, 211: 247, 101: 248, 169: 249, 130: 250, 127: 251, 93: 252, 69: 253, 68: 254, 229: 255}
算法:图像字典加密是将像素点使用映射关系(字典)让像素点排列完全混乱实现加密。