-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
71 lines (56 loc) · 2.11 KB
/
main.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# python3
import os
def convert(inpath, outpath):
# get all files in the directory
fileitems = os.listdir(inpath)
cc = 0
# loop over each file in the directory
for fileitem in fileitems:
# get file name
filename = fileitem[0:fileitem.find('.')]
# read binary file
f1 = open(os.path.join(inpath, fileitem), 'rb')
fileitembytes = f1.read()
newfile = []
if (fileitembytes[0] ^ 0xFF) == (fileitembytes[1] ^ 0xD8):
y1 = fileitembytes[0] ^ 0xFF
print('%d: %s,JPG,encode pattern 0x%X' % (cc + 1, fileitem, y1))
# perform byte conversion
for i in fileitembytes:
newbyte = i ^ y1
newfile.append(newbyte)
newfile2 = bytes(newfile)
# write new file
f2 = open(os.path.join(outpath, filename + '.jpg'), 'wb')
f2.write(newfile2)
cc += 1
elif (fileitembytes[0] ^ 0x89) == (fileitembytes[1] ^ 0x50):
y1 = fileitembytes[0] ^ 0x89
print('%d: %s,PNG,encode pattern 0x%X' % (cc + 1, fileitem, y1))
for i in fileitembytes:
newbyte = i ^ y1
newfile.append(newbyte)
newfile2 = bytes(newfile)
f2 = open(os.path.join(outpath, filename+'.png'), 'wb')
f2.write(newfile2)
cc += 1
elif (fileitembytes[0] ^ 0x47) == (fileitembytes[1] ^ 0x49):
y1 = fileitembytes[0] ^ 0x47
print('%d: %s,GIF,encode pattern 0x%X' % (cc + 1, fileitem, y1))
for i in fileitembytes:
newbyte = i ^ y1
newfile.append(newbyte)
newfile2 = bytes(newfile)
f2 = open(os.path.join(outpath, filename+'.gif'), 'wb')
f2.write(newfile2)
cc += 1
else:
print('%s not recognized!' % fileitem)
print('converted %d images in total!' % cc):
if __name__ == '__main__':
convert(
r'/Applications/WeChat/FileStorage/Image/2020-11',
r'/Downloads/Image/'
)