dat文件转图片
import os
from tqdm import tqdm
def Format(f):
"""
计算异或值
各图片头部信息
jpeg:ff d8 ff
png:89 50 4e 47
gif: 47 49 46 38
"""
dat_r = open(f, "rb")
try:
a = [(0x89, 0x50, 0x4e), (0x47, 0x49, 0x46), (0xff, 0xd8, 0xff)]
for now in dat_r:
for xor in a:
i = 0
res = []
nowg = now[:3]
for nowByte in nowg:
res.append(nowByte ^ xor[i])
i += 1
if res[0] == res[1] == res[2]:
return res[0]
except:
pass
finally:
dat_r.close()
def Decode_single_folder(in_pth,save_pth):
'''
解码单个文件夹
:param in_pth: 输入单个文件夹路径
:param save_pth: 输出单个文件夹路径
:return:
'''
count = 0
for file in tqdm(os.listdir(in_pth)):
dat_file = os.path.join(in_pth,file)
save_file = os.path.join(save_pth,str(count).zfill(6)+'.jpg')
dat = open(dat_file,'rb')
image = open(save_file,'wb')
xo = Format(dat_file)
for now in dat:
for nowByte in now:
try:
newByte = nowByte ^ xo
image.write(bytes([newByte]))
except TypeError:
pass
dat.close()
image.close()
count += 1
def Decode(in_pth,save_pth):
'''
解码 dat 文件夹路径
:param in_pth: 输入文件夹路径
:param save_pth: 输出文件夹路径
:return:
'''
files = []
for filename in os.listdir(in_pth):
files.append(filename)
for file in tqdm(files):
src_pth = os.path.join(in_pth,file)
out_pth = os.path.join(save_pth,file)
if not os.path.exists(out_pth):
os.mkdir(out_pth)
Decode_single_folder(src_pth,out_pth)
def main():
dat_pth = r'C:\Users\Young\Desktop\test\dat'
save_pth = r'C:\Users\Young\Desktop\test\jpg'
Decode(dat_pth,save_pth)
if __name__ == '__main__':
main()