[Python] 纯文本查看 复制代码 import os
import logging
# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def main(input_path, output_path):
"""主函数,处理DAT文件并解码为图片"""
if not os.path.exists(output_path):
os.makedirs(output_path)
dat_files = get_dat_files(input_path)
total_files =len(dat_files)
if total_files ==0:
logging.error('没有找到任何.dat文件')
return
for index, dat_file in enumerate(dat_files, start=1):
file_path = os.path.join(input_path, dat_file)
file_name = os.path.splitext(dat_file)[0]
decode_image(file_path, file_name, output_path)
logging.info(f'正在处理--->[{index}/{total_files}]{index / total_files *100:.2f}%')
def get_dat_files(directory):
"""获取目录下所有.dat文件"""
return [f for f in os.listdir(directory) if f.endswith('.dat')]
def decode_image(dat_path, file_name, output_path):
"""解码DAT文件为图片"""
try:
xor_value, image_type = detect_format(dat_path)
if image_type == 1:
extension = '.png'
elif image_type == 2:
extension = '.gif'
else:
extension = '.jpg'
output_file = os.path.join(output_path, f"{file_name}{extension}")
with open(dat_path, "rb") as dat_file, open(output_file, "wb") as img_file:
dat_file.seek(0)
for byte in dat_file.read():
img_file.write(bytes([byte ^ xor_value]))
logging.info(f"成功解码 {dat_path} -> {output_file}")
except Exception as e:
logging.error(f"解码失败:{dat_path},错误信息:{e}")
def detect_format(dat_path):
"""检测DAT文件的图片格式"""
with open(dat_path, "rb") as dat_file:
header = dat_file.read(3)
formats = [
(b'\x89\x50\x4e', 1), # PNG
(b'\x47\x49\x46', 2), # GIF
(b'\xff\xd8\xff', 3) # JPG
]
for xor_bytes, image_type in formats:
xor_result = bytes([header[i] ^ xor_bytes[i] for i in range(3)])
if xor_result[0] == xor_result[1] == xor_result[2]:
return xor_result[0], image_type
raise ValueError("无法识别的图片格式")
if __name__ == '__main__':
# 输入和输出路径
into_path = r'G:\微信聊天记录\WeChat Files\FileStorage\MsgAttach\2a439fca32ade9c75bab36005e2c4bb4\Thumb\2024-04' # 微信图片DAT文件存放路径
out_path = './output' # 转换后的图片存放路径
main(into_path, out_path) |