sonnet4.6: 如何在windows/android上使用无障碍功能保存微信群图片

以下是两条路的完整拆解,均不需要降级微信


🖥️ 路线 A:Windows — pywinauto (UIA Accessibility)

原理:通过 Python + pywinauto 库(backend='uia')连接微信进程,实现对界面元素的自动化操作,用 Windows UI Automation 监听聊天区图片元素,右键→另存为。

来源

完整脚本

# 安装依赖
# pip install pywinauto pywin32 psutil

import time, os, psutil
from pywinauto.application import Application
import uiautomation as uia  # pip install uiautomation

SAVE_DIR = r"C:\WechatGroupPics"
GROUP_NAME = "你的群名称"
os.makedirs(SAVE_DIR, exist_ok=True)

def get_wechat_pid():
    for p in psutil.process_iter(['pid','name']):
        if p.info['name'] == 'WeChat.exe':
            return p.info['pid']

def connect_wechat():
    pid = get_wechat_pid()
    app = Application(backend='uia').connect(process=pid)
    return app.window(class_name='WeChatMainWndForPC')

def open_group(win, group_name):
    # 搜索群
    win.Edit.set_text(group_name)
    time.sleep(1)
    win.ListItem.click()
    time.sleep(1)

def save_all_images(win):
    # 遍历聊天区所有图片控件
    chat_area = win.child_window(control_type='List')
    items = chat_area.children(control_type='ListItem')
    count = 0
    for item in items:
        imgs = item.descendants(control_type='Image')
        for img in imgs:
            try:
                img.right_click_input()
                time.sleep(0.3)
                # 点「另存为」
                menu = uia.MenuControl(searchDepth=2)
                for m in menu.GetChildren():
                    if '另存' in (m.Name or ''):
                        m.Click()
                        time.sleep(0.5)
                        # 自动填路径并确认
                        dlg = uia.WindowControl(Name='另存为')
                        dlg.EditControl().SetValue(
                            os.path.join(SAVE_DIR, f"img_{count}.jpg"))
                        dlg.ButtonControl(Name='保存').Click()
                        count += 1
                        time.sleep(0.3)
            except:
                pass
    print(f"共保存 {count} 张")

win = connect_wechat()
open_group(win, GROUP_NAME)
save_all_images(win)

已有的 MCP 封装(可直接接 Claude Desktop)

WinAutoWx@pmhw/WinAutoWx)是基于 Python + pywinauto 的 MCP server,支持 Windows 微信自动化操作,包括搜索好友/群、发消息,可通过 HTTP API 或 MCP 协议调用。


📱 路线 B:Android — ADB + wechat-dump(需 Root)

原理:微信在 Android 上把图片存在 /data/data/com.tencent.mm/MicroMsg/${userid}/image2/,通过 ADB 直接拉取。

来源ppwwyyxx/wechat-dumphttps://github.com/ppwwyyxx/wechat-dump
该工具最后验证时间为 2025/01/01,需要 ADB + Root 的 Android 设备,拉取 /data/data/com.tencent.mm/MicroMsg/${userid}/image2 目录即可获得所有图片资源。

# Windows PowerShell 执行
# 1. 确认设备已 root 并连接
adb devices

# 2. 拉取整个 image2 目录(替换 ${userid} 为你的32位hex用户ID)
adb pull /data/data/com.tencent.mm/MicroMsg/${userid}/image2 C:\WechatGroupPics

# 3. 图片是 .dat 加密格式,用 wechat-dump 解码
git clone https://github.com/ppwwyyxx/wechat-dump
cd wechat-dump
pip install -r requirements.txt
python ./android-interact.sh db

📱 路线 C:Android — UIAutomator(不需要 Root

原理:通过 adb shell uiautomator 模拟用户点击,遍历群聊图片并长按→保存。

# 推滚动群聊,长按图片,点保存到相册
adb shell uiautomator runtest /path/to/SaveImages.jar -c com.example.SaveImages

或用 Python 的 uiautomator2(更简单):

pip install uiautomator2
python -m uiautomator2 init   # 首次在设备上安装 agent
import uiautomator2 as u2, time

d = u2.connect()  # 连接手机

# 打开群聊(假设已在群聊界面)
# 向上滚动找图片并保存
while True:
    imgs = d(resourceId="com.tencent.mm:id/b4o")  # 图片控件ID(需用 uiautomator2 inspector 确认)
    for img in imgs:
        img.long_click()
        time.sleep(0.5)
        d(text="保存").click()
        time.sleep(0.3)
    d.swipe_ext("up", scale=0.8)  # 继续上滑加载更多
    time.sleep(1)

方案对比

方案 是否需要降级 是否需要Root 稳定性
Windows pywinauto (UIA) ❌ 不需要 中(UI变化会失效)
Android wechat-dump + ADB ❌ 不需要 ✅ 需要
Android uiautomator2 ❌ 不需要 ❌ 不需要

最推荐:Windows 用 pywinauto,Android 无 root 用 uiautomator2。

posted @ 2026-03-24 11:59  AI健康  阅读(3)  评论(0)    收藏  举报