1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
| # -*- coding: UTF-8 -*-
import matplotlib.pyplot as plt
import cv2
import numpy as np
from PIL import Image
def arnold_encode(image, shuffle_times, a, b):
""" Arnold shuffle for rgb image
Args:
image: input original rgb image
shuffle_times: how many times to shuffle
Returns:
Arnold encode image
"""
# 1:创建新图像
arnold_image = np.zeros(shape=image.shape)
# 2:计算N
h, w = image.shape[0], image.shape[1]
N = h # 或N=w
# 3:遍历像素坐标变换
for time in range(shuffle_times):
for ori_x in range(h):
for ori_y in range(w):
# 按照公式坐标变换
new_x = (ori_x + b * ori_y) % N
new_y = (a * ori_x + (a * b + 1) * ori_y) % N
# 像素赋值
# print(image[ori_x, ori_y, :])
# print(arnold_image[new_x, new_y, :])
arnold_image[new_x, new_y, :] = image[ori_x, ori_y, :]
image = np.copy(arnold_image)
cv2.imwrite('flag_arnold_encode.png', arnold_image,
[int(cv2.IMWRITE_PNG_COMPRESSION), 0])
return arnold_image
def arnold_decode(image, shuffle_times, a, b):
""" decode for rgb image that encoded by Arnold
Args:
image: rgb image encoded by Arnold
shuffle_times: how many times to shuffle
Returns:
decode image
"""
# 1:创建新图像
decode_image = np.zeros(shape=image.shape)
# 2:计算N
h, w = image.shape[0], image.shape[1]
N = h # 或N=w
# 3:遍历像素坐标变换
for time in range(shuffle_times):
for ori_x in range(h):
for ori_y in range(w):
# 按照公式坐标变换
new_x = ((a * b + 1) * ori_x + (-b) * ori_y) % N
new_y = ((-a) * ori_x + ori_y) % N
decode_image[new_x, new_y, :] = image[ori_x, ori_y, :]
image = np.copy(decode_image)
output_path = f'output/flag_panda_dec_{time + 1}.png'
print(f'writing {time + 1}')
cv2.imwrite(output_path, decode_image, [
int(cv2.IMWRITE_PNG_COMPRESSION), 0])
cv2.imwrite('flag_panda_dec.png', decode_image, [
int(cv2.IMWRITE_PNG_COMPRESSION), 0])
return decode_image
c = 179093209181929149953346613617854206675976823277412565868079070299728290913658
p = 302951519846417861008714825074296492447
q = 295488723650623654106370451762393175957
a, b = p, q
period = 190 # panda
# loop = c % period
# Enc
# img = cv2.imread('flag.png')
# arnold_encode(img, round, 11, 13)
# Dec
img = cv2.imread('flag_panda_enc.png')
arnold_decode(img, period, a, b)
|