dat文件转图片

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)
        # print(f'src:{src_pth} , out: {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()
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值