将微信聊天记录长截图进行切割,除第一张切图外,其余切图包含上张切图的后几句聊天记录,支持2种格式,单张或双张切图。
依赖
- pillow
- tkinter
运行
python main.py
操作说明
选择长截图所在文件夹,点击运行即可。
程序会遍历所选文件夹下的所有图片,对其进行截图,结果保存在所选文件夹下的所选图片同名的文件夹下,按顺序进行编号。
主要代码
def splitPicture(file_name: str, width_dt: int = 10):
config_file = "config.json"
if os.path.exists(config_file):
config_json = getConfigData(config_file)
else:
config_json = {"图片高度": 2400, "重复高度": 200}
with open(config_file, "w", encoding="utf-8") as f:
f.write(json.dumps(config_json, ensure_ascii=False))
pic_height = config_json["图片高度"]
idt_height = config_json["重复高度"]
path, text = os.path.splitext(file_name)
_, name = os.path.split(path)
if not os.path.exists(path):
os.mkdir(path)
im = Image.open(file_name)
image_width, image_height = im.size
i = 1
upper = 0
to_image = None
while upper < image_height:
if upper > idt_height:
upper -= idt_height
bottom = upper + pic_height
if bottom > image_height:
bottom = image_height
region = im.crop((0, upper, image_width, bottom))
region.save(os.path.join(path, f"{name}_{i:02d}{text}"))
if to_image is None:
to_image = Image.new(
"RGB", (image_width * 2 + width_dt, pic_height), "white"
)
to_image.paste(region, (0, 0))
else:
to_image.paste(region, (image_width + width_dt, 0))
to_image.save(os.path.join(path, f"_{name}_{(i-1):02d}{text}"))
to_image = Image.new(
"RGB", (image_width * 2 + width_dt, pic_height), "white"
)
to_image.paste(region, (0, 0))
upper = bottom
i += 1
return None
配置
参见config.json
{
"图片高度": 2400,
"重复高度": 200
}
打包
pyinstaller -F -w -i .\favicon.ico -n 切图 .\main.py -i .\favicon.ico -n 切图 .\main.py