Python中如何对Windows版微信接收到的图片进行自动批量处理
同事经常发图片到微信,我要对发来的图片做处理,由于图片量较大,想用脚本代替,语言准备选择 python3,相关的库就是 PIL,但是微信 windows 版中,找不到发来的图片,没法自动处理,有朋友做过类似的脚本嘛?请指教,多谢!
Python中如何对Windows版微信接收到的图片进行自动批量处理
我能马上想到的办法就是用浏览器版的微信,然后通过浏览器油猴 js 脚本把聊天图片下载到本地, 然后本地用 python 之类的语言处理图片
要自动批量处理Windows微信接收的图片,关键是要找到图片存储路径并编写处理脚本。微信默认将接收的图片存储在用户的Documents\WeChat Files目录下。
这里提供一个完整的Python解决方案,包含查找图片、批量重命名和转换格式的示例:
import os
import shutil
from pathlib import Path
from PIL import Image
import re
def find_wechat_image_dir():
"""查找微信图片存储目录"""
user_profile = os.environ['USERPROFILE']
wechat_base = Path(user_profile) / 'Documents' / 'WeChat Files'
if not wechat_base.exists():
raise FileNotFoundError("微信目录未找到")
# 查找所有微信号目录
wechat_ids = [d for d in wechat_base.iterdir() if d.is_dir()]
for wechat_id in wechat_ids:
image_dir = wechat_id / 'FileStorage' / 'Image'
if image_dir.exists():
return image_dir
raise FileNotFoundError("微信图片目录未找到")
def batch_rename_images(image_dir, pattern=r'^.*\.(jpg|jpeg|png|bmp)$', prefix='wechat_'):
"""批量重命名图片文件"""
image_dir = Path(image_dir)
image_files = []
# 递归查找所有图片文件
for ext in ['*.jpg', '*.jpeg', '*.png', '*.bmp', '*.gif']:
image_files.extend(image_dir.rglob(ext))
for i, img_path in enumerate(image_files, 1):
if img_path.is_file():
new_name = f"{prefix}{i:04d}{img_path.suffix}"
new_path = img_path.parent / new_name
try:
shutil.move(str(img_path), str(new_path))
print(f"重命名: {img_path.name} -> {new_name}")
except Exception as e:
print(f"重命名失败 {img_path}: {e}")
def convert_images_format(image_dir, target_format='JPEG', quality=85):
"""转换图片格式"""
image_dir = Path(image_dir)
for img_path in image_dir.rglob('*'):
if img_path.suffix.lower() in ['.png', '.bmp', '.gif']:
try:
with Image.open(img_path) as img:
# 转换为RGB模式(如果是RGBA)
if img.mode in ('RGBA', 'LA'):
background = Image.new('RGB', img.size, (255, 255, 255))
if img.mode == 'RGBA':
background.paste(img, mask=img.split()[-1])
else:
background.paste(img, mask=img.getchannel('A'))
img = background
new_path = img_path.with_suffix(f'.{target_format.lower()}')
img.save(new_path, target_format, quality=quality)
print(f"转换: {img_path.name} -> {new_path.name}")
# 可选:删除原文件
# img_path.unlink()
except Exception as e:
print(f"转换失败 {img_path}: {e}")
def main():
try:
# 1. 找到微信图片目录
image_dir = find_wechat_image_dir()
print(f"找到微信图片目录: {image_dir}")
# 2. 批量重命名(可选)
batch_rename_images(image_dir, prefix='wx_')
# 3. 转换格式(可选)
convert_images_format(image_dir, target_format='JPEG')
print("处理完成!")
except Exception as e:
print(f"错误: {e}")
if __name__ == "__main__":
main()
这个脚本主要做了三件事:
- 自动定位微信图片存储目录
- 批量重命名图片文件(避免微信的随机文件名)
- 统一转换图片格式(如PNG转JPEG)
要使用这个脚本,你需要先安装Pillow库:
pip install Pillow
你可以根据需要修改或扩展处理逻辑,比如添加图片压缩、添加水印、尺寸调整等功能。只需要在convert_images_format函数中添加相应的图像处理代码即可。
总结建议:核心是定位微信存储路径,然后用PIL库进行批量处理。
C:\Users\xxx\Documents\WeChat Files\xxx\Image
C:\Users\xxx\Documents\WeChat Files\xxx\Files
这几个目录的图片不能用吗?
,我找了下这些目录下,没有对方发过来的图片,难道是手机端不能同时登陆的原因?
这是种方法,我等下试下
github 搜索 itchat
图片发到公众号,然后做个公众号后台。
走 anyproxy 来分析内容对应处理?
,这个蛮有用的,我试下,谢谢!
,谢谢
难道就不能直接发压缩包,方便双方?


