forked from Akegarasu/lora-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt_image.py
More file actions
249 lines (218 loc) · 8.79 KB
/
encrypt_image.py
File metadata and controls
249 lines (218 loc) · 8.79 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
from pathlib import Path
from PIL import PngImagePlugin,_util,ImagePalette
from PIL import Image as PILImage
from PIL import Image
import numpy as np
import hashlib
import sys
def get_range(input:str,offset:int,range_len=4):
offset = offset % len(input)
return (input*2)[offset:offset+range_len]
def get_sha256(input:str):
hash_object = hashlib.sha256()
hash_object.update(input.encode('utf-8'))
return hash_object.hexdigest()
def shuffle_arr(arr,key):
sha_key = get_sha256(key)
key_len = len(sha_key)
arr_len = len(arr)
key_offset = 0
for i in range(arr_len):
to_index = int(get_range(sha_key,key_offset,range_len=8),16) % (arr_len -i)
key_offset += 1
if key_offset >= key_len: key_offset = 0
arr[i],arr[to_index] = arr[to_index],arr[i]
return arr
def shuffle_arr_v2(arr,key):
sha_key = get_sha256(key)
arr_len = len(arr)
s_idx = arr_len
for i in range(arr_len):
s_idx = arr_len - i - 1
to_index = int(get_range(sha_key,i,range_len=8),16) % (arr_len -i)
arr[s_idx],arr[to_index] = arr[to_index],arr[s_idx]
return arr
def encrypt_image(image:Image.Image, psw):
width = image.width
height = image.height
x_arr = [i for i in range(width)]
shuffle_arr(x_arr,psw)
y_arr = [i for i in range(height)]
shuffle_arr(y_arr,get_sha256(psw))
pixels = image.load()
for x in range(width):
_x = x_arr[x]
for y in range(height):
_y = y_arr[y]
pixels[x, y], pixels[_x,_y] = pixels[_x,_y],pixels[x, y]
def dencrypt_image(image:Image.Image, psw):
width = image.width
height = image.height
x_arr = [i for i in range(width)]
shuffle_arr(x_arr,psw)
y_arr = [i for i in range(height)]
shuffle_arr(y_arr,get_sha256(psw))
pixels = image.load()
for x in range(width-1,-1,-1):
_x = x_arr[x]
for y in range(height-1,-1,-1):
_y = y_arr[y]
pixels[x, y], pixels[_x,_y] = pixels[_x,_y],pixels[x, y]
def encrypt_image_v2(image:Image.Image, psw):
width = image.width
height = image.height
x_arr = [i for i in range(width)]
shuffle_arr(x_arr,psw)
y_arr = [i for i in range(height)]
shuffle_arr(y_arr,get_sha256(psw))
pixel_array = np.array(image)
for y in range(height):
_y = y_arr[y]
temp = pixel_array[y].copy()
pixel_array[y] = pixel_array[_y]
pixel_array[_y] = temp
pixel_array = np.transpose(pixel_array, axes=(1, 0, 2))
for x in range(width):
_x = x_arr[x]
temp = pixel_array[x].copy()
pixel_array[x] = pixel_array[_x]
pixel_array[_x] = temp
pixel_array = np.transpose(pixel_array, axes=(1, 0, 2))
image.paste(Image.fromarray(pixel_array))
return image
def dencrypt_image_v2(image:Image.Image, psw):
width = image.width
height = image.height
x_arr = [i for i in range(width)]
shuffle_arr(x_arr,psw)
y_arr = [i for i in range(height)]
shuffle_arr(y_arr,get_sha256(psw))
pixel_array = np.array(image)
pixel_array = np.transpose(pixel_array, axes=(1, 0, 2))
for x in range(width-1,-1,-1):
_x = x_arr[x]
temp = pixel_array[x].copy()
pixel_array[x] = pixel_array[_x]
pixel_array[_x] = temp
pixel_array = np.transpose(pixel_array, axes=(1, 0, 2))
for y in range(height-1,-1,-1):
_y = y_arr[y]
temp = pixel_array[y].copy()
pixel_array[y] = pixel_array[_y]
pixel_array[_y] = temp
image.paste(Image.fromarray(pixel_array))
return image
def encrypt_image_v3(image:Image.Image, psw):
width = image.width
height = image.height
x_arr = np.arange(width)
shuffle_arr_v2(x_arr,psw)
y_arr = np.arange(height)
shuffle_arr_v2(y_arr,get_sha256(psw))
pixel_array = np.array(image)
_pixel_array = pixel_array.copy()
for x in range(height):
pixel_array[x] = _pixel_array[y_arr[x]]
pixel_array = np.transpose(pixel_array, axes=(1, 0, 2))
_pixel_array = pixel_array.copy()
for x in range(width):
pixel_array[x] = _pixel_array[x_arr[x]]
pixel_array = np.transpose(pixel_array, axes=(1, 0, 2))
image.paste(Image.fromarray(pixel_array))
return image
def dencrypt_image_v3(image:Image.Image, psw):
width = image.width
height = image.height
x_arr = np.arange(width)
shuffle_arr_v2(x_arr,psw)
y_arr = np.arange(height)
shuffle_arr_v2(y_arr,get_sha256(psw))
pixel_array = np.array(image)
_pixel_array = pixel_array.copy()
for x in range(height):
pixel_array[y_arr[x]] = _pixel_array[x]
pixel_array = np.transpose(pixel_array, axes=(1, 0, 2))
_pixel_array = pixel_array.copy()
for x in range(width):
pixel_array[x_arr[x]] = _pixel_array[x]
pixel_array = np.transpose(pixel_array, axes=(1, 0, 2))
image.paste(Image.fromarray(pixel_array))
return image
_password = '123qwe'
if PILImage.Image.__name__ != 'EncryptedImage':
super_open = PILImage.open
class EncryptedImage(PILImage.Image):
__name__ = "EncryptedImage"
@staticmethod
def from_image(image:PILImage.Image):
image = image.copy()
img = EncryptedImage()
img.im = image.im
img._mode = image.mode
img._size = image.size
img.format = image.format
if image.mode in ("P", "PA"):
if image.palette:
img.palette = image.palette.copy()
else:
img.palette = ImagePalette.ImagePalette()
img.info = image.info.copy()
return img
def save(self, fp, format=None, **params):
filename = ""
if isinstance(fp, Path):
filename = str(fp)
elif _util.is_path(fp):
filename = fp
elif fp == sys.stdout:
try:
fp = sys.stdout.buffer
except AttributeError:
pass
if not filename and hasattr(fp, "name") and _util.is_path(fp.name):
# only set the name for metadata purposes
filename = fp.name
if not filename or not _password:
# 如果没有密码或不保存到硬盘,直接保存
super().save(fp, format = format, **params)
return
if 'Encrypt' in self.info and (self.info['Encrypt'] == 'pixel_shuffle' or self.info['Encrypt'] == 'pixel_shuffle_2' or self.info['Encrypt'] == 'pixel_shuffle_3'):
super().save(fp, format = format, **params)
return
encrypt_image_v3(self, get_sha256(_password))
self.format = PngImagePlugin.PngImageFile.format
if self.info:
self.info['Encrypt'] = 'pixel_shuffle_3'
pnginfo = params.get('pnginfo', PngImagePlugin.PngInfo())
if not pnginfo:
pnginfo = PngImagePlugin.PngInfo()
pnginfo.add_text('Encrypt', 'pixel_shuffle_3')
params.update(pnginfo=pnginfo)
super().save(fp, format=self.format, **params)
# 保存到文件后解密内存内的图片,让直接在内存内使用时图片正常
dencrypt_image_v3(self, get_sha256(_password))
if self.info:
self.info['Encrypt'] = None
def open(fp,*args, **kwargs):
image = super_open(fp,*args, **kwargs)
if _password and image.format.lower() == PngImagePlugin.PngImageFile.format.lower():
pnginfo = image.info or {}
if 'Encrypt' in pnginfo and pnginfo["Encrypt"] == 'pixel_shuffle':
dencrypt_image(image, get_sha256(_password))
pnginfo["Encrypt"] = None
image = EncryptedImage.from_image(image=image)
return image
if 'Encrypt' in pnginfo and pnginfo["Encrypt"] == 'pixel_shuffle_2':
dencrypt_image_v2(image, get_sha256(_password))
pnginfo["Encrypt"] = None
image = EncryptedImage.from_image(image=image)
return image
if 'Encrypt' in pnginfo and pnginfo["Encrypt"] == 'pixel_shuffle_3':
dencrypt_image_v3(image, get_sha256(_password))
pnginfo["Encrypt"] = None
image = EncryptedImage.from_image(image=image)
return image
return EncryptedImage.from_image(image=image)
PILImage.Image = EncryptedImage
PILImage.open = open
print('图片加密插件加载成功')